Visitar URL original
Fixed issue with generics formatters generation · Pull Request #1534 · MessagePack-CSharp/MessagePack-CSharp · GitHub
Skip to content

Fixed issue with generics formatters generation - #1534

Merged
AArnott merged 1 commit into
MessagePack-CSharp:developfrom
Shapes-R-D:fixGenerics
Dec 10, 2022
Merged

AArnott merged 1 commit into
MessagePack-CSharp:developfrom
Shapes-R-D:fixGenerics

Conversation

@ghost

@ghost ghost commented Nov 25, 2022 •

Copy link
Copy Markdown

There is an issue with resolver generation for a nested generic classes.
For example, this set of classes:

[MessagePackObject]
public class ClassA<T>
{
    [Key(0)]
    public readonly T Field { get; set; }
}

[MessagePackObject]
public class ClassB<T>
{
    [Key(0)]
    public List<ClassA<T>> Field { get; set; }
}

[MessagePackObject]
public class ClassC
{
    [Key(0)]
    public ClassB<float> Field {get;set;}
}

Resolver for this set will contain such generated code:

internal static class GeneratedResolverGetFormatterHelper
{
    private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> lookup;

    static GeneratedResolverGetFormatterHelper()
    {
        lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(5)
        {
            { typeof(global::System.Collections.Generic.List<global::Ti.ClassA<float>>), 0 },
            { typeof(global::System.Collections.Generic.List<global::Ti.ClassA<T>>), 1 },
            { typeof(global::Ti.ClassA<float>), 2 },
            { typeof(global::Ti.ClassB<float>), 3 },
            { typeof(global::Ti.ClassC), 4 },
        };
    }

    internal static object GetFormatter(global::System.Type t)
    {
        int key;
        if (!lookup.TryGetValue(t, out key))
        {
            return null;
        }

        switch (key)
        {
            case 0: return new global::MessagePack.Formatters.ListFormatter<global::Ti.ClassA<float>>();
            case 1: return new global::MessagePack.Formatters.ListFormatter<global::Ti.ClassA<T>>();
            case 2: return new MessagePack.Formatters.Ti.ClassAFormatter<float>();
            case 3: return new MessagePack.Formatters.Ti.ClassBFormatter<float>();
            case 4: return new MessagePack.Formatters.Ti.ClassCFormatter();
            default: return null;
        }
    }
}

Obviously, this code won't compile. I've added a recursive check for open generic type to take such situations into accout. Now resolver generates correctly:

internal static class GeneratedResolverGetFormatterHelper
{
    private static readonly global::System.Collections.Generic.Dictionary<global::System.Type, int> lookup;

    static GeneratedResolverGetFormatterHelper()
    {
        lookup = new global::System.Collections.Generic.Dictionary<global::System.Type, int>(4)
        {
            { typeof(global::System.Collections.Generic.List<global::Ti.ClassA<float>>), 0 },
            { typeof(global::Ti.ClassA<float>), 1 },
            { typeof(global::Ti.ClassB<float>), 2 },
            { typeof(global::Ti.ClassC), 3 },
        };
    }

    internal static object GetFormatter(global::System.Type t)
    {
        int key;
        if (!lookup.TryGetValue(t, out key))
        {
            return null;
        }

        switch (key)
        {
            case 0: return new global::MessagePack.Formatters.ListFormatter<global::Ti.ClassA<float>>();
            case 1: return new MessagePack.Formatters.Ti.ClassAFormatter<float>();
            case 2: return new MessagePack.Formatters.Ti.ClassBFormatter<float>();
            case 3: return new MessagePack.Formatters.Ti.ClassCFormatter();
            default: return null;
        }
    }
}

@AArnott AArnott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this.

@AArnott
AArnott changed the base branch from master to develop December 10, 2022 18:29
@AArnott
AArnott changed the base branch from develop to master December 10, 2022 18:29
@AArnott
AArnott changed the base branch from master to develop December 10, 2022 18:29
@AArnott AArnott added this to the v2.5 milestone Dec 10, 2022
@AArnott
AArnott merged commit 897fbee into MessagePack-CSharp:develop Dec 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant