Skip to content

Commit

Permalink
Merge pull request #575 from Cysharp/hotfix/GeneratorTupleSupport
Browse files Browse the repository at this point in the history
Add support for Tuple/ValueTuple
  • Loading branch information
mayuki authored Dec 2, 2022
2 parents 64ead16 + 3795bc8 commit 98e8b00
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public MagicOnionSerializationInfoCollection Collect(IEnumerable<TypeWithIfDirec
var genericTypeArgs = string.Join(", ", type.GenericArguments.Select(x => x.FullName));

string formatterName;
if (type.Namespace == "MagicOnion" && type.Name == "DynamicArgumentTuple")
if (type is { Namespace: "MagicOnion", Name: "DynamicArgumentTuple" })
{
// MagicOnion.DynamicArgumentTuple
var ctorArguments = string.Join(", ", type.GenericArguments.Select(x => $"default({x.FullName})"));
Expand Down Expand Up @@ -262,6 +262,22 @@ public static class WellKnownSerializationTypes
{"global::System.Collections.Generic.IEnumerable<>", "global::MessagePack.Formatters.InterfaceEnumerableFormatter" },
{"global::System.Linq.ILookup<,>", "global::MessagePack.Formatters.InterfaceLookupFormatter" },
{"global::System.Linq.IGrouping<,>", "global::MessagePack.Formatters.InterfaceGroupingFormatter" },
{"global::System.Tuple<>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,,,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,,,,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.Tuple<,,,,,,,>", "global::MessagePack.Formatters.TupleFormatter" },
{"global::System.ValueTuple<>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,,,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,,,,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
{"global::System.ValueTuple<,,,,,,,>", "global::MessagePack.Formatters.ValueTupleFormatter" },
};

public static readonly HashSet<string> BuiltInTypes = new HashSet<string>(new string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,73 @@ public void KnownTypes_Nullable()
serializationInfoCollection.Enums.Should().BeEmpty();
serializationInfoCollection.Generics.Should().BeEmpty();
}


[Fact]
public void KnownTypes_ValueTuple()
{
// Arrange
var collector = new SerializationInfoCollector(new MagicOnionGeneratorTestOutputLogger(testOutputHelper));
var types = new[]
{
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long, float>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long, float, bool>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long, float, bool, byte>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long, float, bool, byte, short>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<ValueTuple<int, string, long, float, bool, byte, short, Guid>>(), new string[] { }),
};

// Act
var serializationInfoCollection = collector.Collect(types);

// Assert
serializationInfoCollection.Should().NotBeNull();
serializationInfoCollection.Generics.Should().HaveCount(8);
serializationInfoCollection.Generics[0].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32>()");
serializationInfoCollection.Generics[1].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String>()");
serializationInfoCollection.Generics[2].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64>()");
serializationInfoCollection.Generics[3].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single>()");
serializationInfoCollection.Generics[4].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean>()");
serializationInfoCollection.Generics[5].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte>()");
serializationInfoCollection.Generics[6].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte, global::System.Int16>()");
serializationInfoCollection.Generics[7].FormatterName.Should().Be("global::MessagePack.Formatters.ValueTupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte, global::System.Int16, global::System.Guid>()");
}

[Fact]
public void KnownTypes_Tuple()
{
// Arrange
var collector = new SerializationInfoCollector(new MagicOnionGeneratorTestOutputLogger(testOutputHelper));
var types = new[]
{
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long, float>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long, float, bool>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long, float, bool, byte>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long, float, bool, byte, short>>(), new string[] { }),
new SerializationInfoCollector.TypeWithIfDirectives(MagicOnionTypeInfo.CreateFromType<Tuple<int, string, long, float, bool, byte, short, Guid>>(), new string[] { }),
};

// Act
var serializationInfoCollection = collector.Collect(types);

// Assert
serializationInfoCollection.Should().NotBeNull();
serializationInfoCollection.Generics.Should().HaveCount(8);
serializationInfoCollection.Generics[0].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32>()");
serializationInfoCollection.Generics[1].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String>()");
serializationInfoCollection.Generics[2].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64>()");
serializationInfoCollection.Generics[3].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single>()");
serializationInfoCollection.Generics[4].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean>()");
serializationInfoCollection.Generics[5].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte>()");
serializationInfoCollection.Generics[6].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte, global::System.Int16>()");
serializationInfoCollection.Generics[7].FormatterName.Should().Be("global::MessagePack.Formatters.TupleFormatter<global::System.Int32, global::System.String, global::System.Int64, global::System.Single, global::System.Boolean, global::System.Byte, global::System.Int16, global::System.Guid>()");
}

[Fact]
public void DynamicArgumentTuple()
{
Expand Down

0 comments on commit 98e8b00

Please sign in to comment.