Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
coenm committed Aug 9, 2023
1 parent 2b87b4c commit 112ee75
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,24 @@ namespace RepoM.Api.IO.ModuleBasedRepositoryActionProvider.ActionDeserializers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;

public class ActionDeserializerComposition
{
private readonly IActionDeserializer[] _deserializers;
private readonly IKeyTypeRegistration<RepositoryAction>[] _typeRegistrations;

public ActionDeserializerComposition(IEnumerable<IActionDeserializer> deserializers)
public ActionDeserializerComposition(IEnumerable<IActionDeserializer> deserializers, IEnumerable<IKeyTypeRegistration<RepositoryAction>> typeRegistrations)
{
_deserializers = deserializers?.Where(x => x != null).ToArray() ?? throw new ArgumentNullException(nameof(deserializers));
_deserializers = deserializers?.ToArray() ?? throw new ArgumentNullException(nameof(deserializers));
_typeRegistrations = typeRegistrations.ToArray();
}

public RepositoryAction? DeserializeSingleAction(string type, JToken jToken, JsonSerializer jsonSerializer)
{
IActionDeserializer? deserializer = _deserializers.FirstOrDefault(x => x.CanDeserialize(type));

RepositoryAction? result = deserializer?.Deserialize(jToken, this, jsonSerializer);
RepositoryAction? result = DeserializeWithCustomDeserializers(type, jToken, jsonSerializer)
??
DeserializeWithDefaultDeserializers(type, jToken, jsonSerializer);

if (result == null)
{
Expand All @@ -36,4 +39,22 @@ public ActionDeserializerComposition(IEnumerable<IActionDeserializer> deserializ

return result;
}

private RepositoryAction? DeserializeWithCustomDeserializers(string type, JToken jToken, JsonSerializer jsonSerializer)
{
IActionDeserializer? deserializer = _deserializers.FirstOrDefault(x => x.CanDeserialize(type));
return deserializer?.Deserialize(jToken, this, jsonSerializer);
}

private RepositoryAction? DeserializeWithDefaultDeserializers(string type, JToken jToken, JsonSerializer jsonSerializer)
{
IKeyTypeRegistration<RepositoryAction>? registration = _typeRegistrations.FirstOrDefault(x => x.Tag.Equals(type, StringComparison.CurrentCultureIgnoreCase));

if (registration != null)
{
return jToken.ToObject(registration.ConfigurationType, jsonSerializer) as RepositoryAction;
}

return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,21 @@ public static class ContainerExtensions
{
public static void RegisterDefaultRepositoryActionDeserializerForType<T>(this Container container) where T : RepositoryAction
{
container.Collection.AppendInstance<IActionDeserializer>(new DefaultActionDeserializer<T>());
// container.Collection.AppendInstance<IKeyTypeRegistration<RepositoryAction>>(new RepositoryActionKeyTypeRegistration<T>());
container.Collection.AppendInstance<IKeyTypeRegistration<RepositoryAction>>(new RepositoryActionKeyTypeRegistration<T>());
}

public static void RegisterDefaultRepositoryActionDeserializerForType(this Container container, Type type)
{
container.Collection.AppendInstance<IActionDeserializer>(new DefaultActionDeserializer(type));
// container.Collection.AppendInstance<IKeyTypeRegistration<RepositoryAction>>(new RepositoryActionKeyTypeRegistration<T>());
container.Collection.AppendInstance<IKeyTypeRegistration<RepositoryAction>>(new RepositoryActionKeyTypeRegistration(type));
}
}


public class DefaultActionDeserializer : IActionDeserializer
public class DefaultActionDeserializer<T> : IActionDeserializer where T : RepositoryAction
{
public DefaultActionDeserializer(Type t)
public DefaultActionDeserializer()
{
ConfigurationType = t;
Tag = t.GetCustomAttribute<RepositoryActionAttribute>()?.Type ?? throw new InvalidOperationException($"RepositoryActionAttribute not found on {t.FullName}");
ConfigurationType = typeof(T);
Tag = typeof(T).GetCustomAttribute<RepositoryActionAttribute>()?.Type ?? throw new InvalidOperationException($"RepositoryActionAttribute not found on {typeof(T).FullName}");
}

private Type ConfigurationType { get; }
Expand All @@ -171,37 +168,25 @@ public bool CanDeserialize(string type)
}
}


public class DefaultActionDeserializer<T> : IActionDeserializer where T : RepositoryAction
public class RepositoryActionKeyTypeRegistration<T> : IKeyTypeRegistration<RepositoryAction> where T : RepositoryAction
{
public DefaultActionDeserializer()
public RepositoryActionKeyTypeRegistration()
{
ConfigurationType = typeof(T);
Tag = typeof(T).GetCustomAttribute<RepositoryActionAttribute>()?.Type ?? throw new InvalidOperationException($"RepositoryActionAttribute not found on {typeof(T).FullName}");
}

private Type ConfigurationType { get; }

private string Tag { get; }


public bool CanDeserialize(string type)
{
return Tag.Equals(type, StringComparison.CurrentCultureIgnoreCase);
}
public Type ConfigurationType { get; }

public RepositoryAction? Deserialize(JToken jToken, ActionDeserializerComposition actionDeserializer, JsonSerializer jsonSerializer)
{
return jToken.ToObject(ConfigurationType, jsonSerializer) as RepositoryAction;
}
public string Tag { get; }
}

public class RepositoryActionKeyTypeRegistration<T> : IKeyTypeRegistration<RepositoryAction> where T : RepositoryAction
file class RepositoryActionKeyTypeRegistration : IKeyTypeRegistration<RepositoryAction>
{
public RepositoryActionKeyTypeRegistration()
public RepositoryActionKeyTypeRegistration(Type t)
{
ConfigurationType = typeof(T);
Tag = typeof(T).GetCustomAttribute<RepositoryActionAttribute>()?.Type ?? throw new InvalidOperationException($"RepositoryActionAttribute not found on {typeof(T).FullName}");
ConfigurationType = t;
Tag = t.GetCustomAttribute<RepositoryActionAttribute>()?.Type ?? throw new InvalidOperationException($"RepositoryActionAttribute not found on {t.FullName}");
}

public Type ConfigurationType { get; }
Expand Down
46 changes: 15 additions & 31 deletions src/RepoM.App/Bootstrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace RepoM.App;
using RepoM.Api;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.Services.Common;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;

internal static class Bootstrapper
Expand Down Expand Up @@ -133,37 +134,20 @@ public static void RegisterServices(IFileSystem fileSystem)
});

Container.Register<ActionDeserializerComposition>(Lifestyle.Singleton);
//
// Container.Collection.Register<IActionDeserializer>(
// new[] { typeof(IActionDeserializer).Assembly, },
// Lifestyle.Singleton);
var types1 = GetExportedTypesFrom(typeof(IActionDeserializer).Assembly)
.Where(t => typeof(IActionDeserializer).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => t.GetTypeInfo() is { IsAbstract: false, IsGenericTypeDefinition: false, })
// .Where(t => t != typeof(Api.IO.ModuleBasedRepositoryActionProvider.Data.RepositoryAction))
.Where(t => t != typeof(DefaultActionDeserializer))
.Where(t => t != typeof(DefaultActionDeserializer<>))
.ToArray();

Container.Collection.Register<IActionDeserializer>(types1, Lifestyle.Singleton);

// foreach (var type in types1)
// {
// Container.RegisterDefaultRepositoryActionDeserializerForType(type);
// }


var types = GetExportedTypesFrom(typeof(IActionDeserializer).Assembly)
.Where(t => typeof(Api.IO.ModuleBasedRepositoryActionProvider.Data.RepositoryAction).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => t.GetTypeInfo() is { IsAbstract: false, IsGenericTypeDefinition: false, })
.Where(t => t != typeof(Api.IO.ModuleBasedRepositoryActionProvider.Data.RepositoryAction))
// .Where(t => t != typeof(DefaultActionDeserializer<>))
.ToArray();
foreach (var type in types)
{
Container.RegisterDefaultRepositoryActionDeserializerForType(type);
}


// Register custom Repository Action deserializers
var actionDeserializerTypes = GetExportedTypesFrom(typeof(IActionDeserializer).Assembly)
.Where(t => typeof(IActionDeserializer).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => t.GetTypeInfo() is { IsAbstract: false, IsGenericTypeDefinition: false, })
.Where(t => t != typeof(DefaultActionDeserializer<>));
Container.Collection.Register<IActionDeserializer>(actionDeserializerTypes, Lifestyle.Singleton);

// Register all repository action types
GetExportedTypesFrom(typeof(IActionDeserializer).Assembly)
.Where(t => typeof(Api.IO.ModuleBasedRepositoryActionProvider.Data.RepositoryAction).GetTypeInfo().IsAssignableFrom(t.GetTypeInfo()))
.Where(t => t.GetTypeInfo() is { IsAbstract: false, IsGenericTypeDefinition: false, })
.Where(t => t != typeof(Api.IO.ModuleBasedRepositoryActionProvider.Data.RepositoryAction))
.ForEach(t => Container.RegisterDefaultRepositoryActionDeserializerForType(t));

static IEnumerable<Type> GetExportedTypesFrom(Assembly assembly)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ namespace RepoM.Core.Plugin.RepositoryOrdering.Configuration;
/// <summary>
/// Configuration registration per name
/// </summary>
public interface IConfigurationRegistration
public interface IConfigurationRegistration : IKeyTypeRegistration<object>
{
public Type ConfigurationType { get; }

public string Tag { get; }
}

public interface IKeyTypeRegistration<T> : IConfigurationRegistration
/// <summary>
/// Configuration registration per name
/// </summary>
public interface IKeyTypeRegistration<T>
{
public Type ConfigurationType { get; }

public string Tag { get; }
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
namespace RepoM.Api.Tests;

using System;
using System.Collections.Generic;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.ActionDeserializers;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data.Actions;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;

internal static class DynamicRepositoryActionDeserializerFactory
{
Expand All @@ -29,11 +32,12 @@ public static JsonDynamicRepositoryActionDeserializer Create()
new DefaultActionDeserializer<RepositoryActionPinRepositoryV1>(),
new ActionForEachV1Deserializer(),
new DefaultActionDeserializer<RepositoryActionJustTextV1>(),
}));
},
Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}

