Skip to content

[QUICKFIX] Support generating dicts by reflection #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static IGen Build(
new NullableGenHandler(),
new RegistryReflectedGenHandler(registeredGensByType),
new CollectionReflectedGenHandler(),
new DictionaryReflectedGenHandler(),
new ArrayReflectedGenHandler(),
new EnumReflectedGenHandler(),
new DefaultConstructorReflectedGenHandler(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace GalaxyCheck.Gens.ReflectedGenHelpers.ReflectedGenHandlers
{
internal class DictionaryReflectedGenHandler : IReflectedGenHandler
{
public bool CanHandleGen(Type type, ReflectedGenHandlerContext context)
{
if (type.IsGenericType)
{
var genericTypeDefinition = type.GetGenericTypeDefinition();
return GenMethodByGenericTypeDefinition.ContainsKey(genericTypeDefinition);
}

return false;
}

public IGen CreateGen(IReflectedGenHandler innerHandler, Type type, ReflectedGenHandlerContext context)
{
var keyType = type.GetGenericArguments().First();
var elementType = type.GetGenericArguments().Skip(1).First();

var elementNullabilityInfo = context.NullabilityInfo?.GenericTypeArguments.Skip(1).SingleOrDefault();
var keyGen = innerHandler.CreateGen(keyType, context.Next("[*]", keyType, null));
var elementGen = innerHandler.CreateGen(elementType, context.Next("[*]", elementType, elementNullabilityInfo));

var genericTypeDefinition = type.GetGenericTypeDefinition();
var methodName = GenMethodByGenericTypeDefinition[genericTypeDefinition];

var methodInfo = typeof(DictionaryReflectedGenHandler).GetMethod(
methodName,
BindingFlags.Static | BindingFlags.NonPublic)!;

var genericMethodInfo = methodInfo.MakeGenericMethod(keyType, elementType);

return (IGen)genericMethodInfo.Invoke(null!, new object[] { keyGen, elementGen })!;
}

private static readonly IReadOnlyDictionary<Type, string> GenMethodByGenericTypeDefinition = new Dictionary<Type, string>
{
{ typeof(Dictionary<,>), nameof(CreateDictionaryGen) },
};

private static IGen<Dictionary<TKey, TValue>> CreateDictionaryGen<TKey, TValue>(IGen<TKey> keyGen, IGen<TValue> elementGen) where TKey : notnull
{
return Gen
.Int32()
.Between(0, 2)
.SelectMany(count => Gen.Zip(keyGen.SetOf().WithCount(count), elementGen.ListOf().WithCount(count)))
.Select(keysAndValues => Enumerable.Zip(keysAndValues.Item1, keysAndValues.Item2).ToDictionary(x => x.First, x => x.Second));
}
}
}
Loading