public static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RepoM.Plugin.AzureDevOps.Tests.ActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Api.Tests.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.AzureDevOps.ActionProvider.Options;
using VerifyTests;
using VerifyXunit;
Expand Down Expand Up @@ -78,6 +79,6 @@ private RepositoryActionConfiguration SutDeserialize(string rawContent, Serializ

private static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new IActionDeserializer[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new IActionDeserializer[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace RepoM.Plugin.AzureDevOps.Tests.TestFramework;

using System;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.ActionDeserializers;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.AzureDevOps.ActionProvider.Options;

internal static class DynamicRepositoryActionDeserializerFactory
Expand All @@ -16,11 +18,12 @@ public static JsonDynamicRepositoryActionDeserializer Create()
{
new DefaultActionDeserializer<RepositoryActionAzureDevOpsCreatePullRequestsV1>(),
new DefaultActionDeserializer<RepositoryActionAzureDevOpsGetPullRequestsV1>(),
}));
},
Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}

public static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RepoM.Plugin.Clipboard.Tests.ActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Api.Tests.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.Clipboard.ActionProvider;
using VerifyTests;
using VerifyXunit;
Expand Down Expand Up @@ -78,6 +79,6 @@ private RepositoryActionConfiguration SutDeserialize(string rawContent, Serializ

private static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace RepoM.Plugin.Clipboard.Tests.TestFramework;

using System;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.ActionDeserializers;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.Clipboard.ActionProvider;

internal static class DynamicRepositoryActionDeserializerFactory
Expand All @@ -15,11 +17,12 @@ public static JsonDynamicRepositoryActionDeserializer Create()
new IActionDeserializer[]
{
new DefaultActionDeserializer<RepositoryActionClipboardCopyV1>(),
}));
},
Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}

public static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RepoM.Plugin.Heidi.Tests.ActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Api.Tests.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.Heidi.ActionProvider;
using VerifyTests;
using VerifyXunit;
Expand Down Expand Up @@ -78,6 +79,6 @@ private RepositoryActionConfiguration SutDeserialize(string rawContent, Serializ

private static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
namespace RepoM.Plugin.Heidi.Tests.TestFramework;

using System;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.ActionDeserializers;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.Heidi.ActionProvider;

internal static class DynamicRepositoryActionDeserializerFactory
Expand All @@ -15,11 +17,12 @@ public static JsonDynamicRepositoryActionDeserializer Create()
new IActionDeserializer[]
{
new DefaultActionDeserializer<RepositoryActionHeidiDatabasesV1>(),
}));
},
Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}

public static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace RepoM.Plugin.SonarCloud.Tests;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Data;
using RepoM.Api.IO.ModuleBasedRepositoryActionProvider.Deserialization;
using RepoM.Api.Tests.IO.ModuleBasedRepositoryActionProvider;
using RepoM.Core.Plugin.RepositoryOrdering.Configuration;
using RepoM.Plugin.SonarCloud;
using VerifyTests;
using VerifyXunit;
Expand Down Expand Up @@ -78,6 +79,6 @@ private RepositoryActionConfiguration SutDeserialize(string rawContent, Serializ

private static JsonDynamicRepositoryActionDeserializer CreateWithDeserializer(IActionDeserializer actionDeserializer)
{
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new IActionDeserializer[] { actionDeserializer, }));
return new JsonDynamicRepositoryActionDeserializer(new ActionDeserializerComposition(new IActionDeserializer[] { actionDeserializer, }, Array.Empty<IKeyTypeRegistration<RepositoryAction>>()));
}
}
Loading

0 comments on commit 112ee75

Please sign in to comment.