From 9f3653bcee5e923c278f15741e2002c746329d23 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Mon, 1 Jul 2024 08:17:19 -0700 Subject: [PATCH 1/2] Move more pieces over to TypeInfo Reworks enum deserialization to go through TypeInfo. This simplifies the code and makes the deserialization slightly faster. --- samples/unions/Program.cs | 4 +- src/generator/Diagnostics.cs | 2 + src/generator/Generator.Deserialize.cs | 5 +- src/generator/Generator.Impl.cs | 140 +++--------------- src/generator/Generator.SerdeTypeInfo.cs | 64 +------- src/generator/Generator.Serialize.cs | 107 ++++--------- src/generator/Generator.Wrapper.cs | 68 ++++----- src/generator/Generator.cs | 130 ++++++++++------ src/generator/Resources.resx | 3 + src/generator/WellKnownTypes.cs | 2 - src/pack/Serde.Pkg.proj | 2 +- src/serde-xml/XmlSerializer.cs | 7 +- src/serde/Attributes.cs | 44 +++--- src/serde/ISerialize.cs | 2 +- src/serde/TypeInfo.cs | 1 + src/serde/json/JsonDeserializer.cs | 65 ++++---- ...zerImpl.cs => JsonSerializer.Serialize.cs} | 71 +++------ .../Serde.Generation.Test/DeserializeTests.cs | 4 +- test/Serde.Generation.Test/SerializeTests.cs | 4 +- test/Serde.Generation.Test/WrapperTests.cs | 40 ++++- ...llInOne.ColorEnumSerdeTypeInfo.verified.cs | 2 +- ...InOne.ColorEnumWrap.ISerialize.verified.cs | 13 +- ...Serde.Test.AllInOne.ISerialize.verified.cs | 32 ++-- .../ColorByteSerdeTypeInfo.verified.cs | 2 +- .../ColorIntSerdeTypeInfo.verified.cs | 2 +- .../ColorLongSerdeTypeInfo.verified.cs | 2 +- .../ColorULongSerdeTypeInfo.verified.cs | 2 +- .../S.ISerialize.verified.cs | 4 +- .../S.ISerialize.verified.cs | 4 +- .../ColorEnumSerdeTypeInfo.verified.cs | 2 +- .../ColorEnumWrap.ISerialize.verified.cs | 13 +- .../ColorEnumSerdeTypeInfo.verified.cs | 2 +- .../ColorEnumWrap.ISerialize.verified.cs | 13 +- .../S.ISerialize.verified.cs | 2 +- .../S2.ISerialize.verified.cs | 2 +- .../S.ISerialize.verified.cs | 4 +- .../TestCase15.Class0.ISerialize.verified.cs | 4 +- .../TestCase15.Class1.ISerialize.verified.cs | 4 +- .../C.ISerialize.verified.cs | 2 +- .../C2.ISerialize.verified.cs | 2 +- ....Nested.Namespace.C.ISerialize.verified.cs | 8 +- ...mespace.ColorByteSerdeTypeInfo.verified.cs | 2 +- ...space.ColorByteWrap.ISerialize.verified.cs | 13 +- ...amespace.ColorIntSerdeTypeInfo.verified.cs | 2 +- ...espace.ColorIntWrap.ISerialize.verified.cs | 13 +- ...mespace.ColorLongSerdeTypeInfo.verified.cs | 2 +- ...space.ColorLongWrap.ISerialize.verified.cs | 13 +- ...espace.ColorULongSerdeTypeInfo.verified.cs | 2 +- ...pace.ColorULongWrap.ISerialize.verified.cs | 13 +- .../C.ISerialize.verified.cs | 2 +- .../RgbSerdeTypeInfo.verified.cs | 2 +- .../RgbWrap.ISerialize.verified.cs | 13 +- .../OPTSWrap.ISerialize.verified.cs | 8 +- .../S.ISerialize.verified.cs | 2 +- .../SectionWrap.ISerialize.verified.cs | 4 +- ...ictionaryGenerate#C.ISerialize.verified.cs | 2 +- ...citGenericWrapper#C.ISerialize.verified.cs | 2 +- .../ExplicitWrapper#C.ISerialize.verified.cs | 2 +- ...onaryImplGenerate#C.ISerialize.verified.cs | 2 +- .../MemberSkip#Rgb.ISerialize.verified.cs | 4 +- ...SkipDeserialize#Rgb.ISerialize.verified.cs | 6 +- ...erSkipSerialize#Rgb.ISerialize.verified.cs | 4 +- .../NestedArray#C.ISerialize.verified.cs | 2 +- .../NestedArray2#C.ISerialize.verified.cs | 2 +- ...tialClasses#A.B.C.D.ISerialize.verified.cs | 2 +- .../NullableFields#S.ISerialize.verified.cs | 4 +- ...NullableRefFields#S.ISerialize.verified.cs | 10 +- .../Rgb#Rgb.ISerialize.verified.cs | 6 +- .../TypeWithArray#C.ISerialize.verified.cs | 2 +- .../Address.ISerialize.verified.cs | 10 +- .../OPTSWrap.ISerialize.verified.cs | 8 +- .../Test.ChannelSerdeTypeInfo.verified.cs | 2 +- .../FinalDiagnostics.verified.txt | 13 ++ .../Outer.SectionSerdeTypeInfo.verified.cs | 14 ++ .../Outer.SectionWrap.ISerialize.verified.cs | 20 +++ .../S.ISerialize.verified.cs | 16 ++ .../SSerdeTypeInfo.verified.cs | 10 ++ .../OPTSWrap.ISerialize.verified.cs | 8 +- .../Outer.SectionWrap.ISerialize.verified.cs | 4 +- .../S.ISerialize.verified.cs | 2 +- .../PointWrap.ISerialize.verified.cs | 4 +- .../target.verified.txt | 2 +- .../Test.Parent.ISerialize.verified.cs | 2 +- .../Test.RecursiveWrap.ISerialize.verified.cs | 2 +- .../target.verified.txt | 2 +- test/Serde.Test/JsonDeserializeTests.cs | 43 ++++++ ...de.Test.AllInOne.ColorEnumSerdeTypeInfo.cs | 2 +- ....Test.AllInOne.ColorEnumWrap.ISerialize.cs | 13 +- .../Serde.Test.AllInOne.ISerialize.cs | 32 ++-- ...tomImplTests.RgbWithFieldMap.ISerialize.cs | 6 +- ...ustomArrayWrapExplicitOnType.ISerialize.cs | 2 +- ...mImArrayExplicitWrapOnMember.ISerialize.cs | 2 +- ...st.JsonSerializerTests.Color.ISerialize.cs | 6 +- ...rializerTests.NullableFields.ISerialize.cs | 4 +- .../Serde.Test.MaxSizeType.ISerialize.cs | 128 ++++++++-------- test/Serde.Xml.Test/XmlTests.cs | 2 +- ...de.Test.AllInOne.ColorEnumSerdeTypeInfo.cs | 2 +- ....Test.AllInOne.ColorEnumWrap.ISerialize.cs | 13 +- .../Serde.Test.AllInOne.ISerialize.cs | 32 ++-- ...erde.Test.SampleTest.Address.ISerialize.cs | 10 +- ....Test.SampleTest.OrderedItem.ISerialize.cs | 10 +- ...est.SampleTest.PurchaseOrder.ISerialize.cs | 12 +- ...rde.Test.XmlTests.BoolStruct.ISerialize.cs | 2 +- ...Serde.Test.XmlTests.MapTest1.ISerialize.cs | 2 +- ...e.Test.XmlTests.NestedArrays.ISerialize.cs | 2 +- ....XmlTests.StructWithIntField.ISerialize.cs | 2 +- ....XmlTests.TypeWithArrayField.ISerialize.cs | 2 +- 107 files changed, 706 insertions(+), 769 deletions(-) rename src/serde/json/{JsonSerializerImpl.cs => JsonSerializer.Serialize.cs} (68%) create mode 100644 test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/FinalDiagnostics.verified.txt create mode 100644 test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionSerdeTypeInfo.verified.cs create mode 100644 test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionWrap.ISerialize.verified.cs create mode 100644 test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/S.ISerialize.verified.cs create mode 100644 test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/SSerdeTypeInfo.verified.cs diff --git a/samples/unions/Program.cs b/samples/unions/Program.cs index 1f49d96b..49bcb31b 100644 --- a/samples/unions/Program.cs +++ b/samples/unions/Program.cs @@ -46,10 +46,10 @@ public void Serialize(BaseType value, ISerializer serializer) serializeType.End(); } - [GenerateSerde(Through = nameof(Value))] + [GenerateSerde(ThroughMember = nameof(Value))] private readonly partial record struct DerivedAWrap(DerivedA Value); - [GenerateSerde(Through = nameof(Value))] + [GenerateSerde(ThroughMember = nameof(Value))] private readonly partial record struct DerivedBWrap(DerivedB Value); } diff --git a/src/generator/Diagnostics.cs b/src/generator/Diagnostics.cs index 90f29204..f15d5ed5 100644 --- a/src/generator/Diagnostics.cs +++ b/src/generator/Diagnostics.cs @@ -16,6 +16,7 @@ internal enum DiagId ERR_CantWrapSpecialType = 3, ERR_CantFindConstructorSignature = 4, ERR_CantFindNestedWrapper = 5, + ERR_WrapperDoesntImplementInterface = 6, } internal static class Diagnostics @@ -27,6 +28,7 @@ internal static class Diagnostics ERR_CantWrapSpecialType => nameof(ERR_CantWrapSpecialType), ERR_CantFindConstructorSignature => nameof(ERR_CantFindConstructorSignature), ERR_CantFindNestedWrapper => nameof(ERR_CantFindNestedWrapper), + ERR_WrapperDoesntImplementInterface => nameof(ERR_WrapperDoesntImplementInterface), }; public static Diagnostic CreateDiagnostic(DiagId id, Location location, params object[] args) diff --git a/src/generator/Generator.Deserialize.cs b/src/generator/Generator.Deserialize.cs index c8373005..09d09763 100644 --- a/src/generator/Generator.Deserialize.cs +++ b/src/generator/Generator.Deserialize.cs @@ -21,7 +21,6 @@ internal class DeserializeImplGenerator internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateDeserializeImpl( GeneratorExecutionContext context, ITypeSymbol receiverType, - ExpressionSyntax receiverExpr, ImmutableList inProgress) { TypeSyntax typeSyntax = ParseTypeName(receiverType.ToDisplayString()); @@ -136,7 +135,7 @@ private static MethodDeclarationSyntax GenerateCustomDeserializeMethod( { wrapperName = explicitWrap.ToString(); } - else if (ImplementsSerde(m.Type, context, SerdeUsage.Deserialize)) + else if (ImplementsSerde(m.Type, m.Type, context, SerdeUsage.Deserialize)) { wrapperName = memberType; } @@ -494,7 +493,7 @@ private static MemberDeclarationSyntax GenerateCustomTypeVisitor( { wrapperName = explicitWrap.ToString(); } - else if (ImplementsSerde(m.Type, context, SerdeUsage.Deserialize)) + else if (ImplementsSerde(m.Type, m.Type, context, SerdeUsage.Deserialize)) { wrapperName = memberType; } diff --git a/src/generator/Generator.Impl.cs b/src/generator/Generator.Impl.cs index 7e88c01e..4f4292a5 100644 --- a/src/generator/Generator.Impl.cs +++ b/src/generator/Generator.Impl.cs @@ -57,110 +57,9 @@ internal void AddSource(string fileName, string content) partial class SerdeImplRoslynGenerator { internal static void GenerateImpl( - AttributeData attributeData, - SerdeUsage usage, - BaseTypeDeclarationSyntax typeDecl, - SemanticModel model, - GeneratorExecutionContext context, - ImmutableList inProgress) - { - var typeSymbol = model.GetDeclaredSymbol(typeDecl); - if (typeSymbol is null) - { - return; - } - - ITypeSymbol receiverType; - ExpressionSyntax receiverExpr; - string? wrapperName; - string? wrappedName; - // If the Through property is set, then we are implementing a wrapper type - if (attributeData.NamedArguments is [ (nameof(GenerateSerialize.Through), { Value: string memberName }) ]) - { - var members = model.LookupSymbols(typeDecl.SpanStart, typeSymbol, memberName); - if (members.Length != 1) - { - // TODO: Error about bad lookup - return; - } - receiverType = SymbolUtilities.GetSymbolType(members[0]); - receiverExpr = IdentifierName(memberName); - wrapperName = typeDecl.Identifier.ValueText; - wrappedName = receiverType.ToDisplayString(); - } - // Enums are also always wrapped, but the attribute is on the enum itself - else if (typeDecl.IsKind(SyntaxKind.EnumDeclaration)) - { - receiverType = typeSymbol; - receiverExpr = IdentifierName("Value"); - wrappedName = typeDecl.Identifier.ValueText; - wrapperName = GetWrapperName(wrappedName); - } - // Just a normal interface implementation - else - { - wrapperName = null; - wrappedName = null; - if (!typeDecl.Modifiers.Any(tok => tok.IsKind(SyntaxKind.PartialKeyword))) - { - // Type must be partial - context.ReportDiagnostic(CreateDiagnostic( - DiagId.ERR_TypeNotPartial, - typeDecl.Identifier.GetLocation(), - typeDecl.Identifier.ValueText)); - return; - } - receiverType = typeSymbol; - receiverExpr = ThisExpression(); - } - - GenerateImpl( - usage, - new TypeDeclContext(typeDecl), - receiverType, - receiverExpr, - context, - inProgress); - } - - private static void GenerateEnumWrapper( - BaseTypeDeclarationSyntax typeDecl, - SemanticModel semanticModel, - GeneratorExecutionContext context) - { - var receiverType = semanticModel.GetDeclaredSymbol(typeDecl); - if (receiverType is null) - { - return; - } - - // Generate enum wrapper stub - var typeDeclContext = new TypeDeclContext(typeDecl); - var typeName = typeDeclContext.Name; - var wrapperName = GetWrapperName(typeName); - var newType = SyntaxFactory.ParseMemberDeclaration($$""" -readonly partial struct {{wrapperName}} { } -""")!; - newType = typeDeclContext.WrapNewType(newType); - string fullWrapperName = string.Join(".", typeDeclContext.NamespaceNames - .Concat(typeDeclContext.ParentTypeInfo.Select(x => x.Name)) - .Concat(new[] { wrapperName })); - - var tree = CompilationUnit( - externs: default, - usings: default, - attributeLists: default, - members: List(new[] { newType })); - tree = tree.NormalizeWhitespace(eol: Environment.NewLine); - - context.AddSource(fullWrapperName, Environment.NewLine + tree.ToFullString()); - } - - private static void GenerateImpl( SerdeUsage usage, TypeDeclContext typeDeclContext, ITypeSymbol receiverType, - ExpressionSyntax receiverExpr, GeneratorExecutionContext context, ImmutableList inProgress) { @@ -169,8 +68,8 @@ private static void GenerateImpl( // Generate statements for the implementation var (implMembers, baseList) = usage switch { - SerdeUsage.Serialize => SerializeImplRoslynGenerator.GenerateSerializeGenericImpl(context, receiverType, receiverExpr, inProgress), - SerdeUsage.Deserialize => DeserializeImplGenerator.GenerateDeserializeImpl(context, receiverType, receiverExpr, inProgress), + SerdeUsage.Serialize => SerializeImplRoslynGenerator.GenerateSerializeGenericImpl(context, receiverType, inProgress), + SerdeUsage.Deserialize => DeserializeImplGenerator.GenerateDeserializeImpl(context, receiverType, inProgress), _ => throw ExceptionUtilities.Unreachable }; @@ -236,36 +135,34 @@ private static void GenerateImpl( } /// - /// Check to see if the type implements ISerialize or IDeserialize, depending on the WrapUsage. + /// Check to see if the implements ISerialize{} or IDeserialize{}, depending on the WrapUsage. /// - internal static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionContext context, SerdeUsage usage) + internal static bool ImplementsSerde(ITypeSymbol targetType, ITypeSymbol argType, GeneratorExecutionContext context, SerdeUsage usage) { // Nullable types are not considered as implementing the Serde interfaces -- they use wrappers to map to the underlying - if (memberType.NullableAnnotation == NullableAnnotation.Annotated || - memberType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) + if (argType.NullableAnnotation == NullableAnnotation.Annotated || + argType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) { return false; } // Check if the type either has the GenerateSerialize attribute, or directly implements ISerialize // (If the type has the GenerateSerialize attribute then the generator will implement the interface) - if (memberType.TypeKind is not TypeKind.Enum && HasGenerateAttribute(memberType, usage)) + if (argType.TypeKind is not TypeKind.Enum && HasGenerateAttribute(argType, usage)) { return true; } - INamedTypeSymbol? serdeSymbol; - if (usage == SerdeUsage.Serialize) - { - serdeSymbol = context.Compilation.GetTypeByMetadataName("Serde.ISerialize"); - } - else - { - var deserialize = context.Compilation.GetTypeByMetadataName("Serde.IDeserialize`1"); - serdeSymbol = deserialize?.Construct(memberType); - } - if (serdeSymbol is not null && memberType.Interfaces.Contains(serdeSymbol, SymbolEqualityComparer.Default) - || (memberType is ITypeParameterSymbol param && param.ConstraintTypes.Contains(serdeSymbol, SymbolEqualityComparer.Default))) + var mdName = usage switch { + SerdeUsage.Serialize => "Serde.ISerialize`1", + SerdeUsage.Deserialize => "Serde.IDeserialize`1", + _ => throw new ArgumentException("Invalid SerdeUsage", nameof(usage)) + }; + var serdeSymbol = context.Compilation.GetTypeByMetadataName(mdName)?.Construct(argType); + + if (serdeSymbol is not null && targetType.AllInterfaces.Contains(serdeSymbol, SymbolEqualityComparer.Default) + || (targetType is ITypeParameterSymbol param && param.ConstraintTypes.Contains(serdeSymbol, SymbolEqualityComparer.Default))) { return true; } @@ -298,8 +195,7 @@ internal static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionC return SyntaxFactory.ParseTypeName(wrapperFqn); } - private static string GetWrapperName(string typeName) => typeName + "Wrap"; - + internal static string GetWrapperName(string typeName) => typeName + "Wrap"; internal static bool HasGenerateAttribute(ITypeSymbol memberType, SerdeUsage usage) { diff --git a/src/generator/Generator.SerdeTypeInfo.cs b/src/generator/Generator.SerdeTypeInfo.cs index 5dd7cf36..47c1a9f5 100644 --- a/src/generator/Generator.SerdeTypeInfo.cs +++ b/src/generator/Generator.SerdeTypeInfo.cs @@ -25,71 +25,11 @@ internal static class SerdeTypeInfoGenerator /// } /// /// - public static void GenerateTypeInfo( - AttributeData attributeData, - BaseTypeDeclarationSyntax typeDecl, - SemanticModel model, - GeneratorExecutionContext context) - { - var typeSymbol = model.GetDeclaredSymbol(typeDecl); - if (typeSymbol is null) - { - return; - } - - INamedTypeSymbol receiverType; - ExpressionSyntax receiverExpr; - string? wrapperName; - string? wrappedName; - // If the Through property is set, then we are implementing a wrapper type - if (attributeData.NamedArguments is [ (nameof(GenerateSerialize.Through), { Value: string memberName }) ]) - { - var members = model.LookupSymbols(typeDecl.SpanStart, typeSymbol, memberName); - if (members.Length != 1) - { - // TODO: Error about bad lookup - return; - } - receiverType = (INamedTypeSymbol)SymbolUtilities.GetSymbolType(members[0]); - receiverExpr = IdentifierName(memberName); - wrapperName = typeDecl.Identifier.ValueText; - wrappedName = receiverType.ToDisplayString(); - } - // Enums are also always wrapped, but the attribute is on the enum itself - else if (typeDecl.IsKind(SyntaxKind.EnumDeclaration)) - { - receiverType = typeSymbol; - receiverExpr = IdentifierName("Value"); - wrappedName = typeDecl.Identifier.ValueText; - wrapperName = GetWrapperName(wrappedName); - } - // Just a normal interface implementation - else - { - wrapperName = null; - wrappedName = null; - if (!typeDecl.Modifiers.Any(tok => tok.IsKind(SyntaxKind.PartialKeyword))) - { - // Type must be partial - context.ReportDiagnostic(CreateDiagnostic( - DiagId.ERR_TypeNotPartial, - typeDecl.Identifier.GetLocation(), - typeDecl.Identifier.ValueText)); - return; - } - receiverType = typeSymbol; - receiverExpr = ThisExpression(); - } - - GenerateTypeInfo(typeDecl, receiverType, context); - } - public static void GenerateTypeInfo( BaseTypeDeclarationSyntax typeDecl, INamedTypeSymbol receiverType, GeneratorExecutionContext context) { - var statements = new List(); var fieldsAndProps = SymbolUtilities.GetDataMembers(receiverType, SerdeUsage.Both); var typeDeclContext = new TypeDeclContext(typeDecl); @@ -105,7 +45,7 @@ internal static class {{typeName}}SerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "{{typeName}}", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.{{(receiverType.TypeKind == TypeKind.Enum ? "Enum" : "CustomType")}}, new (string, System.Reflection.MemberInfo)[] { {{string.Join("," + Environment.NewLine, fieldsAndProps.Select(x => $@"(""{x.GetFormattedName()}"", typeof({typeString}).Get{(x.Symbol.Kind == SymbolKind.Field ? "Field" : "Property")}(""{x.Name}"")!)"))}} @@ -120,6 +60,4 @@ internal static class {{typeName}}SerdeTypeInfo context.AddSource(fullTypeName, newType); } - - private static string GetWrapperName(string typeName) => typeName + "Wrap"; } \ No newline at end of file diff --git a/src/generator/Generator.Serialize.cs b/src/generator/Generator.Serialize.cs index a688f9bd..2a1f0109 100644 --- a/src/generator/Generator.Serialize.cs +++ b/src/generator/Generator.Serialize.cs @@ -17,12 +17,9 @@ namespace Serde { public partial class SerializeImplRoslynGenerator { - private static string GetWrapperName(string typeName) => typeName + "Wrap"; - internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateSerializeGenericImpl( GeneratorExecutionContext context, ITypeSymbol receiverType, - ExpressionSyntax receiverExpr, ImmutableList inProgress) { var statements = new List(); @@ -32,40 +29,36 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateSerializeGen { // For enums, the generated body should look like // ``` - // var name = receiver switch + // var typeInfo = {typeName}SerdeTypeInfo; + // var index = value switch // { - // Enum.Case1 => "Case1", - // Enum.Case2 => "Case2", - // _ => null + // Enum.Case1 => 0, + // Enum.Case2 => 1, + // var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum '{TypeName}'"); // }; // serializer.SerializeEnumValue("Enum", name, (Underlying)value, default(Underlying)); var enumType = (INamedTypeSymbol)receiverType; var typeSyntax = enumType.ToFqnSyntax(); var underlying = enumType.EnumUnderlyingType!; - var cases = fieldsAndProps.Select(m => SwitchExpressionArm( - ConstantPattern(QualifiedName((NameSyntax)typeSyntax, IdentifierName(m.Name))), - whenClause: null, - expression: StringLiteral(m.GetFormattedName()))); - cases = cases.Concat(new[] { SwitchExpressionArm( - DiscardPattern(), - whenClause: null, - LiteralExpression(SyntaxKind.NullLiteralExpression, Token(SyntaxKind.NullKeyword))) }); - statements.Add(LocalDeclarationStatement(VariableDeclaration( - IdentifierName("var"), - SeparatedList(new[] { VariableDeclarator( - Identifier("name"), - argumentList: null, - EqualsValueClause(SwitchExpression(IdentifierName("value"), SeparatedList(cases)))) })))); + statements.Add(ParseStatement($"var _l_typeInfo = {receiverType.ToFqnSyntax()}SerdeTypeInfo.TypeInfo;")); + statements.Add(ParseStatement($$""" + var index = value switch + { + {{string.Join("," + Environment.NewLine, fieldsAndProps + .Select((m, i) => $"{typeSyntax}.{m.Name} => {i}")) }}, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum '{{enumType.Name}}'"), + }; + """)); var wrapper = TryGetPrimitiveWrapper(underlying, SerdeUsage.Serialize).Unwrap().Wrapper; statements.Add(ParseStatement( - $"serializer.SerializeEnumValue(\"{receiverType.Name}\", name, ({underlying.ToFqnSyntax()})value, default({wrapper.ToFullString()}));")); + $"serializer.SerializeEnumValue(_l_typeInfo, index, ({underlying.ToFqnSyntax()})value, default({wrapper.ToFullString()}));")); } else { // The generated body of ISerialize is // `var _l_typeInfo = {TypeName}SerdeTypeInfo.TypeInfo;` // `var type = serializer.SerializeType(_l_typeInfo); - // type.SerializeField("FieldName", receiver.FieldValue); + // type.SerializeField(_l_typeInfo, FieldIndex, receiver.FieldValue); // type.End(); // `var _l_typeInfo = {TypeName}SerdeTypeInfo.TypeInfo;` @@ -77,9 +70,8 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateSerializeGen for (int i = 0; i < fieldsAndProps.Count; i++) { var m = fieldsAndProps[i]; - // Generate statements of the form `type.SerializeField("FieldName", receiver.FieldValue)` - var memberExpr = MakeMemberAccessExpr(m, receiverExpr); - var typeAndWrapperOpt = MakeSerializeType(m, context, memberExpr, inProgress); + // Generate statements of the form `type.SerializeField("FieldName", value.FieldValue)` + var typeAndWrapperOpt = MakeSerializeType(m, context, inProgress); if (typeAndWrapperOpt is not {} typeAndWrapper) { // No built-in handling and doesn't implement ISerialize, error @@ -92,7 +84,8 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateSerializeGen } else { - statements.Add(MakeSerializeFieldStmt(m, i, memberExpr, typeAndWrapper, receiverExpr)); + var memberExpr = MakeMemberAccessExpr(m, IdentifierName("value")); + statements.Add(MakeSerializeFieldStmt(m, i, memberExpr, typeAndWrapper, IdentifierName("value"))); } } @@ -178,7 +171,6 @@ static ExpressionStatementSyntax MakeSerializeFieldStmt( private static TypeAndWrapper? MakeSerializeType( DataMemberSymbol member, GeneratorExecutionContext context, - ExpressionSyntax memberExpr, ImmutableList inProgress) { // 1. Check for an explicit wrapper @@ -188,7 +180,7 @@ static ExpressionStatementSyntax MakeSerializeFieldStmt( } // 2. Check for a direct implementation of ISerialize - if (ImplementsSerde(member.Type, context, SerdeUsage.Serialize)) + if (SerdeImplRoslynGenerator.ImplementsSerde(member.Type, member.Type, context, SerdeUsage.Serialize)) { return new(member.Type.ToFqnSyntax(), GenericName(Identifier("IdWrap"), TypeArgumentList(SeparatedList(new[] { member.Type.ToFqnSyntax() })))); @@ -198,44 +190,6 @@ static ExpressionStatementSyntax MakeSerializeFieldStmt( return TryGetAnyWrapper(member.Type, context, SerdeUsage.Serialize, inProgress); } - /// - /// Check to see if the type implements ISerialize or IDeserialize, depending on the WrapUsage. - /// - private static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionContext context, SerdeUsage usage) - { - // Nullable types are not considered as implementing the Serde interfaces -- they use wrappers to map to the underlying - if (memberType.NullableAnnotation == NullableAnnotation.Annotated || - memberType.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) - { - return false; - } - - // Check if the type either has the GenerateSerialize attribute, or directly implements ISerialize - // (If the type has the GenerateSerialize attribute then the generator will implement the interface) - if (memberType.TypeKind is not TypeKind.Enum && SerdeImplRoslynGenerator.HasGenerateAttribute(memberType, usage)) - { - return true; - } - - INamedTypeSymbol? serdeSymbol; - if (usage == SerdeUsage.Serialize) - { - serdeSymbol = context.Compilation.GetTypeByMetadataName("Serde.ISerialize`1"); - serdeSymbol = serdeSymbol?.Construct(memberType); - } - else - { - var deserialize = context.Compilation.GetTypeByMetadataName("Serde.IDeserialize`1"); - serdeSymbol = deserialize?.Construct(memberType); - } - if (serdeSymbol is not null && memberType.Interfaces.Contains(serdeSymbol, SymbolEqualityComparer.Default) - || (memberType is ITypeParameterSymbol param && param.ConstraintTypes.Contains(serdeSymbol, SymbolEqualityComparer.Default))) - { - return true; - } - return false; - } - private static TypeSyntax? TryGetExplicitWrapper( DataMemberSymbol member, GeneratorExecutionContext context, @@ -245,18 +199,14 @@ private static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionCo if (TryGetExplicitWrapperType(member, usage, context) is {} wrapperType) { var memberType = member.Type; - var serdeInterfaceName = usage switch - { - SerdeUsage.Serialize => "Serde.ISerialize`1", - SerdeUsage.Deserialize => "Serde.IDeserialize`1", - _ => throw ExceptionUtilities.Unreachable - }; - var serdeInterface = context.Compilation.GetTypeByMetadataName(serdeInterfaceName)?.Construct(memberType); - if (wrapperType.AllInterfaces.Contains(serdeInterface, SymbolEqualityComparer.Default)) + if (SerdeImplRoslynGenerator.ImplementsSerde(wrapperType, memberType, context, usage)) { + // If the wrapper type directly implements the interface, just return it return ParseTypeName(wrapperType.ToDisplayString()); } + // Otherwise, the wrapper type should be an unconstructed generic type which we need to + // construct with the appropriate wrappers for the member type's type arguments. var typeArgs = memberType switch { INamedTypeSymbol n => n.TypeArguments, @@ -352,7 +302,7 @@ private static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionCo foreach (var elemType in elemTypes) { var elemSyntax = elemType.ToFqnSyntax(); - if (ImplementsSerde(elemType, context, usage)) + if (SerdeImplRoslynGenerator.ImplementsSerde(elemType, elemType, context, usage)) { // Special case for List-like types: // If the element type directly implements ISerialize, we can @@ -448,7 +398,7 @@ private static bool ImplementsSerde(ITypeSymbol memberType, GeneratorExecutionCo return null; } - var wrapperName = GetWrapperName(type.Name); + var wrapperName = SerdeImplRoslynGenerator.GetWrapperName(type.Name); var containing = type.ContainingType?.ToDisplayString(); if (containing is null && type.ContainingNamespace is { IsGlobalNamespace: false } ns) { @@ -533,8 +483,7 @@ private static bool HasGenerateAttribute(ITypeSymbol memberType, SerdeUsage usag INamedTypeSymbol t when TryGetWrapperName(t, context, usage) is (var ValueType, (var WrapperType, var Args)) => (ValueType, - MakeWrappedExpression( - WrapperType, Args, context, usage, inProgress)), + MakeWrappedExpression(WrapperType, Args, context, usage, inProgress)), _ => null, }; diff --git a/src/generator/Generator.Wrapper.cs b/src/generator/Generator.Wrapper.cs index 63b61744..bd7e39ce 100644 --- a/src/generator/Generator.Wrapper.cs +++ b/src/generator/Generator.Wrapper.cs @@ -16,53 +16,37 @@ namespace Serde { partial class SerdeImplRoslynGenerator { - internal static void GenerateWrapper( - GeneratorExecutionContext context, - AttributeData attributeData, - TypeDeclarationSyntax typeDecl, - SemanticModel model) + private static void GenerateEnumWrapper( + BaseTypeDeclarationSyntax typeDecl, + SemanticModel semanticModel, + GeneratorExecutionContext context) { - // Type must be partial - if (!typeDecl.Modifiers.Any(tok => tok.IsKind(SyntaxKind.PartialKeyword))) - { - context.ReportDiagnostic(CreateDiagnostic( - DiagId.ERR_TypeNotPartial, - typeDecl.Identifier.GetLocation(), - typeDecl.Identifier.ValueText)); - return; - } - - if (attributeData.ConstructorArguments is not [ { Value: string memberName } ]) - { - return; - } - - var containerSymbol = model.GetDeclaredSymbol(typeDecl)!; - ExpressionSyntax receiverExpr; - ITypeSymbol receiverType; - var members = model.LookupSymbols(typeDecl.SpanStart, containerSymbol, memberName); - if (members.Length != 1) + var receiverType = semanticModel.GetDeclaredSymbol(typeDecl); + if (receiverType is null) { - // Error about bad lookup return; } - receiverType = SymbolUtilities.GetSymbolType(members[0]); - receiverExpr = IdentifierName(memberName); - if (receiverType.SpecialType != SpecialType.None) - { - context.ReportDiagnostic(CreateDiagnostic( - DiagId.ERR_CantWrapSpecialType, - attributeData.ApplicationSyntaxReference!.GetSyntax().GetLocation(), - receiverType)); - return; - } + // Generate enum wrapper stub + var typeDeclContext = new TypeDeclContext(typeDecl); + var typeName = typeDeclContext.Name; + var wrapperName = GetWrapperName(typeName); + var newType = SyntaxFactory.ParseMemberDeclaration($$""" +readonly partial struct {{wrapperName}} { } +""")!; + newType = typeDeclContext.WrapNewType(newType); + string fullWrapperName = string.Join(".", typeDeclContext.NamespaceNames + .Concat(typeDeclContext.ParentTypeInfo.Select(x => x.Name)) + .Concat(new[] { wrapperName })); - var inProgress = ImmutableList.Create(receiverType); + var tree = CompilationUnit( + externs: default, + usings: default, + attributeLists: default, + members: List(new[] { newType })); + tree = tree.NormalizeWhitespace(eol: Environment.NewLine); - SerdeTypeInfoGenerator.GenerateTypeInfo(typeDecl, (INamedTypeSymbol)receiverType, context); - GenerateImpl(SerdeUsage.Serialize, new TypeDeclContext(typeDecl), receiverType, receiverExpr, context, inProgress); - GenerateImpl(SerdeUsage.Deserialize, new TypeDeclContext(typeDecl), receiverType, receiverExpr, context, inProgress); + context.AddSource(fullWrapperName, Environment.NewLine + tree.ToFullString()); } private static TypeSyntax? TryGetCompoundWrapper(ITypeSymbol type, GeneratorExecutionContext context, SerdeUsage usage, ImmutableList inProgress) @@ -128,7 +112,7 @@ INamedTypeSymbol t when TryGetWrapperName(t, context, usage) is { } tuple { var elemTypeSyntax = ParseTypeName(elemType.ToDisplayString()); - if (ImplementsSerde(elemType, context, usage)) + if (ImplementsSerde(elemType, elemType, context, usage)) { // Special case for List-like types: // If the element type directly implements ISerialize, we can @@ -254,7 +238,7 @@ INamedTypeSymbol t when TryGetWrapperName(t, context, usage) is { } tuple { foreach (var attr in symbol.GetAttributes()) { - if (attr.AttributeClass?.Name is "SerdeWrapAttribute") + if (attr.AttributeClass?.Name is nameof(SerdeWrapAttribute)) { if (attr is { ConstructorArguments: { Length: 1 } attrArgs } && attrArgs[0] is { Value: INamedTypeSymbol wrapperType }) diff --git a/src/generator/Generator.cs b/src/generator/Generator.cs index 93936aa9..4af033dc 100644 --- a/src/generator/Generator.cs +++ b/src/generator/Generator.cs @@ -7,6 +7,8 @@ using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.CSharp.Syntax; +using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; +using static Serde.Diagnostics; namespace Serde { @@ -72,59 +74,100 @@ static GenerationOutput GenerateForCtx( CancellationToken cancelToken) { var generationContext = new GeneratorExecutionContext(attrCtx); - var typeDecl = (BaseTypeDeclarationSyntax)attrCtx.TargetNode; - if (typeDecl.IsKind(SyntaxKind.EnumDeclaration)) + RunGeneration(usage, attrCtx, generationContext, cancelToken); + return generationContext.GetOutput(); + + static void RunGeneration( + SerdeUsage usage, + GeneratorAttributeSyntaxContext attrCtx, + GeneratorExecutionContext generationContext, + CancellationToken cancelToken) { - GenerateEnumWrapper( + var typeDecl = (BaseTypeDeclarationSyntax)attrCtx.TargetNode; + var model = attrCtx.SemanticModel; + var typeSymbol = model.GetDeclaredSymbol(typeDecl); + if (typeSymbol is null) + { + return; + } + + var attributeData = attrCtx.Attributes.Single(); + + INamedTypeSymbol receiverType = typeSymbol; + // If the Through property is set, then we are implementing a wrapper type + if (attributeData.NamedArguments is [(nameof(GenerateSerialize.ThroughMember), { Value: string memberName })]) + { + var members = model.LookupSymbols(typeDecl.SpanStart, typeSymbol, memberName); + if (members.Length != 1) + { + // TODO: Error about bad lookup + return; + } + receiverType = (INamedTypeSymbol)SymbolUtilities.GetSymbolType(members[0]); + + if (receiverType.SpecialType != SpecialType.None) + { + generationContext.ReportDiagnostic(CreateDiagnostic( + DiagId.ERR_CantWrapSpecialType, + attributeData.ApplicationSyntaxReference!.GetSyntax().GetLocation(), + receiverType)); + return; + } + } + else if (!typeDecl.IsKind(SyntaxKind.EnumDeclaration)) + { + if (!typeDecl.Modifiers.Any(tok => tok.IsKind(SyntaxKind.PartialKeyword))) + { + // Type must be partial + generationContext.ReportDiagnostic(CreateDiagnostic( + DiagId.ERR_TypeNotPartial, + typeDecl.Identifier.GetLocation(), + typeDecl.Identifier.ValueText)); + return; + } + } + + if (typeDecl.IsKind(SyntaxKind.EnumDeclaration)) + { + GenerateEnumWrapper( + typeDecl, + attrCtx.SemanticModel, + generationContext); + } + + SerdeTypeInfoGenerator.GenerateTypeInfo( typeDecl, - attrCtx.SemanticModel, + receiverType, generationContext); + + var inProgress = ImmutableList.Create(receiverType); + + if (usage.HasFlag(SerdeUsage.Serialize)) + { + GenerateImpl( + SerdeUsage.Serialize, + new TypeDeclContext(typeDecl), + receiverType, + generationContext, + inProgress); + } + + if (usage.HasFlag(SerdeUsage.Deserialize)) + { + GenerateImpl( + SerdeUsage.Deserialize, + new TypeDeclContext(typeDecl), + receiverType, + generationContext, + inProgress); + } } - SerdeTypeInfoGenerator.GenerateTypeInfo( - attrCtx.Attributes.Single(), - (BaseTypeDeclarationSyntax)attrCtx.TargetNode, - attrCtx.SemanticModel, - generationContext); - if (usage.HasFlag(SerdeUsage.Serialize)) - { - SerdeImplRoslynGenerator.GenerateImpl( - attrCtx.Attributes.Single(), - SerdeUsage.Serialize, - (BaseTypeDeclarationSyntax)attrCtx.TargetNode, - attrCtx.SemanticModel, - generationContext, - ImmutableList.Empty); - } - if (usage.HasFlag(SerdeUsage.Deserialize)) - { - SerdeImplRoslynGenerator.GenerateImpl( - attrCtx.Attributes.Single(), - SerdeUsage.Deserialize, - (BaseTypeDeclarationSyntax)attrCtx.TargetNode, - attrCtx.SemanticModel, - generationContext, - ImmutableList.Empty); - } - return generationContext.GetOutput(); } var generateSerdeTypes = context.SyntaxProvider.ForAttributeWithMetadataName( WellKnownAttribute.GenerateSerde.GetFqn(), (_, _) => true, (attrCtx, cancelToken) => GenerateForCtx(SerdeUsage.Both, attrCtx, cancelToken)); - var generateWrapperTypes = context.SyntaxProvider.ForAttributeWithMetadataName( - WellKnownAttribute.GenerateWrapper.GetFqn(), - (_, _) => true, - (attrCtx, cancelToken) => - { - var generationContext = new GeneratorExecutionContext(attrCtx); - SerdeImplRoslynGenerator.GenerateWrapper( - generationContext, - attrCtx.Attributes.Single(), - (TypeDeclarationSyntax)attrCtx.TargetNode, - attrCtx.SemanticModel); - return generationContext.GetOutput(); - }); var generateSerializeTypes = context.SyntaxProvider.ForAttributeWithMetadataName( WellKnownAttribute.GenerateSerialize.GetFqn(), @@ -160,7 +203,6 @@ static GenerationOutput GenerateForCtx( } }; - context.RegisterSourceOutput(generateWrapperTypes, provideOutput); context.RegisterSourceOutput(generateSerdeTypes, provideOutput); context.RegisterSourceOutput(combined, provideOutput); } diff --git a/src/generator/Resources.resx b/src/generator/Resources.resx index c18f9cb4..eed48d64 100644 --- a/src/generator/Resources.resx +++ b/src/generator/Resources.resx @@ -132,4 +132,7 @@ Could not find nested type named '{0}' inside type '{1}'. The wrapped type '{2}' is generic, so the expected wrapper is a parent type with SerializeImpl and DeserializeImpl nested wrappers. + + The wrapper type '{0}' doesn't implement the interface '{1}'. + \ No newline at end of file diff --git a/src/generator/WellKnownTypes.cs b/src/generator/WellKnownTypes.cs index ef9a966b..543adccf 100644 --- a/src/generator/WellKnownTypes.cs +++ b/src/generator/WellKnownTypes.cs @@ -25,7 +25,6 @@ internal enum WellKnownAttribute GenerateSerialize, GenerateDeserialize, GenerateSerde, - GenerateWrapper, SerdeTypeOptions, SerdeMemberOptions } @@ -69,7 +68,6 @@ public static ImmutableArray GetAvailableInterfacesInOrder(Gen WellKnownAttribute.GenerateDeserialize => nameof(WellKnownAttribute.GenerateDeserialize), WellKnownAttribute.GenerateSerialize => nameof(WellKnownAttribute.GenerateSerialize), WellKnownAttribute.GenerateSerde => nameof(WellKnownAttribute.GenerateSerde), - WellKnownAttribute.GenerateWrapper => nameof(WellKnownAttribute.GenerateWrapper), WellKnownAttribute.SerdeTypeOptions => nameof(WellKnownAttribute.SerdeTypeOptions), WellKnownAttribute.SerdeMemberOptions => nameof(WellKnownAttribute.SerdeMemberOptions), }; diff --git a/src/pack/Serde.Pkg.proj b/src/pack/Serde.Pkg.proj index 4a240d1a..19c9dc96 100644 --- a/src/pack/Serde.Pkg.proj +++ b/src/pack/Serde.Pkg.proj @@ -5,7 +5,7 @@ net8.0 Serde - 0.6.0 + 0.7.0-preview1 true BSD-3-Clause https://github.com/agocke/serde-dn diff --git a/src/serde-xml/XmlSerializer.cs b/src/serde-xml/XmlSerializer.cs index 3bcb8f1e..44ea74b0 100644 --- a/src/serde-xml/XmlSerializer.cs +++ b/src/serde-xml/XmlSerializer.cs @@ -68,13 +68,12 @@ public void SerializeDouble(double d) _writer.WriteValue(d); } - public void SerializeEnumValue(string enumName, string? valueName, T value) where T : notnull + void ISerializer.SerializeEnumValue(TypeInfo typeInfo, int index, T value, U serialize) { + var name = typeInfo.GetStringSerializeName(index); + SerializeString(name); } - void ISerializer.SerializeEnumValue(string enumName, string? valueName, T value, U serialize) - {} - public void SerializeFloat(float f) => SerializeDouble(f); public void SerializeI16(short i16) => SerializeI64(i16); diff --git a/src/serde/Attributes.cs b/src/serde/Attributes.cs index cf99b0ca..a9243a38 100644 --- a/src/serde/Attributes.cs +++ b/src/serde/Attributes.cs @@ -23,7 +23,13 @@ sealed class GenerateSerialize : Attribute /// If non-null, the name of the member used to implement serialization. This is used to /// implement serialization for a wrapper type. /// - public string? Through { get; init; } + public string? ThroughMember { get; init; } + + /// + /// If non-null, the name of the type used to implement serialization and deserialization. + /// This is used to implement serialization and deserialization for a wrapper type. + /// + public Type? ThroughType { get; init; } } /// @@ -42,8 +48,13 @@ sealed class GenerateDeserialize : Attribute /// If non-null, the name of the member used to implement deserialization. This is used to /// implement deserialization for a wrapper type. /// - public string? Through { get; init; } + public string? ThroughMember { get; init; } + /// + /// If non-null, the name of the type used to implement serialization and deserialization. + /// This is used to implement serialization and deserialization for a wrapper type. + /// + public Type? ThroughType { get; init; } } /// @@ -62,34 +73,13 @@ sealed class GenerateSerde : Attribute /// If non-null, the name of the member used to implement serialization and deserialization. /// This is used to implement serialization and deserialization for a wrapper type. /// - public string? Through { get; init; } -} + public string? ThroughMember { get; init; } -/// -/// Generates the equivalent of , but delegated to a member of the name -/// passed in as a parameter. -/// -[AttributeUsage(AttributeTargets.Struct, AllowMultiple = false, Inherited = false)] -[Conditional("EMIT_GENERATE_SERDE_ATTRIBUTE")] -#if !SRCGEN -public -#else -internal -#endif -sealed class GenerateWrapper : Attribute -{ /// - /// The name of the member used for delegation. - /// - public string MemberName { get; } - - /// - /// Constructor for GenerateWrapper. + /// If non-null, the name of the type used to implement serialization and deserialization. + /// This is used to implement serialization and deserialization for a wrapper type. /// - public GenerateWrapper(string memberName) - { - MemberName = memberName; - } + public Type? ThroughType { get; init; } } [AttributeUsage( diff --git a/src/serde/ISerialize.cs b/src/serde/ISerialize.cs index 06369cdb..bd3b3909 100644 --- a/src/serde/ISerialize.cs +++ b/src/serde/ISerialize.cs @@ -76,7 +76,7 @@ public interface ISerializer void SerializeDecimal(decimal d); void SerializeString(string s); void SerializeNull(); - void SerializeEnumValue(string enumName, string? valueName, T value, U serialize) + void SerializeEnumValue(TypeInfo typeInfo, int index, T value, U serialize) where T : unmanaged where U : ISerialize; diff --git a/src/serde/TypeInfo.cs b/src/serde/TypeInfo.cs index 77b1c89f..e4bf2b44 100644 --- a/src/serde/TypeInfo.cs +++ b/src/serde/TypeInfo.cs @@ -26,6 +26,7 @@ public enum TypeKind CustomType, Enumerable, Dictionary, + Enum, } public TypeKind Kind { get; } diff --git a/src/serde/json/JsonDeserializer.cs b/src/serde/json/JsonDeserializer.cs index e64000fa..95f65a80 100644 --- a/src/serde/json/JsonDeserializer.cs +++ b/src/serde/json/JsonDeserializer.cs @@ -301,20 +301,22 @@ public T DeserializeString(IDeserializeVisitor v) public T DeserializeIdentifier(IDeserializeVisitor v) => DeserializeString(v); - public T DeserializeType(string typeName, ReadOnlySpan fieldNames, IDeserializeVisitor v) - { - // Types are identical to dictionaries - return DeserializeDictionary(v); - } - public IDeserializeType DeserializeType(TypeInfo fieldMap) { - ref var reader = ref GetReader(); - reader.ReadOrThrow(); + // Custom types look like dictionaries, enums are inline strings + if (fieldMap.Kind == TypeInfo.TypeKind.CustomType) + { + ref var reader = ref GetReader(); + reader.ReadOrThrow(); - if (reader.TokenType != JsonTokenType.StartObject) + if (reader.TokenType != JsonTokenType.StartObject) + { + throw new InvalidDeserializeValueException("Expected object start"); + } + } + else if (fieldMap.Kind != TypeInfo.TypeKind.Enum) { - throw new InvalidDeserializeValueException("Expected object start"); + throw new ArgumentException("Expected either CustomType or Enum kind, found " + fieldMap.Kind); } return this; @@ -365,29 +367,38 @@ V IDeserializeType.ReadValue(int index) int IDeserializeType.TryReadIndex(TypeInfo map, out string? errorName) { - bool foundProperty = false; ref var reader = ref GetReader(); - while (!foundProperty) + if (map.Kind == TypeInfo.TypeKind.Enum) { + // Enums are just treated as strings reader.ReadOrThrow(); - switch (reader.TokenType) + Debug.Assert(reader.TokenType == JsonTokenType.String); + } + else + { + bool foundProperty = false; + while (!foundProperty) { - case JsonTokenType.EndObject: - errorName = null; - return IDeserializeType.EndOfType; - case JsonTokenType.PropertyName: - foundProperty = true; - break; - default: - // If we aren't at a property name, we must be at a value and intending to skip it - // Call Skip in case we are starting a new array or object. Doesn't do - // anything for bare tokens, but we've already read one token forward above, - // so we can simply continue - reader.Skip(); - break; + reader.ReadOrThrow(); + switch (reader.TokenType) + { + case JsonTokenType.EndObject: + errorName = null; + return IDeserializeType.EndOfType; + case JsonTokenType.PropertyName: + foundProperty = true; + break; + default: + // If we aren't at a property name, we must be at a value and intending to skip it + // Call Skip in case we are starting a new array or object. Doesn't do + // anything for bare tokens, but we've already read one token forward above, + // so we can simply continue + reader.Skip(); + break; + } } + Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); } - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); Utf8Span span; if (reader.HasValueSequence || reader.ValueIsEscaped) diff --git a/src/serde/json/JsonSerializerImpl.cs b/src/serde/json/JsonSerializer.Serialize.cs similarity index 68% rename from src/serde/json/JsonSerializerImpl.cs rename to src/serde/json/JsonSerializer.Serialize.cs index c713d17d..52bae073 100644 --- a/src/serde/json/JsonSerializerImpl.cs +++ b/src/serde/json/JsonSerializer.Serialize.cs @@ -31,74 +31,50 @@ internal JsonSerializer(Utf8JsonWriter writer) // Implementations of ISerializer partial class JsonSerializer : ISerializer { - public void SerializeBool(bool b) => _writer.WriteBooleanValue(b); + void ISerializer.SerializeBool(bool b) => _writer.WriteBooleanValue(b); - public void SerializeChar(char c) => SerializeString(c.ToString()); + void ISerializer.SerializeChar(char c) => SerializeString(c.ToString()); - public void SerializeByte(byte b) => _writer.WriteNumberValue(b); + void ISerializer.SerializeByte(byte b) => _writer.WriteNumberValue(b); - public void SerializeU16(ushort u16) => _writer.WriteNumberValue(u16); + void ISerializer.SerializeU16(ushort u16) => _writer.WriteNumberValue(u16); - public void SerializeU32(uint u32) => _writer.WriteNumberValue(u32); + void ISerializer.SerializeU32(uint u32) => _writer.WriteNumberValue(u32); - public void SerializeU64(ulong u64) => _writer.WriteNumberValue(u64); + void ISerializer.SerializeU64(ulong u64) => _writer.WriteNumberValue(u64); - public void SerializeSByte(sbyte b) => _writer.WriteNumberValue(b); + void ISerializer.SerializeSByte(sbyte b) => _writer.WriteNumberValue(b); - public void SerializeI16(short i16) => _writer.WriteNumberValue(i16); + void ISerializer.SerializeI16(short i16) => _writer.WriteNumberValue(i16); - public void SerializeI32(int i32) => _writer.WriteNumberValue(i32); + void ISerializer.SerializeI32(int i32) => _writer.WriteNumberValue(i32); - public void SerializeI64(long i64) => _writer.WriteNumberValue(i64); + void ISerializer.SerializeI64(long i64) => _writer.WriteNumberValue(i64); - public void SerializeFloat(float f) => _writer.WriteNumberValue(f); + void ISerializer.SerializeFloat(float f) => _writer.WriteNumberValue(f); - public void SerializeDouble(double d) => _writer.WriteNumberValue(d); + void ISerializer.SerializeDouble(double d) => _writer.WriteNumberValue(d); - public void SerializeDecimal(decimal d) => _writer.WriteNumberValue(d); + void ISerializer.SerializeDecimal(decimal d) => _writer.WriteNumberValue(d); - public void SerializeString(string s) => _writer.WriteStringValue(s); - public void SerializeNull() => _writer.WriteNullValue(); + private void SerializeString(string s) => _writer.WriteStringValue(s); - public void SerializeNotNull(T t) where T : notnull, ISerialize - { - t.Serialize(t, this); - } - - public void SerializeNotNull(T t, U u) - where T : notnull - where U : ISerialize - { - u.Serialize(t, this); - } - - public void SerializeEnumValue(string enumName, string? valueName, T value) where T : notnull - { - if (valueName is null) - { - throw new InvalidOperationException($"Cannot serialize unnamed enum value '{value}' of enum '{enumName}'"); - } - _writer.WriteStringValue(valueName); - } + void ISerializer.SerializeString(string s) => SerializeString(s); + void ISerializer.SerializeNull() => _writer.WriteNullValue(); - public void SerializeEnumValue(string enumName, string? valueName, T value, U serialize) - where T : unmanaged - where U : ISerialize + void ISerializer.SerializeEnumValue(TypeInfo typeInfo, int index, T value, U serialize) { - if (valueName is null) - { - throw new InvalidOperationException($"Cannot serialize unnamed enum value '{value}' of enum '{enumName}'"); - } + var valueName = typeInfo.GetSerializeName(index); _writer.WriteStringValue(valueName); } - public ISerializeType SerializeType(TypeInfo typeInfo) + ISerializeType ISerializer.SerializeType(TypeInfo typeInfo) { _writer.WriteStartObject(); return this; } - public ISerializeCollection SerializeCollection(TypeInfo typeInfo, int? length) + ISerializeCollection ISerializer.SerializeCollection(TypeInfo typeInfo, int? length) { if (typeInfo.Kind == TypeInfo.TypeKind.Dictionary) { @@ -145,11 +121,8 @@ void ISerializeCollection.End(TypeInfo typeInfo) } - private readonly struct KeySerializer : ISerializer + private sealed class KeySerializer(JsonSerializer _parent) : ISerializer { - private readonly JsonSerializer _parent; - public KeySerializer(JsonSerializer parent) => this._parent = parent; - public void SerializeBool(bool b) => throw new KeyNotStringException(); public void SerializeChar(char c) => throw new KeyNotStringException(); public void SerializeByte(byte b) => throw new KeyNotStringException(); @@ -178,7 +151,7 @@ public void SerializeString(string s) _parent._writer.WritePropertyName(s); } - void ISerializer.SerializeEnumValue(string enumName, string? valueName, T value, U serialize) + void ISerializer.SerializeEnumValue(TypeInfo typeInfo, int index, T value, U serialize) => throw new KeyNotStringException(); public ISerializeCollection SerializeCollection(TypeInfo typeInfo, int? length) => throw new KeyNotStringException(); diff --git a/test/Serde.Generation.Test/DeserializeTests.cs b/test/Serde.Generation.Test/DeserializeTests.cs index b4f6fd3d..618e052f 100644 --- a/test/Serde.Generation.Test/DeserializeTests.cs +++ b/test/Serde.Generation.Test/DeserializeTests.cs @@ -19,7 +19,7 @@ public Task NestedExplicitDeserializeWrapper() using System.Collections.Immutable; using System.Runtime.InteropServices.ComTypes; -[GenerateDeserialize(Through = nameof(Value))] +[GenerateDeserialize(ThroughMember = nameof(Value))] readonly partial record struct OptsWrap(BIND_OPTS Value); [GenerateDeserialize] @@ -40,7 +40,7 @@ public Task DeserializeOnlyWrap() using Serde; using System.Runtime.InteropServices.ComTypes; -[GenerateDeserialize(Through = nameof(Value))] +[GenerateDeserialize(ThroughMember = nameof(Value))] readonly partial record struct Wrap(BIND_OPTS Value); """; diff --git a/test/Serde.Generation.Test/SerializeTests.cs b/test/Serde.Generation.Test/SerializeTests.cs index a11ca752..94afcdf6 100644 --- a/test/Serde.Generation.Test/SerializeTests.cs +++ b/test/Serde.Generation.Test/SerializeTests.cs @@ -20,7 +20,7 @@ public Task NestedExplicitSerializeWrapper() using System.Collections.Immutable; using System.Runtime.InteropServices.ComTypes; -[GenerateSerde(Through = nameof(Value))] +[GenerateSerde(ThroughMember = nameof(Value))] readonly partial record struct OPTSWrap(BIND_OPTS Value); [GenerateSerde] @@ -43,7 +43,7 @@ public Task SerializeOnlyWrapper() using Serde; using System.Collections.Specialized; -[GenerateSerialize(Through = nameof(Value))] +[GenerateSerialize(ThroughMember = nameof(Value))] readonly partial record struct SectionWrap(BitVector32.Section Value); """; diff --git a/test/Serde.Generation.Test/WrapperTests.cs b/test/Serde.Generation.Test/WrapperTests.cs index 105a8367..6a67e70b 100644 --- a/test/Serde.Generation.Test/WrapperTests.cs +++ b/test/Serde.Generation.Test/WrapperTests.cs @@ -21,7 +21,7 @@ public Task NestedExplicitWrapper() partial class Outer { - [GenerateSerialize(Through = nameof(Value))] + [GenerateSerialize(ThroughMember = nameof(Value))] public readonly partial record struct SectionWrap(BitVector32.Section Value); } @@ -36,6 +36,32 @@ partial struct S return VerifyMultiFile(src); } + [Fact] + public Task InvalidNestedWrapper() + { + var src = """ +using Serde; +using System.Collections.Immutable; +using System.Collections.Specialized; + +partial class Outer +{ + [GenerateSerialize(ThroughMember = nameof(Value))] + public readonly partial record struct SectionWrap(BitVector32.Section Value); +} + +[GenerateSerialize] +partial struct S +{ + [SerdeMemberOptions( + // Wrong outer wrapper type + WrapperSerialize = typeof(ArrayWrap.SerializeImpl))] + public ImmutableArray Sections; +} +"""; + return VerifyMultiFile(src); + } + [Fact] public Task GenerateSerdeWrap() { @@ -43,7 +69,7 @@ public Task GenerateSerdeWrap() using System.Runtime.InteropServices.ComTypes; using Serde; -[GenerateSerde(Through = nameof(Value))] +[GenerateSerde(ThroughMember = nameof(Value))] readonly partial record struct OPTSWrap(BIND_OPTS Value); """; @@ -55,7 +81,7 @@ public Task StringWrap() { var src = @" using Serde; -[GenerateWrapper(nameof(_s))] +[GenerateSerde(ThroughMember = nameof(_s))] readonly partial struct StringWrap { private readonly string _s; @@ -75,7 +101,7 @@ public Task RecordStringWrap() { var src = @" using Serde; -[GenerateWrapper(""Wrapped"")] +[GenerateSerde(ThroughMember = ""Wrapped"")] partial record struct StringWrap(string Wrapped); "; return VerifyDiagnostics(src, @@ -94,7 +120,7 @@ class Point public int X; public int Y; } -[GenerateWrapper(nameof(_point))] +[GenerateSerde(ThroughMember = nameof(_point))] partial struct PointWrap { private readonly Point _point; @@ -113,7 +139,7 @@ public Task NestedDeserializeWrap() var src = @" using System.Runtime.InteropServices.ComTypes; -[Serde.GenerateWrapper(nameof(Value))] +[Serde.GenerateSerde(ThroughMember = nameof(Value))] internal readonly partial record struct OPTSWrap(BIND_OPTS Value); [Serde.GenerateDeserialize] @@ -249,7 +275,7 @@ public partial record Recursive using Serde; namespace Test; -[GenerateWrapper(nameof(Value))] +[GenerateSerde(ThroughMember = nameof(Value))] internal partial record struct RecursiveWrap(Recursive Value); [GenerateSerde] diff --git a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.verified.cs index bce38162..61e859b4 100644 --- a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.verified.cs @@ -6,7 +6,7 @@ internal static class ColorEnumSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorEnum", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Red")!), ("blue", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Blue")!), diff --git a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.verified.cs index 1f92c040..6c6217b8 100644 --- a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.verified.cs @@ -12,14 +12,15 @@ partial struct ColorEnumWrap : Serde.ISerialize { void ISerialize.Serialize(Serde.Test.AllInOne.ColorEnum value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var index = value switch { - Serde.Test.AllInOne.ColorEnum.Red => "red", - Serde.Test.AllInOne.ColorEnum.Blue => "blue", - Serde.Test.AllInOne.ColorEnum.Green => "green", - _ => null + Serde.Test.AllInOne.ColorEnum.Red => 0, + Serde.Test.AllInOne.ColorEnum.Blue => 1, + Serde.Test.AllInOne.ColorEnum.Green => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorEnum'"), }; - serializer.SerializeEnumValue("ColorEnum", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } } diff --git a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ISerialize.verified.cs index e6f3771e..e4b4dd5b 100644 --- a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ISerialize.verified.cs @@ -12,22 +12,22 @@ partial record AllInOne : Serde.ISerialize { var _l_typeInfo = AllInOneSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.BoolField); - type.SerializeField(_l_typeInfo, 1, this.CharField); - type.SerializeField(_l_typeInfo, 2, this.ByteField); - type.SerializeField(_l_typeInfo, 3, this.UShortField); - type.SerializeField(_l_typeInfo, 4, this.UIntField); - type.SerializeField(_l_typeInfo, 5, this.ULongField); - type.SerializeField(_l_typeInfo, 6, this.SByteField); - type.SerializeField(_l_typeInfo, 7, this.ShortField); - type.SerializeField(_l_typeInfo, 8, this.IntField); - type.SerializeField(_l_typeInfo, 9, this.LongField); - type.SerializeField(_l_typeInfo, 10, this.StringField); - type.SerializeFieldIfNotNull>(_l_typeInfo, 11, this.NullStringField); - type.SerializeField>(_l_typeInfo, 12, this.UIntArr); - type.SerializeField>>(_l_typeInfo, 13, this.NestedArr); - type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, this.IntImm); - type.SerializeField(_l_typeInfo, 15, this.Color); + type.SerializeField(_l_typeInfo, 0, value.BoolField); + type.SerializeField(_l_typeInfo, 1, value.CharField); + type.SerializeField(_l_typeInfo, 2, value.ByteField); + type.SerializeField(_l_typeInfo, 3, value.UShortField); + type.SerializeField(_l_typeInfo, 4, value.UIntField); + type.SerializeField(_l_typeInfo, 5, value.ULongField); + type.SerializeField(_l_typeInfo, 6, value.SByteField); + type.SerializeField(_l_typeInfo, 7, value.ShortField); + type.SerializeField(_l_typeInfo, 8, value.IntField); + type.SerializeField(_l_typeInfo, 9, value.LongField); + type.SerializeField(_l_typeInfo, 10, value.StringField); + type.SerializeFieldIfNotNull>(_l_typeInfo, 11, value.NullStringField); + type.SerializeField>(_l_typeInfo, 12, value.UIntArr); + type.SerializeField>>(_l_typeInfo, 13, value.NestedArr); + type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, value.IntImm); + type.SerializeField(_l_typeInfo, 15, value.Color); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteSerdeTypeInfo.verified.cs index 6fc8a04d..8cd1f8c0 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorByteSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorByte", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(ColorByte).GetField("Red")!), ("green", typeof(ColorByte).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntSerdeTypeInfo.verified.cs index d1e3a68a..58b8258e 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorIntSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorInt", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(ColorInt).GetField("Red")!), ("green", typeof(ColorInt).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongSerdeTypeInfo.verified.cs index 3b461a9a..1a22f4fa 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorLongSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorLong", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(ColorLong).GetField("Red")!), ("green", typeof(ColorLong).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongSerdeTypeInfo.verified.cs index 6a634a25..95b1f612 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorULongSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorULong", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(ColorULong).GetField("Red")!), ("green", typeof(ColorULong).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.CamelCase/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.CamelCase/S.ISerialize.verified.cs index 04c6c5ab..371700c4 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.CamelCase/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.CamelCase/S.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.One); - type.SerializeField(_l_typeInfo, 1, this.TwoWord); + type.SerializeField(_l_typeInfo, 0, value.One); + type.SerializeField(_l_typeInfo, 1, value.TwoWord); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.Default/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.Default/S.ISerialize.verified.cs index 04c6c5ab..371700c4 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.Default/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.Default/S.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.One); - type.SerializeField(_l_typeInfo, 1, this.TwoWord); + type.SerializeField(_l_typeInfo, 0, value.One); + type.SerializeField(_l_typeInfo, 1, value.TwoWord); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumSerdeTypeInfo.verified.cs index 93eddfb9..61fbe38a 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorEnumSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorEnum", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("Red", typeof(ColorEnum).GetField("Red")!), ("Green", typeof(ColorEnum).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.ISerialize.verified.cs index 75c58740..d9eeca54 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.ISerialize.verified.cs @@ -8,13 +8,14 @@ partial struct ColorEnumWrap : Serde.ISerialize { void ISerialize.Serialize(ColorEnum value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = ColorEnumSerdeTypeInfo.TypeInfo; + var index = value switch { - ColorEnum.Red => "Red", - ColorEnum.Green => "Green", - ColorEnum.Blue => "Blue", - _ => null + ColorEnum.Red => 0, + ColorEnum.Green => 1, + ColorEnum.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorEnum'"), }; - serializer.SerializeEnumValue("ColorEnum", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumSerdeTypeInfo.verified.cs index 4e797c4c..36b131b1 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class ColorEnumSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorEnum", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(ColorEnum).GetField("Red")!), ("green", typeof(ColorEnum).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.ISerialize.verified.cs index 31da5f8e..d9eeca54 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.ISerialize.verified.cs @@ -8,13 +8,14 @@ partial struct ColorEnumWrap : Serde.ISerialize { void ISerialize.Serialize(ColorEnum value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = ColorEnumSerdeTypeInfo.TypeInfo; + var index = value switch { - ColorEnum.Red => "red", - ColorEnum.Green => "green", - ColorEnum.Blue => "blue", - _ => null + ColorEnum.Red => 0, + ColorEnum.Green => 1, + ColorEnum.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorEnum'"), }; - serializer.SerializeEnumValue("ColorEnum", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S.ISerialize.verified.cs index 62e1981e..b7876368 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.E); + type.SerializeField(_l_typeInfo, 0, value.E); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S2.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S2.ISerialize.verified.cs index 26ffb3d1..e5496141 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S2.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/S2.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(S2 value, ISerializer serializer) { var _l_typeInfo = S2SerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.E); + type.SerializeField(_l_typeInfo, 0, value.E); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.KebabCase/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.KebabCase/S.ISerialize.verified.cs index 04c6c5ab..371700c4 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.KebabCase/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.KebabCase/S.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.One); - type.SerializeField(_l_typeInfo, 1, this.TwoWord); + type.SerializeField(_l_typeInfo, 0, value.One); + type.SerializeField(_l_typeInfo, 1, value.TwoWord); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class0.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class0.ISerialize.verified.cs index 13ba3e1d..29b43f83 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class0.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class0.ISerialize.verified.cs @@ -12,8 +12,8 @@ partial class Class0 : Serde.ISerialize { var _l_typeInfo = Class0SerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField>>(_l_typeInfo, 0, this.Field0); - type.SerializeField>(_l_typeInfo, 1, this.Field1); + type.SerializeField>>(_l_typeInfo, 0, value.Field0); + type.SerializeField>(_l_typeInfo, 1, value.Field1); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class1.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class1.ISerialize.verified.cs index 1eb3340a..2120c143 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class1.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.ArrayOfGenerateSerialize/TestCase15.Class1.ISerialize.verified.cs @@ -12,8 +12,8 @@ partial class Class1 : Serde.ISerialize { var _l_typeInfo = Class1SerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Field0); - type.SerializeField(_l_typeInfo, 1, this.Field1); + type.SerializeField(_l_typeInfo, 0, value.Field0); + type.SerializeField(_l_typeInfo, 1, value.Field1); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C.ISerialize.verified.cs index 7369a48c..50b40ff3 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.X); + type.SerializeField(_l_typeInfo, 0, value.X); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C2.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C2.ISerialize.verified.cs index 63871ad9..e526f8d3 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C2.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.DictionaryGenerate2/C2.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C2 value, ISerializer serializer) { var _l_typeInfo = C2SerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.DictWrap.SerializeImpl>>(_l_typeInfo, 0, this.Map); + type.SerializeField, Serde.DictWrap.SerializeImpl>>(_l_typeInfo, 0, value.Map); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.C.ISerialize.verified.cs index 31ac68f3..01ba2472 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.C.ISerialize.verified.cs @@ -12,10 +12,10 @@ partial class C : Serde.ISerialize { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.ColorInt); - type.SerializeField(_l_typeInfo, 1, this.ColorByte); - type.SerializeField(_l_typeInfo, 2, this.ColorLong); - type.SerializeField(_l_typeInfo, 3, this.ColorULong); + type.SerializeField(_l_typeInfo, 0, value.ColorInt); + type.SerializeField(_l_typeInfo, 1, value.ColorByte); + type.SerializeField(_l_typeInfo, 2, value.ColorLong); + type.SerializeField(_l_typeInfo, 3, value.ColorULong); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteSerdeTypeInfo.verified.cs index 197a561a..86ad4535 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteSerdeTypeInfo.verified.cs @@ -4,7 +4,7 @@ internal static class ColorByteSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorByte", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Some.Nested.Namespace.ColorByte).GetField("Red")!), ("green", typeof(Some.Nested.Namespace.ColorByte).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteWrap.ISerialize.verified.cs index 618710f6..4fb0fd0f 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorByteWrap.ISerialize.verified.cs @@ -10,14 +10,15 @@ partial struct ColorByteWrap : Serde.ISerialize { void ISerialize.Serialize(Some.Nested.Namespace.ColorByte value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Some.Nested.Namespace.ColorByteSerdeTypeInfo.TypeInfo; + var index = value switch { - Some.Nested.Namespace.ColorByte.Red => "red", - Some.Nested.Namespace.ColorByte.Green => "green", - Some.Nested.Namespace.ColorByte.Blue => "blue", - _ => null + Some.Nested.Namespace.ColorByte.Red => 0, + Some.Nested.Namespace.ColorByte.Green => 1, + Some.Nested.Namespace.ColorByte.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorByte'"), }; - serializer.SerializeEnumValue("ColorByte", name, (byte)value, default(ByteWrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (byte)value, default(ByteWrap)); } } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntSerdeTypeInfo.verified.cs index 5d78d043..68a4f275 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntSerdeTypeInfo.verified.cs @@ -4,7 +4,7 @@ internal static class ColorIntSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorInt", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Some.Nested.Namespace.ColorInt).GetField("Red")!), ("green", typeof(Some.Nested.Namespace.ColorInt).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntWrap.ISerialize.verified.cs index 953cce0b..792f8123 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorIntWrap.ISerialize.verified.cs @@ -10,14 +10,15 @@ partial struct ColorIntWrap : Serde.ISerialize { void ISerialize.Serialize(Some.Nested.Namespace.ColorInt value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Some.Nested.Namespace.ColorIntSerdeTypeInfo.TypeInfo; + var index = value switch { - Some.Nested.Namespace.ColorInt.Red => "red", - Some.Nested.Namespace.ColorInt.Green => "green", - Some.Nested.Namespace.ColorInt.Blue => "blue", - _ => null + Some.Nested.Namespace.ColorInt.Red => 0, + Some.Nested.Namespace.ColorInt.Green => 1, + Some.Nested.Namespace.ColorInt.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorInt'"), }; - serializer.SerializeEnumValue("ColorInt", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongSerdeTypeInfo.verified.cs index 7c989121..2e57a669 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongSerdeTypeInfo.verified.cs @@ -4,7 +4,7 @@ internal static class ColorLongSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorLong", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Some.Nested.Namespace.ColorLong).GetField("Red")!), ("green", typeof(Some.Nested.Namespace.ColorLong).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongWrap.ISerialize.verified.cs index 758f5acc..697f854d 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorLongWrap.ISerialize.verified.cs @@ -10,14 +10,15 @@ partial struct ColorLongWrap : Serde.ISerialize { void ISerialize.Serialize(Some.Nested.Namespace.ColorLong value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Some.Nested.Namespace.ColorLongSerdeTypeInfo.TypeInfo; + var index = value switch { - Some.Nested.Namespace.ColorLong.Red => "red", - Some.Nested.Namespace.ColorLong.Green => "green", - Some.Nested.Namespace.ColorLong.Blue => "blue", - _ => null + Some.Nested.Namespace.ColorLong.Red => 0, + Some.Nested.Namespace.ColorLong.Green => 1, + Some.Nested.Namespace.ColorLong.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorLong'"), }; - serializer.SerializeEnumValue("ColorLong", name, (long)value, default(Int64Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (long)value, default(Int64Wrap)); } } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongSerdeTypeInfo.verified.cs index 60e4b88e..337a8f0e 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongSerdeTypeInfo.verified.cs @@ -4,7 +4,7 @@ internal static class ColorULongSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorULong", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Some.Nested.Namespace.ColorULong).GetField("Red")!), ("green", typeof(Some.Nested.Namespace.ColorULong).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongWrap.ISerialize.verified.cs index 4ed4fe3b..f311a38e 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.EnumMember/Some.Nested.Namespace.ColorULongWrap.ISerialize.verified.cs @@ -10,14 +10,15 @@ partial struct ColorULongWrap : Serde.ISerialize.Serialize(Some.Nested.Namespace.ColorULong value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Some.Nested.Namespace.ColorULongSerdeTypeInfo.TypeInfo; + var index = value switch { - Some.Nested.Namespace.ColorULong.Red => "red", - Some.Nested.Namespace.ColorULong.Green => "green", - Some.Nested.Namespace.ColorULong.Blue => "blue", - _ => null + Some.Nested.Namespace.ColorULong.Red => 0, + Some.Nested.Namespace.ColorULong.Green => 1, + Some.Nested.Namespace.ColorULong.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorULong'"), }; - serializer.SerializeEnumValue("ColorULong", name, (ulong)value, default(UInt64Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (ulong)value, default(UInt64Wrap)); } } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/C.ISerialize.verified.cs index b64bb0d1..95de9bcd 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeFieldIfNotNull>(_l_typeInfo, 0, this.ColorOpt); + type.SerializeFieldIfNotNull>(_l_typeInfo, 0, value.ColorOpt); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbSerdeTypeInfo.verified.cs index d70b9255..def17343 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbSerdeTypeInfo.verified.cs @@ -3,7 +3,7 @@ internal static class RgbSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "Rgb", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Rgb).GetField("Red")!), ("green", typeof(Rgb).GetField("Green")!), diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbWrap.ISerialize.verified.cs index d5d9dda4..6ba0936d 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.NestedEnumWrapper/RgbWrap.ISerialize.verified.cs @@ -8,13 +8,14 @@ partial struct RgbWrap : Serde.ISerialize { void ISerialize.Serialize(Rgb value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = RgbSerdeTypeInfo.TypeInfo; + var index = value switch { - Rgb.Red => "red", - Rgb.Green => "green", - Rgb.Blue => "blue", - _ => null + Rgb.Red => 0, + Rgb.Green => 1, + Rgb.Blue => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'Rgb'"), }; - serializer.SerializeEnumValue("Rgb", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/OPTSWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/OPTSWrap.ISerialize.verified.cs index 234daf54..7961ee54 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/OPTSWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/OPTSWrap.ISerialize.verified.cs @@ -10,10 +10,10 @@ partial record struct OPTSWrap : Serde.ISerialize(_l_typeInfo, 0, Value.cbStruct); - type.SerializeField(_l_typeInfo, 1, Value.dwTickCountDeadline); - type.SerializeField(_l_typeInfo, 2, Value.grfFlags); - type.SerializeField(_l_typeInfo, 3, Value.grfMode); + type.SerializeField(_l_typeInfo, 0, value.cbStruct); + type.SerializeField(_l_typeInfo, 1, value.dwTickCountDeadline); + type.SerializeField(_l_typeInfo, 2, value.grfFlags); + type.SerializeField(_l_typeInfo, 3, value.grfMode); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/S.ISerialize.verified.cs index c7003332..6bfa5138 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.NestedExplicitSerializeWrapper/S.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 0, this.Opts); + type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 0, value.Opts); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests.SerializeOnlyWrapper/SectionWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests.SerializeOnlyWrapper/SectionWrap.ISerialize.verified.cs index f454a40a..bbafa87b 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests.SerializeOnlyWrapper/SectionWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests.SerializeOnlyWrapper/SectionWrap.ISerialize.verified.cs @@ -10,8 +10,8 @@ partial record struct SectionWrap : Serde.ISerialize(_l_typeInfo, 0, Value.Mask); - type.SerializeField(_l_typeInfo, 1, Value.Offset); + type.SerializeField(_l_typeInfo, 0, value.Mask); + type.SerializeField(_l_typeInfo, 1, value.Offset); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/DictionaryGenerate#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/DictionaryGenerate#C.ISerialize.verified.cs index 19f7ef71..4a40001f 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/DictionaryGenerate#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/DictionaryGenerate#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.DictWrap.SerializeImpl>(_l_typeInfo, 0, this.Map); + type.SerializeField, Serde.DictWrap.SerializeImpl>(_l_typeInfo, 0, value.Map); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitGenericWrapper#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitGenericWrapper#C.ISerialize.verified.cs index 24cdb4d2..b2576002 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitGenericWrapper#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitGenericWrapper#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, SWrap.SerializeImpl>(_l_typeInfo, 0, this.S); + type.SerializeField, SWrap.SerializeImpl>(_l_typeInfo, 0, value.S); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitWrapper#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitWrapper#C.ISerialize.verified.cs index d9e41843..677ee941 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitWrapper#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/ExplicitWrapper#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.S); + type.SerializeField(_l_typeInfo, 0, value.S); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/IDictionaryImplGenerate#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/IDictionaryImplGenerate#C.ISerialize.verified.cs index b566322b..caeba106 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/IDictionaryImplGenerate#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/IDictionaryImplGenerate#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.IDictWrap.SerializeImpl>(_l_typeInfo, 0, this.RDictionary); + type.SerializeField, Serde.IDictWrap.SerializeImpl>(_l_typeInfo, 0, value.RDictionary); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkip#Rgb.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkip#Rgb.ISerialize.verified.cs index 730a11c5..3133bd12 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkip#Rgb.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkip#Rgb.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(Rgb value, ISerializer serializer) { var _l_typeInfo = RgbSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Blue); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipDeserialize#Rgb.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipDeserialize#Rgb.ISerialize.verified.cs index 7b92dbf8..af1c9206 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipDeserialize#Rgb.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipDeserialize#Rgb.ISerialize.verified.cs @@ -10,9 +10,9 @@ void ISerialize.Serialize(Rgb value, ISerializer serializer) { var _l_typeInfo = RgbSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Green); - type.SerializeField(_l_typeInfo, 2, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Green); + type.SerializeField(_l_typeInfo, 2, value.Blue); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipSerialize#Rgb.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipSerialize#Rgb.ISerialize.verified.cs index 730a11c5..3133bd12 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipSerialize#Rgb.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/MemberSkipSerialize#Rgb.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(Rgb value, ISerializer serializer) { var _l_typeInfo = RgbSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Blue); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray#C.ISerialize.verified.cs index 5efecff5..1e5d7f08 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField>>(_l_typeInfo, 0, this.NestedArr); + type.SerializeField>>(_l_typeInfo, 0, value.NestedArr); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray2#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray2#C.ISerialize.verified.cs index 5efecff5..1e5d7f08 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray2#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/NestedArray2#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField>>(_l_typeInfo, 0, this.NestedArr); + type.SerializeField>>(_l_typeInfo, 0, value.NestedArr); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/NestedPartialClasses#A.B.C.D.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/NestedPartialClasses#A.B.C.D.ISerialize.verified.cs index 3dcfa6c6..446aee28 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/NestedPartialClasses#A.B.C.D.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/NestedPartialClasses#A.B.C.D.ISerialize.verified.cs @@ -16,7 +16,7 @@ partial class D : Serde.ISerialize { var _l_typeInfo = DSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Field); + type.SerializeField(_l_typeInfo, 0, value.Field); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/NullableFields#S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/NullableFields#S.ISerialize.verified.cs index 331ff573..11235695 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/NullableFields#S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/NullableFields#S.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize>.Serialize(S value, IS { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeFieldIfNotNull>(_l_typeInfo, 0, this.FI); - type.SerializeFieldIfNotNull>>(_l_typeInfo, 3, this.F3); + type.SerializeFieldIfNotNull>(_l_typeInfo, 0, value.FI); + type.SerializeFieldIfNotNull>>(_l_typeInfo, 3, value.F3); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/NullableRefFields#S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/NullableRefFields#S.ISerialize.verified.cs index da18cdec..5aa62f39 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/NullableRefFields#S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/NullableRefFields#S.ISerialize.verified.cs @@ -10,11 +10,11 @@ void ISerialize>.Serialize(S value, IS { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeFieldIfNotNull>(_l_typeInfo, 0, this.FS); - type.SerializeField>(_l_typeInfo, 1, this.F1); - type.SerializeFieldIfNotNull>(_l_typeInfo, 2, this.F2); - type.SerializeFieldIfNotNull>>(_l_typeInfo, 3, this.F3); - type.SerializeFieldIfNotNull>(_l_typeInfo, 4, this.F4); + type.SerializeFieldIfNotNull>(_l_typeInfo, 0, value.FS); + type.SerializeField>(_l_typeInfo, 1, value.F1); + type.SerializeFieldIfNotNull>(_l_typeInfo, 2, value.F2); + type.SerializeFieldIfNotNull>>(_l_typeInfo, 3, value.F3); + type.SerializeFieldIfNotNull>(_l_typeInfo, 4, value.F4); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/Rgb#Rgb.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/Rgb#Rgb.ISerialize.verified.cs index 7b92dbf8..af1c9206 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/Rgb#Rgb.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/Rgb#Rgb.ISerialize.verified.cs @@ -10,9 +10,9 @@ void ISerialize.Serialize(Rgb value, ISerializer serializer) { var _l_typeInfo = RgbSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Green); - type.SerializeField(_l_typeInfo, 2, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Green); + type.SerializeField(_l_typeInfo, 2, value.Blue); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/SerializeTests/TypeWithArray#C.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/SerializeTests/TypeWithArray#C.ISerialize.verified.cs index ef5066d8..ee966a65 100644 --- a/test/Serde.Generation.Test/test_output/SerializeTests/TypeWithArray#C.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/SerializeTests/TypeWithArray#C.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(C value, ISerializer serializer) { var _l_typeInfo = CSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField>(_l_typeInfo, 0, this.IntArr); + type.SerializeField>(_l_typeInfo, 0, value.IntArr); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.AttributeWrapperTest/Address.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.AttributeWrapperTest/Address.ISerialize.verified.cs index 0b4be560..88c40923 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.AttributeWrapperTest/Address.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.AttributeWrapperTest/Address.ISerialize.verified.cs @@ -10,11 +10,11 @@ void ISerialize
.Serialize(Address value, ISerializer serializer) { var _l_typeInfo = AddressSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Name); - type.SerializeField(_l_typeInfo, 1, this.Line1); - type.SerializeField(_l_typeInfo, 2, this.City); - type.SerializeField(_l_typeInfo, 3, this.State); - type.SerializeField(_l_typeInfo, 4, this.Zip); + type.SerializeField(_l_typeInfo, 0, value.Name); + type.SerializeField(_l_typeInfo, 1, value.Line1); + type.SerializeField(_l_typeInfo, 2, value.City); + type.SerializeField(_l_typeInfo, 3, value.State); + type.SerializeField(_l_typeInfo, 4, value.Zip); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.GenerateSerdeWrap/OPTSWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.GenerateSerdeWrap/OPTSWrap.ISerialize.verified.cs index 234daf54..7961ee54 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.GenerateSerdeWrap/OPTSWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.GenerateSerdeWrap/OPTSWrap.ISerialize.verified.cs @@ -10,10 +10,10 @@ partial record struct OPTSWrap : Serde.ISerialize(_l_typeInfo, 0, Value.cbStruct); - type.SerializeField(_l_typeInfo, 1, Value.dwTickCountDeadline); - type.SerializeField(_l_typeInfo, 2, Value.grfFlags); - type.SerializeField(_l_typeInfo, 3, Value.grfMode); + type.SerializeField(_l_typeInfo, 0, value.cbStruct); + type.SerializeField(_l_typeInfo, 1, value.dwTickCountDeadline); + type.SerializeField(_l_typeInfo, 2, value.grfFlags); + type.SerializeField(_l_typeInfo, 3, value.grfMode); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelSerdeTypeInfo.verified.cs index b44ba9ee..7ca84c57 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelSerdeTypeInfo.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelSerdeTypeInfo.verified.cs @@ -4,7 +4,7 @@ internal static class ChannelSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "Channel", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("a", typeof(Test.Channel).GetField("A")!), ("b", typeof(Test.Channel).GetField("B")!), diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/FinalDiagnostics.verified.txt b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/FinalDiagnostics.verified.txt new file mode 100644 index 00000000..64ff646c --- /dev/null +++ b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/FinalDiagnostics.verified.txt @@ -0,0 +1,13 @@ +[ + { + "Id": "CS7036", + "Title": "", + "Severity": "Error", + "WarningLevel": "0", + "Location": "SerdeGenerator/Serde.SerdeImplRoslynGenerator/S.ISerialize.cs: (11,13)-(11,124)", + "HelpLink": "https://msdn.microsoft.com/query/roslyn.query?appId=roslyn&k=k(CS7036)", + "MessageFormat": "There is no argument given that corresponds to the required parameter '{0}' of '{1}'", + "Message": "There is no argument given that corresponds to the required parameter 'serialize' of 'ISerializeType.SerializeField(TypeInfo, int, T, U)'", + "Category": "Compiler" + } +] \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionSerdeTypeInfo.verified.cs new file mode 100644 index 00000000..4431cc61 --- /dev/null +++ b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionSerdeTypeInfo.verified.cs @@ -0,0 +1,14 @@ +//HintName: Outer.SectionSerdeTypeInfo.cs +partial class Outer +{ + internal static class SectionSerdeTypeInfo +{ + internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( + "Section", + Serde.TypeInfo.TypeKind.CustomType, + new (string, System.Reflection.MemberInfo)[] { +("mask", typeof(System.Collections.Specialized.BitVector32.Section).GetProperty("Mask")!), +("offset", typeof(System.Collections.Specialized.BitVector32.Section).GetProperty("Offset")!) + }); +} +} \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionWrap.ISerialize.verified.cs new file mode 100644 index 00000000..a17191f0 --- /dev/null +++ b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/Outer.SectionWrap.ISerialize.verified.cs @@ -0,0 +1,20 @@ +//HintName: Outer.SectionWrap.ISerialize.cs + +#nullable enable +using System; +using Serde; + +partial class Outer +{ + partial record struct SectionWrap : Serde.ISerialize + { + void ISerialize.Serialize(System.Collections.Specialized.BitVector32.Section value, ISerializer serializer) + { + var _l_typeInfo = SectionSerdeTypeInfo.TypeInfo; + var type = serializer.SerializeType(_l_typeInfo); + type.SerializeField(_l_typeInfo, 0, value.Mask); + type.SerializeField(_l_typeInfo, 1, value.Offset); + type.End(); + } + } +} \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/S.ISerialize.verified.cs new file mode 100644 index 00000000..336ae851 --- /dev/null +++ b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/S.ISerialize.verified.cs @@ -0,0 +1,16 @@ +//HintName: S.ISerialize.cs + +#nullable enable +using System; +using Serde; + +partial struct S : Serde.ISerialize +{ + void ISerialize.Serialize(S value, ISerializer serializer) + { + var _l_typeInfo = SSerdeTypeInfo.TypeInfo; + var type = serializer.SerializeType(_l_typeInfo); + type.SerializeField, Serde.ArrayWrap.SerializeImpl>(_l_typeInfo, 0, value.Sections); + type.End(); + } +} \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/SSerdeTypeInfo.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/SSerdeTypeInfo.verified.cs new file mode 100644 index 00000000..7ce3edf1 --- /dev/null +++ b/test/Serde.Generation.Test/test_output/WrapperTests.InvalidNestedWrapper/SSerdeTypeInfo.verified.cs @@ -0,0 +1,10 @@ +//HintName: SSerdeTypeInfo.cs +internal static class SSerdeTypeInfo +{ + internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( + "S", + Serde.TypeInfo.TypeKind.CustomType, + new (string, System.Reflection.MemberInfo)[] { +("sections", typeof(S).GetField("Sections")!) + }); +} \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.NestedDeserializeWrap/OPTSWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.NestedDeserializeWrap/OPTSWrap.ISerialize.verified.cs index 234daf54..7961ee54 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.NestedDeserializeWrap/OPTSWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.NestedDeserializeWrap/OPTSWrap.ISerialize.verified.cs @@ -10,10 +10,10 @@ partial record struct OPTSWrap : Serde.ISerialize(_l_typeInfo, 0, Value.cbStruct); - type.SerializeField(_l_typeInfo, 1, Value.dwTickCountDeadline); - type.SerializeField(_l_typeInfo, 2, Value.grfFlags); - type.SerializeField(_l_typeInfo, 3, Value.grfMode); + type.SerializeField(_l_typeInfo, 0, value.cbStruct); + type.SerializeField(_l_typeInfo, 1, value.dwTickCountDeadline); + type.SerializeField(_l_typeInfo, 2, value.grfFlags); + type.SerializeField(_l_typeInfo, 3, value.grfMode); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/Outer.SectionWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/Outer.SectionWrap.ISerialize.verified.cs index 2007428d..a17191f0 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/Outer.SectionWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/Outer.SectionWrap.ISerialize.verified.cs @@ -12,8 +12,8 @@ partial record struct SectionWrap : Serde.ISerialize(_l_typeInfo, 0, Value.Mask); - type.SerializeField(_l_typeInfo, 1, Value.Offset); + type.SerializeField(_l_typeInfo, 0, value.Mask); + type.SerializeField(_l_typeInfo, 1, value.Offset); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/S.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/S.ISerialize.verified.cs index bf011d7b..fdb086bc 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/S.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.NestedExplicitWrapper/S.ISerialize.verified.cs @@ -10,7 +10,7 @@ void ISerialize.Serialize(S value, ISerializer serializer) { var _l_typeInfo = SSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 0, this.Sections); + type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 0, value.Sections); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.PointWrap/PointWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.PointWrap/PointWrap.ISerialize.verified.cs index 30bb883f..b0cff239 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.PointWrap/PointWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.PointWrap/PointWrap.ISerialize.verified.cs @@ -10,8 +10,8 @@ void ISerialize.Serialize(Point value, ISerializer serializer) { var _l_typeInfo = PointSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, _point.X); - type.SerializeField(_l_typeInfo, 1, _point.Y); + type.SerializeField(_l_typeInfo, 0, value.X); + type.SerializeField(_l_typeInfo, 1, value.Y); type.End(); } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.RecordStringWrap/target.verified.txt b/test/Serde.Generation.Test/test_output/WrapperTests.RecordStringWrap/target.verified.txt index 4b595c6f..15ebe228 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.RecordStringWrap/target.verified.txt +++ b/test/Serde.Generation.Test/test_output/WrapperTests.RecordStringWrap/target.verified.txt @@ -5,7 +5,7 @@ Title: , Severity: Error, WarningLevel: 0, - Location: : (2,1)-(2,27), + Location: : (2,1)-(2,41), MessageFormat: The type 'string' can't be automatically wrapped because it is a built-in type., Message: The type 'string' can't be automatically wrapped because it is a built-in type., Category: Serde diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.Parent.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.Parent.ISerialize.verified.cs index da4b7fc8..abed678b 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.Parent.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.Parent.ISerialize.verified.cs @@ -12,7 +12,7 @@ partial record Parent : Serde.ISerialize { var _l_typeInfo = ParentSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.R); + type.SerializeField(_l_typeInfo, 0, value.R); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.RecursiveWrap.ISerialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.RecursiveWrap.ISerialize.verified.cs index 45d162d5..fe35bc5c 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.RecursiveWrap.ISerialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.RecursiveType/Test.RecursiveWrap.ISerialize.verified.cs @@ -12,7 +12,7 @@ void ISerialize.Serialize(Recursive value, ISerializer serializer) { var _l_typeInfo = RecursiveSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeFieldIfNotNull(_l_typeInfo, 0, Value.Next); + type.SerializeFieldIfNotNull(_l_typeInfo, 0, value.Next); type.End(); } } diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.StringWrap/target.verified.txt b/test/Serde.Generation.Test/test_output/WrapperTests.StringWrap/target.verified.txt index 69abef1f..ebf802da 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.StringWrap/target.verified.txt +++ b/test/Serde.Generation.Test/test_output/WrapperTests.StringWrap/target.verified.txt @@ -5,7 +5,7 @@ Title: , Severity: Error, WarningLevel: 0, - Location: : (2,1)-(2,28), + Location: : (2,1)-(2,42), MessageFormat: The type 'string' can't be automatically wrapped because it is a built-in type., Message: The type 'string' can't be automatically wrapped because it is a built-in type., Category: Serde diff --git a/test/Serde.Test/JsonDeserializeTests.cs b/test/Serde.Test/JsonDeserializeTests.cs index ed77d4ff..887939ec 100644 --- a/test/Serde.Test/JsonDeserializeTests.cs +++ b/test/Serde.Test/JsonDeserializeTests.cs @@ -292,5 +292,48 @@ public partial record SkipDeserialize [SerdeMemberOptions(SkipDeserialize = true)] public string Skip => "xyz"; } + + [Fact] + public void DeserializeEnum() + { + Assert.Equal(ColorEnum.Red, JsonSerializer.Deserialize("\"red\"")); + } + + private struct ColorEnumWrap : IDeserialize + { + private static readonly TypeInfo s_typeInfo = TypeInfo.Create( + nameof(ColorEnum), + TypeInfo.TypeKind.Enum, + [ + ("red", typeof(ColorEnum).GetField("Red")!), + ("green", typeof(ColorEnum).GetField("Green")!), + ("blue", typeof(ColorEnum).GetField("Blue")!), + ] + ); + + static ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) + { + var typeInfo = s_typeInfo; + var de = deserializer.DeserializeType(s_typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) + { + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + return index switch { + 0 => ColorEnum.Red, + 1 => ColorEnum.Green, + 2 => ColorEnum.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}") + }; + } + } + + private enum ColorEnum + { + Red, + Green, + Blue + } } } \ No newline at end of file diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs index 0e8e8b24..51b577c6 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs @@ -5,7 +5,7 @@ internal static class ColorEnumSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorEnum", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Red")!), ("blue", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Blue")!), diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs index f459c92e..ca2fe45d 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs @@ -11,14 +11,15 @@ partial struct ColorEnumWrap : Serde.ISerialize { void ISerialize.Serialize(Serde.Test.AllInOne.ColorEnum value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var index = value switch { - Serde.Test.AllInOne.ColorEnum.Red => "red", - Serde.Test.AllInOne.ColorEnum.Blue => "blue", - Serde.Test.AllInOne.ColorEnum.Green => "green", - _ => null + Serde.Test.AllInOne.ColorEnum.Red => 0, + Serde.Test.AllInOne.ColorEnum.Blue => 1, + Serde.Test.AllInOne.ColorEnum.Green => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorEnum'"), }; - serializer.SerializeEnumValue("ColorEnum", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs index ad0b22a4..f65b76d3 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs @@ -11,22 +11,22 @@ partial record AllInOne : Serde.ISerialize { var _l_typeInfo = AllInOneSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.BoolField); - type.SerializeField(_l_typeInfo, 1, this.CharField); - type.SerializeField(_l_typeInfo, 2, this.ByteField); - type.SerializeField(_l_typeInfo, 3, this.UShortField); - type.SerializeField(_l_typeInfo, 4, this.UIntField); - type.SerializeField(_l_typeInfo, 5, this.ULongField); - type.SerializeField(_l_typeInfo, 6, this.SByteField); - type.SerializeField(_l_typeInfo, 7, this.ShortField); - type.SerializeField(_l_typeInfo, 8, this.IntField); - type.SerializeField(_l_typeInfo, 9, this.LongField); - type.SerializeField(_l_typeInfo, 10, this.StringField); - type.SerializeFieldIfNotNull>(_l_typeInfo, 11, this.NullStringField); - type.SerializeField>(_l_typeInfo, 12, this.UIntArr); - type.SerializeField>>(_l_typeInfo, 13, this.NestedArr); - type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, this.IntImm); - type.SerializeField(_l_typeInfo, 15, this.Color); + type.SerializeField(_l_typeInfo, 0, value.BoolField); + type.SerializeField(_l_typeInfo, 1, value.CharField); + type.SerializeField(_l_typeInfo, 2, value.ByteField); + type.SerializeField(_l_typeInfo, 3, value.UShortField); + type.SerializeField(_l_typeInfo, 4, value.UIntField); + type.SerializeField(_l_typeInfo, 5, value.ULongField); + type.SerializeField(_l_typeInfo, 6, value.SByteField); + type.SerializeField(_l_typeInfo, 7, value.ShortField); + type.SerializeField(_l_typeInfo, 8, value.IntField); + type.SerializeField(_l_typeInfo, 9, value.LongField); + type.SerializeField(_l_typeInfo, 10, value.StringField); + type.SerializeFieldIfNotNull>(_l_typeInfo, 11, value.NullStringField); + type.SerializeField>(_l_typeInfo, 12, value.UIntArr); + type.SerializeField>>(_l_typeInfo, 13, value.NestedArr); + type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, value.IntImm); + type.SerializeField(_l_typeInfo, 15, value.Color); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.CustomImplTests.RgbWithFieldMap.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.CustomImplTests.RgbWithFieldMap.ISerialize.cs index 78f1b154..c0d3f431 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.CustomImplTests.RgbWithFieldMap.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.CustomImplTests.RgbWithFieldMap.ISerialize.cs @@ -13,9 +13,9 @@ partial record RgbWithFieldMap : Serde.ISerialize(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Green); - type.SerializeField(_l_typeInfo, 2, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Green); + type.SerializeField(_l_typeInfo, 2, value.Blue); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomArrayWrapExplicitOnType.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomArrayWrapExplicitOnType.ISerialize.cs index 6d274b41..0297f5a1 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomArrayWrapExplicitOnType.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomArrayWrapExplicitOnType.ISerialize.cs @@ -13,7 +13,7 @@ partial record struct CustomArrayWrapExplicitOnType : Serde.ISerialize, Serde.Test.GenericWrapperTests.CustomImArray2Wrap.SerializeImpl>(_l_typeInfo, 0, this.A); + type.SerializeField, Serde.Test.GenericWrapperTests.CustomImArray2Wrap.SerializeImpl>(_l_typeInfo, 0, value.A); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomImArrayExplicitWrapOnMember.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomImArrayExplicitWrapOnMember.ISerialize.cs index 1d2d5626..83a08f45 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomImArrayExplicitWrapOnMember.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.GenericWrapperTests.CustomImArrayExplicitWrapOnMember.ISerialize.cs @@ -13,7 +13,7 @@ partial record struct CustomImArrayExplicitWrapOnMember : Serde.ISerialize, Serde.Test.GenericWrapperTests.CustomImArrayWrap.SerializeImpl>(_l_typeInfo, 0, this.A); + type.SerializeField, Serde.Test.GenericWrapperTests.CustomImArrayWrap.SerializeImpl>(_l_typeInfo, 0, value.A); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.Color.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.Color.ISerialize.cs index be61836d..a7a46934 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.Color.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.Color.ISerialize.cs @@ -13,9 +13,9 @@ partial struct Color : Serde.ISerialize { var _l_typeInfo = ColorSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Red); - type.SerializeField(_l_typeInfo, 1, this.Green); - type.SerializeField(_l_typeInfo, 2, this.Blue); + type.SerializeField(_l_typeInfo, 0, value.Red); + type.SerializeField(_l_typeInfo, 1, value.Green); + type.SerializeField(_l_typeInfo, 2, value.Blue); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.NullableFields.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.NullableFields.ISerialize.cs index 6494037e..4cda7e92 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.NullableFields.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.JsonSerializerTests.NullableFields.ISerialize.cs @@ -13,8 +13,8 @@ partial class NullableFields : Serde.ISerialize>(_l_typeInfo, 0, this.S); - type.SerializeField, Serde.DictWrap.SerializeImpl>>(_l_typeInfo, 1, this.D); + type.SerializeFieldIfNotNull>(_l_typeInfo, 0, value.S); + type.SerializeField, Serde.DictWrap.SerializeImpl>>(_l_typeInfo, 1, value.D); type.End(); } } diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.MaxSizeType.ISerialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.MaxSizeType.ISerialize.cs index 986f012a..dcc3110b 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.MaxSizeType.ISerialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.MaxSizeType.ISerialize.cs @@ -11,70 +11,70 @@ partial struct MaxSizeType : Serde.ISerialize { var _l_typeInfo = MaxSizeTypeSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Field1); - type.SerializeField(_l_typeInfo, 1, this.Field2); - type.SerializeField(_l_typeInfo, 2, this.Field3); - type.SerializeField(_l_typeInfo, 3, this.Field4); - type.SerializeField(_l_typeInfo, 4, this.Field5); - type.SerializeField(_l_typeInfo, 5, this.Field6); - type.SerializeField(_l_typeInfo, 6, this.Field7); - type.SerializeField(_l_typeInfo, 7, this.Field8); - type.SerializeField(_l_typeInfo, 8, this.Field9); - type.SerializeField(_l_typeInfo, 9, this.Field10); - type.SerializeField(_l_typeInfo, 10, this.Field11); - type.SerializeField(_l_typeInfo, 11, this.Field12); - type.SerializeField(_l_typeInfo, 12, this.Field13); - type.SerializeField(_l_typeInfo, 13, this.Field14); - type.SerializeField(_l_typeInfo, 14, this.Field15); - type.SerializeField(_l_typeInfo, 15, this.Field16); - type.SerializeField(_l_typeInfo, 16, this.Field17); - type.SerializeField(_l_typeInfo, 17, this.Field18); - type.SerializeField(_l_typeInfo, 18, this.Field19); - type.SerializeField(_l_typeInfo, 19, this.Field20); - type.SerializeField(_l_typeInfo, 20, this.Field21); - type.SerializeField(_l_typeInfo, 21, this.Field22); - type.SerializeField(_l_typeInfo, 22, this.Field23); - type.SerializeField(_l_typeInfo, 23, this.Field24); - type.SerializeField(_l_typeInfo, 24, this.Field25); - type.SerializeField(_l_typeInfo, 25, this.Field26); - type.SerializeField(_l_typeInfo, 26, this.Field27); - type.SerializeField(_l_typeInfo, 27, this.Field28); - type.SerializeField(_l_typeInfo, 28, this.Field29); - type.SerializeField(_l_typeInfo, 29, this.Field30); - type.SerializeField(_l_typeInfo, 30, this.Field31); - type.SerializeField(_l_typeInfo, 31, this.Field32); - type.SerializeField(_l_typeInfo, 32, this.Field33); - type.SerializeField(_l_typeInfo, 33, this.Field34); - type.SerializeField(_l_typeInfo, 34, this.Field35); - type.SerializeField(_l_typeInfo, 35, this.Field36); - type.SerializeField(_l_typeInfo, 36, this.Field37); - type.SerializeField(_l_typeInfo, 37, this.Field38); - type.SerializeField(_l_typeInfo, 38, this.Field39); - type.SerializeField(_l_typeInfo, 39, this.Field40); - type.SerializeField(_l_typeInfo, 40, this.Field41); - type.SerializeField(_l_typeInfo, 41, this.Field42); - type.SerializeField(_l_typeInfo, 42, this.Field43); - type.SerializeField(_l_typeInfo, 43, this.Field44); - type.SerializeField(_l_typeInfo, 44, this.Field45); - type.SerializeField(_l_typeInfo, 45, this.Field46); - type.SerializeField(_l_typeInfo, 46, this.Field47); - type.SerializeField(_l_typeInfo, 47, this.Field48); - type.SerializeField(_l_typeInfo, 48, this.Field49); - type.SerializeField(_l_typeInfo, 49, this.Field50); - type.SerializeField(_l_typeInfo, 50, this.Field51); - type.SerializeField(_l_typeInfo, 51, this.Field52); - type.SerializeField(_l_typeInfo, 52, this.Field53); - type.SerializeField(_l_typeInfo, 53, this.Field54); - type.SerializeField(_l_typeInfo, 54, this.Field55); - type.SerializeField(_l_typeInfo, 55, this.Field56); - type.SerializeField(_l_typeInfo, 56, this.Field57); - type.SerializeField(_l_typeInfo, 57, this.Field58); - type.SerializeField(_l_typeInfo, 58, this.Field59); - type.SerializeField(_l_typeInfo, 59, this.Field60); - type.SerializeField(_l_typeInfo, 60, this.Field61); - type.SerializeField(_l_typeInfo, 61, this.Field62); - type.SerializeField(_l_typeInfo, 62, this.Field63); - type.SerializeField(_l_typeInfo, 63, this.Field64); + type.SerializeField(_l_typeInfo, 0, value.Field1); + type.SerializeField(_l_typeInfo, 1, value.Field2); + type.SerializeField(_l_typeInfo, 2, value.Field3); + type.SerializeField(_l_typeInfo, 3, value.Field4); + type.SerializeField(_l_typeInfo, 4, value.Field5); + type.SerializeField(_l_typeInfo, 5, value.Field6); + type.SerializeField(_l_typeInfo, 6, value.Field7); + type.SerializeField(_l_typeInfo, 7, value.Field8); + type.SerializeField(_l_typeInfo, 8, value.Field9); + type.SerializeField(_l_typeInfo, 9, value.Field10); + type.SerializeField(_l_typeInfo, 10, value.Field11); + type.SerializeField(_l_typeInfo, 11, value.Field12); + type.SerializeField(_l_typeInfo, 12, value.Field13); + type.SerializeField(_l_typeInfo, 13, value.Field14); + type.SerializeField(_l_typeInfo, 14, value.Field15); + type.SerializeField(_l_typeInfo, 15, value.Field16); + type.SerializeField(_l_typeInfo, 16, value.Field17); + type.SerializeField(_l_typeInfo, 17, value.Field18); + type.SerializeField(_l_typeInfo, 18, value.Field19); + type.SerializeField(_l_typeInfo, 19, value.Field20); + type.SerializeField(_l_typeInfo, 20, value.Field21); + type.SerializeField(_l_typeInfo, 21, value.Field22); + type.SerializeField(_l_typeInfo, 22, value.Field23); + type.SerializeField(_l_typeInfo, 23, value.Field24); + type.SerializeField(_l_typeInfo, 24, value.Field25); + type.SerializeField(_l_typeInfo, 25, value.Field26); + type.SerializeField(_l_typeInfo, 26, value.Field27); + type.SerializeField(_l_typeInfo, 27, value.Field28); + type.SerializeField(_l_typeInfo, 28, value.Field29); + type.SerializeField(_l_typeInfo, 29, value.Field30); + type.SerializeField(_l_typeInfo, 30, value.Field31); + type.SerializeField(_l_typeInfo, 31, value.Field32); + type.SerializeField(_l_typeInfo, 32, value.Field33); + type.SerializeField(_l_typeInfo, 33, value.Field34); + type.SerializeField(_l_typeInfo, 34, value.Field35); + type.SerializeField(_l_typeInfo, 35, value.Field36); + type.SerializeField(_l_typeInfo, 36, value.Field37); + type.SerializeField(_l_typeInfo, 37, value.Field38); + type.SerializeField(_l_typeInfo, 38, value.Field39); + type.SerializeField(_l_typeInfo, 39, value.Field40); + type.SerializeField(_l_typeInfo, 40, value.Field41); + type.SerializeField(_l_typeInfo, 41, value.Field42); + type.SerializeField(_l_typeInfo, 42, value.Field43); + type.SerializeField(_l_typeInfo, 43, value.Field44); + type.SerializeField(_l_typeInfo, 44, value.Field45); + type.SerializeField(_l_typeInfo, 45, value.Field46); + type.SerializeField(_l_typeInfo, 46, value.Field47); + type.SerializeField(_l_typeInfo, 47, value.Field48); + type.SerializeField(_l_typeInfo, 48, value.Field49); + type.SerializeField(_l_typeInfo, 49, value.Field50); + type.SerializeField(_l_typeInfo, 50, value.Field51); + type.SerializeField(_l_typeInfo, 51, value.Field52); + type.SerializeField(_l_typeInfo, 52, value.Field53); + type.SerializeField(_l_typeInfo, 53, value.Field54); + type.SerializeField(_l_typeInfo, 54, value.Field55); + type.SerializeField(_l_typeInfo, 55, value.Field56); + type.SerializeField(_l_typeInfo, 56, value.Field57); + type.SerializeField(_l_typeInfo, 57, value.Field58); + type.SerializeField(_l_typeInfo, 58, value.Field59); + type.SerializeField(_l_typeInfo, 59, value.Field60); + type.SerializeField(_l_typeInfo, 60, value.Field61); + type.SerializeField(_l_typeInfo, 61, value.Field62); + type.SerializeField(_l_typeInfo, 62, value.Field63); + type.SerializeField(_l_typeInfo, 63, value.Field64); type.End(); } } diff --git a/test/Serde.Xml.Test/XmlTests.cs b/test/Serde.Xml.Test/XmlTests.cs index 3f9494f4..d780e6b6 100644 --- a/test/Serde.Xml.Test/XmlTests.cs +++ b/test/Serde.Xml.Test/XmlTests.cs @@ -43,7 +43,7 @@ public partial class XmlTests 2 - + blue """; diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs index 0e8e8b24..51b577c6 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.cs @@ -5,7 +5,7 @@ internal static class ColorEnumSerdeTypeInfo { internal static readonly Serde.TypeInfo TypeInfo = Serde.TypeInfo.Create( "ColorEnum", - Serde.TypeInfo.TypeKind.CustomType, + Serde.TypeInfo.TypeKind.Enum, new (string, System.Reflection.MemberInfo)[] { ("red", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Red")!), ("blue", typeof(Serde.Test.AllInOne.ColorEnum).GetField("Blue")!), diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs index f459c92e..ca2fe45d 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.ISerialize.cs @@ -11,14 +11,15 @@ partial struct ColorEnumWrap : Serde.ISerialize { void ISerialize.Serialize(Serde.Test.AllInOne.ColorEnum value, ISerializer serializer) { - var name = value switch + var _l_typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var index = value switch { - Serde.Test.AllInOne.ColorEnum.Red => "red", - Serde.Test.AllInOne.ColorEnum.Blue => "blue", - Serde.Test.AllInOne.ColorEnum.Green => "green", - _ => null + Serde.Test.AllInOne.ColorEnum.Red => 0, + Serde.Test.AllInOne.ColorEnum.Blue => 1, + Serde.Test.AllInOne.ColorEnum.Green => 2, + var v => throw new InvalidOperationException($"Cannot serialize unnamed enum value '{v}' of enum 'ColorEnum'"), }; - serializer.SerializeEnumValue("ColorEnum", name, (int)value, default(Int32Wrap)); + serializer.SerializeEnumValue(_l_typeInfo, index, (int)value, default(Int32Wrap)); } } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs index ad0b22a4..f65b76d3 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ISerialize.cs @@ -11,22 +11,22 @@ partial record AllInOne : Serde.ISerialize { var _l_typeInfo = AllInOneSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.BoolField); - type.SerializeField(_l_typeInfo, 1, this.CharField); - type.SerializeField(_l_typeInfo, 2, this.ByteField); - type.SerializeField(_l_typeInfo, 3, this.UShortField); - type.SerializeField(_l_typeInfo, 4, this.UIntField); - type.SerializeField(_l_typeInfo, 5, this.ULongField); - type.SerializeField(_l_typeInfo, 6, this.SByteField); - type.SerializeField(_l_typeInfo, 7, this.ShortField); - type.SerializeField(_l_typeInfo, 8, this.IntField); - type.SerializeField(_l_typeInfo, 9, this.LongField); - type.SerializeField(_l_typeInfo, 10, this.StringField); - type.SerializeFieldIfNotNull>(_l_typeInfo, 11, this.NullStringField); - type.SerializeField>(_l_typeInfo, 12, this.UIntArr); - type.SerializeField>>(_l_typeInfo, 13, this.NestedArr); - type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, this.IntImm); - type.SerializeField(_l_typeInfo, 15, this.Color); + type.SerializeField(_l_typeInfo, 0, value.BoolField); + type.SerializeField(_l_typeInfo, 1, value.CharField); + type.SerializeField(_l_typeInfo, 2, value.ByteField); + type.SerializeField(_l_typeInfo, 3, value.UShortField); + type.SerializeField(_l_typeInfo, 4, value.UIntField); + type.SerializeField(_l_typeInfo, 5, value.ULongField); + type.SerializeField(_l_typeInfo, 6, value.SByteField); + type.SerializeField(_l_typeInfo, 7, value.ShortField); + type.SerializeField(_l_typeInfo, 8, value.IntField); + type.SerializeField(_l_typeInfo, 9, value.LongField); + type.SerializeField(_l_typeInfo, 10, value.StringField); + type.SerializeFieldIfNotNull>(_l_typeInfo, 11, value.NullStringField); + type.SerializeField>(_l_typeInfo, 12, value.UIntArr); + type.SerializeField>>(_l_typeInfo, 13, value.NestedArr); + type.SerializeField, Serde.ImmutableArrayWrap.SerializeImpl>(_l_typeInfo, 14, value.IntImm); + type.SerializeField(_l_typeInfo, 15, value.Color); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.Address.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.Address.ISerialize.cs index a72fb161..1abd2c58 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.Address.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.Address.ISerialize.cs @@ -13,11 +13,11 @@ partial record Address : Serde.ISerialize { var _l_typeInfo = AddressSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.Name); - type.SerializeField(_l_typeInfo, 1, this.Line1); - type.SerializeFieldIfNotNull>(_l_typeInfo, 2, this.City); - type.SerializeField(_l_typeInfo, 3, this.State); - type.SerializeField(_l_typeInfo, 4, this.Zip); + type.SerializeField(_l_typeInfo, 0, value.Name); + type.SerializeField(_l_typeInfo, 1, value.Line1); + type.SerializeFieldIfNotNull>(_l_typeInfo, 2, value.City); + type.SerializeField(_l_typeInfo, 3, value.State); + type.SerializeField(_l_typeInfo, 4, value.Zip); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.OrderedItem.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.OrderedItem.ISerialize.cs index 97e4ace5..d204d4a6 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.OrderedItem.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.OrderedItem.ISerialize.cs @@ -13,11 +13,11 @@ partial record OrderedItem : Serde.ISerialize { var _l_typeInfo = OrderedItemSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.ItemName); - type.SerializeField(_l_typeInfo, 1, this.Description); - type.SerializeField(_l_typeInfo, 2, this.UnitPrice); - type.SerializeField(_l_typeInfo, 3, this.Quantity); - type.SerializeField(_l_typeInfo, 4, this.LineTotal); + type.SerializeField(_l_typeInfo, 0, value.ItemName); + type.SerializeField(_l_typeInfo, 1, value.Description); + type.SerializeField(_l_typeInfo, 2, value.UnitPrice); + type.SerializeField(_l_typeInfo, 3, value.Quantity); + type.SerializeField(_l_typeInfo, 4, value.LineTotal); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.PurchaseOrder.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.PurchaseOrder.ISerialize.cs index 33e7632f..080eea1e 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.PurchaseOrder.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.SampleTest.PurchaseOrder.ISerialize.cs @@ -13,12 +13,12 @@ partial record PurchaseOrder : Serde.ISerialize>(_l_typeInfo, 0, this.ShipTo); - type.SerializeField(_l_typeInfo, 1, this.OrderDate); - type.SerializeField>>(_l_typeInfo, 2, this.OrderedItems); - type.SerializeField(_l_typeInfo, 3, this.SubTotal); - type.SerializeField(_l_typeInfo, 4, this.ShipCost); - type.SerializeField(_l_typeInfo, 5, this.TotalCost); + type.SerializeField>(_l_typeInfo, 0, value.ShipTo); + type.SerializeField(_l_typeInfo, 1, value.OrderDate); + type.SerializeField>>(_l_typeInfo, 2, value.OrderedItems); + type.SerializeField(_l_typeInfo, 3, value.SubTotal); + type.SerializeField(_l_typeInfo, 4, value.ShipCost); + type.SerializeField(_l_typeInfo, 5, value.TotalCost); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.BoolStruct.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.BoolStruct.ISerialize.cs index d5f802f2..40bbea08 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.BoolStruct.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.BoolStruct.ISerialize.cs @@ -13,7 +13,7 @@ partial struct BoolStruct : Serde.ISerialize { var _l_typeInfo = BoolStructSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField(_l_typeInfo, 0, this.BoolField); + type.SerializeField(_l_typeInfo, 0, value.BoolField); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.MapTest1.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.MapTest1.ISerialize.cs index fff803c4..912e0cba 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.MapTest1.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.MapTest1.ISerialize.cs @@ -13,7 +13,7 @@ partial class MapTest1 : Serde.ISerialize { var _l_typeInfo = MapTest1SerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField, Serde.DictWrap.SerializeImpl>(_l_typeInfo, 0, this.MapField); + type.SerializeField, Serde.DictWrap.SerializeImpl>(_l_typeInfo, 0, value.MapField); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.NestedArrays.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.NestedArrays.ISerialize.cs index da260dd1..4c271b77 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.NestedArrays.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.NestedArrays.ISerialize.cs @@ -13,7 +13,7 @@ partial class NestedArrays : Serde.ISerialize { var _l_typeInfo = NestedArraysSerdeTypeInfo.TypeInfo; var type = serializer.SerializeType(_l_typeInfo); - type.SerializeField>>>(_l_typeInfo, 0, this.A); + type.SerializeField>>>(_l_typeInfo, 0, value.A); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.StructWithIntField.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.StructWithIntField.ISerialize.cs index e47933f6..ba4ba81d 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.StructWithIntField.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.StructWithIntField.ISerialize.cs @@ -13,7 +13,7 @@ partial record StructWithIntField : Serde.ISerialize(_l_typeInfo, 0, this.X); + type.SerializeField(_l_typeInfo, 0, value.X); type.End(); } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.TypeWithArrayField.ISerialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.TypeWithArrayField.ISerialize.cs index c29599dc..17e4353f 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.TypeWithArrayField.ISerialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.XmlTests.TypeWithArrayField.ISerialize.cs @@ -13,7 +13,7 @@ partial class TypeWithArrayField : Serde.ISerialize>>(_l_typeInfo, 0, this.ArrayField); + type.SerializeField>>(_l_typeInfo, 0, value.ArrayField); type.End(); } } From b54e08d6c9e3ee97427b64f9547d676b54e03295 Mon Sep 17 00:00:00 2001 From: Andy Gocke Date: Mon, 1 Jul 2024 23:27:14 -0700 Subject: [PATCH 2/2] Fix enum generation using typeinfo --- src/generator/Generator.Deserialize.cs | 401 +++--------------- ...One.ColorEnumWrap.IDeserialize.verified.cs | 32 +- .../ColorByteWrap.IDeserialize.verified.cs | 32 +- .../ColorIntWrap.IDeserialize.verified.cs | 32 +- .../ColorLongWrap.IDeserialize.verified.cs | 32 +- .../ColorULongWrap.IDeserialize.verified.cs | 32 +- .../ColorEnumWrap.IDeserialize.verified.cs | 32 +- .../ColorEnumWrap.IDeserialize.verified.cs | 32 +- .../Test.ChannelWrap.IDeserialize.verified.cs | 32 +- ...est.AllInOne.ColorEnumWrap.IDeserialize.cs | 32 +- ...est.AllInOne.ColorEnumWrap.IDeserialize.cs | 32 +- 11 files changed, 189 insertions(+), 532 deletions(-) diff --git a/src/generator/Generator.Deserialize.cs b/src/generator/Generator.Deserialize.cs index 09d09763..2e0ab267 100644 --- a/src/generator/Generator.Deserialize.cs +++ b/src/generator/Generator.Deserialize.cs @@ -16,8 +16,6 @@ namespace Serde { internal class DeserializeImplGenerator { - private const string GeneratedVisitorName = "SerdeVisitor"; - internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateDeserializeImpl( GeneratorExecutionContext context, ITypeSymbol receiverType, @@ -35,9 +33,8 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateDeserializeI MemberDeclarationSyntax[] members; if (receiverType.TypeKind == TypeKind.Enum) { - var method = GenerateOldDeserializeMethod(context, interfaceSyntax, receiverType); - var visitorType = GenerateVisitor(receiverType, typeSyntax, context, inProgress); - members = [ method, visitorType ]; + var method = GenerateEnumDeserializeMethod(receiverType, typeSyntax); + members = [ method ]; } else { @@ -48,6 +45,63 @@ internal static (MemberDeclarationSyntax[], BaseListSyntax) GenerateDeserializeI return (members, baseList); } + /// + /// Generates the method body for deserializing an enum. + /// Code looks like: + /// + /// static T IDeserialize<T>Deserialize(IDeserializer deserializer) + /// { + /// var typeInfo = TSerdeTypeInfo.TypeInfo; + /// var de = deserializer.DeserializeType(typeInfo); + /// int index; + /// if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) + /// { + /// throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + /// } + /// return index switch + /// { + /// {index} => {enum member}, + /// ... + /// _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}") + /// }; + /// } + /// + /// + private static MethodDeclarationSyntax GenerateEnumDeserializeMethod( + ITypeSymbol type, + TypeSyntax typeSyntax) + { + Debug.Assert(type.TypeKind == TypeKind.Enum); + + var members = SymbolUtilities.GetDataMembers(type, SerdeUsage.Both); + var typeFqn = typeSyntax.ToString(); + var assignedVarType = members.Count switch { + <= 8 => "byte", + <= 16 => "ushort", + <= 32 => "uint", + <= 64 => "ulong", + _ => throw new InvalidOperationException("Too many members in type") + }; + var src = $$""" +static {{typeFqn}} IDeserialize<{{typeFqn}}>.Deserialize(IDeserializer deserializer) +{ + var typeInfo = {{typeFqn}}SerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) + { + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + return index switch { + {{string.Join("," + Environment.NewLine, members + .Select((m, i) => $"{i} => {typeSyntax}.{m.Name}")) }}, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}") + }; +} +"""; + return (MethodDeclarationSyntax)ParseMemberDeclaration(src)!; + } + /// /// Generates /// @@ -190,347 +244,10 @@ private static MethodDeclarationSyntax GenerateCustomDeserializeMethod( localsBuilder.ToString(), "0b" + Convert.ToString(assignedMaskValue, 2)); } - - } - - // This is the old visitor-driven deserialization method. It is being replaced by the new - // TypeInfo-driven deserialization. - // Generate method `void ISerialize.Deserialize(IDeserializer deserializer) { ... }` - private static MethodDeclarationSyntax GenerateOldDeserializeMethod( - GeneratorExecutionContext context, - QualifiedNameSyntax interfaceSyntax, - ITypeSymbol typeSymbol) - { - var stmts = new List(); - - // var visitor = new 'GeneratedVisitorName'(); - stmts.Add(LocalDeclarationStatement( - VariableDeclaration( - IdentifierName("var"), - SeparatedList(new[] { VariableDeclarator( - Identifier("visitor"), - argumentList: null, - initializer: EqualsValueClause(ObjectCreationExpression( - IdentifierName(GeneratedVisitorName), - ArgumentList(), - initializer: null))) - }) - ))); - - - // Generate statements: - // var visitor = new 'GeneratedVisitorName'(); - - // Three options: - // 1. Built-in type - // 2. Enum type - // 3. Custom type - // - // var fieldNames = new[] { 'field1', 'field2', 'field3' ... }; - // return deserializer.DeserializeType('TypeName', fieldNames, visitor); - - var serdeName = SerdeImplRoslynGenerator.SerdeBuiltInName(typeSymbol.SpecialType); - var typeSyntax = ParseTypeName(typeSymbol.ToString()); - if (serdeName is not null) - { - // Built-in type - stmts.Add(ReturnStatement(InvocationExpression( - QualifiedName( - IdentifierName("deserializer"), - IdentifierName("Deserialize" + serdeName) - ), - ArgumentList(SeparatedList(new[] { - Argument(IdentifierName("visitor")) - })) - ))); - } - else if (typeSymbol.TypeKind == TypeKind.Enum) - { - // 2. Enum type - stmts.Add(ReturnStatement(InvocationExpression( - QualifiedName( - IdentifierName("deserializer"), - IdentifierName("DeserializeString") - ), - ArgumentList(SeparatedList(new[] { - Argument(IdentifierName("visitor")) - })) - ))); - } - else - { - // 3. Custom type - var members = SymbolUtilities.GetDataMembers(typeSymbol, SerdeUsage.Deserialize); - var namesArray = ImplicitArrayCreationExpression(InitializerExpression( - SyntaxKind.ArrayInitializerExpression, - SeparatedList(members.Select(d => (ExpressionSyntax)StringLiteral(d.Name))))); - - // var fieldNames = new[] { 'field1', 'field2', 'field3' ... }; - stmts.Add(LocalDeclarationStatement( - VariableDeclaration( - IdentifierName("var"), - SeparatedList(new[] { VariableDeclarator( - Identifier("fieldNames"), - argumentList: null, - initializer: EqualsValueClause(namesArray)) }) - ) - )); - - // return deserializer.DeserializeType('TypeName', fieldNames, visitor); - stmts.Add(ReturnStatement(InvocationExpression( - QualifiedName( - IdentifierName("deserializer"), - IdentifierName("DeserializeType")), - ArgumentList(SeparatedList(new[] { - Argument(StringLiteral(typeSymbol.Name)), - Argument(IdentifierName("fieldNames")), - Argument(IdentifierName("visitor")) - })) - ))); - } - - return MethodDeclaration( - attributeLists: default, - modifiers: TokenList(Token(SyntaxKind.StaticKeyword)), - typeSyntax, - explicitInterfaceSpecifier: ExplicitInterfaceSpecifier(interfaceSyntax), - identifier: Identifier("Deserialize"), - typeParameterList: null, - parameterList: ParameterList(SeparatedList(new[] { Parameter("IDeserializer", "deserializer", byRef: false) })), - constraintClauses: default, - body: Block(stmts.ToArray()), - expressionBody: null - ); - } - - private static TypeDeclarationSyntax GenerateVisitor( - ITypeSymbol type, - TypeSyntax typeSyntax, - GeneratorExecutionContext context, - ImmutableList inProgress) - { - // Serde.IDeserializeVisitor<'typeName'> - var interfaceSyntax = QualifiedName(IdentifierName("Serde"), GenericName( - Identifier("IDeserializeVisitor"), - TypeArgumentList(SeparatedList(new[] { typeSyntax })) - )); - - var typeName = typeSyntax.ToString(); - var typeMembers = new List(); - // Members: - // public string ExpectedTypeName => 'typeName'; - typeMembers.Add(PropertyDeclaration( - attributeLists: default, - modifiers: new SyntaxTokenList(new[] { Token(SyntaxKind.PublicKeyword) }), - type: PredefinedType(Token(SyntaxKind.StringKeyword)), - explicitInterfaceSpecifier: null, - identifier: Identifier("ExpectedTypeName"), - accessorList: null, - expressionBody: ArrowExpressionClause(StringLiteral(typeName)), - initializer: null, - semicolonToken: Token(SyntaxKind.SemicolonToken))); - - // Three cases - // 1. Built-in type - // 2. Enum type - // 3. Custom type - - var serdeName = SerdeBuiltInName(type.SpecialType); - if (serdeName is not null) - { - var methodText = $"{typeName} IDeserializeVisitor<{typeName}>.Visit{serdeName}({typeName} x) => x;"; - typeMembers.Add(ParseMemberDeclaration(methodText)!); - } - else if (type.TypeKind == TypeKind.Enum) - { - var stringCases = new StringBuilder(); - var spanCases = new StringBuilder(); - foreach (var m in SymbolUtilities.GetDataMembers(type, SerdeUsage.Deserialize)) - { - var formatted = m.GetFormattedName(); - stringCases.Append($$""" - "{{formatted}}" => {{typeName}}.{{m.Name}}, -"""); - spanCases.Append($$""" - _ when System.MemoryExtensions.SequenceEqual(s, "{{formatted}}"u8) => {{typeName}}.{{m.Name}}, -"""); - } - var methodText = $$""" -{{typeName}} Serde.IDeserializeVisitor<{{typeName}}>.VisitString(string s) => s switch - { -{{stringCases}} - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s) - }; -"""; - - var spanMethodText = $$""" -{{typeName}} Serde.IDeserializeVisitor<{{typeName}}>.VisitUtf8Span(System.ReadOnlySpan s) => s switch - { -{{spanCases}} - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s)) - }; -"""; - typeMembers.Add(ParseMemberDeclaration(methodText)!); - typeMembers.Add(ParseMemberDeclaration(spanMethodText)!); - } - else - { - var members = SymbolUtilities.GetDataMembers(type, SerdeUsage.Deserialize); - typeMembers.Add(GenerateFieldNameVisitor(type, typeName, members)); - typeMembers.Add(GenerateCustomTypeVisitor(type, typeName, context, members, inProgress)); - } - - // private sealed class SerdeVisitor : IDeserializeVisitor<'typeName'> - // { - // 'Members' - // } - return ClassDeclaration( - attributeLists: default, - modifiers: new SyntaxTokenList( - Token(SyntaxKind.PrivateKeyword), - Token(SyntaxKind.SealedKeyword)), - Identifier(GeneratedVisitorName), - typeParameterList: null, - baseList: BaseList(SeparatedList(new BaseTypeSyntax[] { SimpleBaseType(interfaceSyntax) })), - constraintClauses: default, - members: List(typeMembers)); - } - - private static MemberDeclarationSyntax GenerateFieldNameVisitor(ITypeSymbol type, string typeName, List members) - { - var text = $$""" -private sealed class FieldNameVisitor : Serde.IDeserialize, Serde.IDeserializeVisitor -{ - public static readonly FieldNameVisitor Instance = new FieldNameVisitor(); - public static byte Deserialize(IDeserializer deserializer) - => deserializer.DeserializeString(Instance); - - public string ExpectedTypeName => "string"; - - byte Serde.IDeserializeVisitor.VisitString(string s) => VisitUtf8Span(System.Text.Encoding.UTF8.GetBytes(s)); - public byte VisitUtf8Span(System.ReadOnlySpan s) - { - switch (s[0]) - { - {{GetSwitchCases()}} - } - } -} -"""; - string GetSwitchCases() - { - var cases = new StringBuilder(); - for (int i = 0; i < members.Count; i++) - { - var m = members[i]; - var formatted = m.GetFormattedName(); - cases.AppendLine($""" - case (byte)'{formatted[0]}' when s.SequenceEqual("{formatted}"u8): - return {i+1}; - - """); - } - string unknownMemberBehavior = SymbolUtilities.GetTypeOptions(type).DenyUnknownMembers - ? $"throw new InvalidDeserializeValueException(\"Unexpected field or property name in type {typeName}: '\" + System.Text.Encoding.UTF8.GetString(s) + \"'\");" - : "return 0;"; - cases.AppendLine($""" - default: - {unknownMemberBehavior} - """); - return cases.ToString(); - } - - return ParseMemberDeclaration(text)!; } private const string AssignedVarName = "_r_assignedValid"; - private static MemberDeclarationSyntax GenerateCustomTypeVisitor( - ITypeSymbol type, - string typeName, - GeneratorExecutionContext context, - List members, - ImmutableList inProgress) - { - var assignedVarType = members.Count switch { - <= 8 => "byte", - <= 16 => "ushort", - <= 32 => "uint", - <= 64 => "ulong", - _ => throw new InvalidOperationException("Too many members in type") - }; - var (cases, locals, assignedMask) = InitCasesAndLocals(); - string typeCreationExpr = GenerateTypeCreation(context, typeName, type, members, assignedMask); - var methodText = $$""" -{{typeName}} Serde.IDeserializeVisitor<{{typeName}}>.VisitDictionary(ref D d) -{ - {{locals}} - {{assignedVarType}} {{AssignedVarName}} = {{assignedMask}}; - while (d.TryGetNextKey(out byte key)) - { - switch (key) - { -{{cases}} - } - } - {{typeCreationExpr}} - return newType; -} -"""; - return ParseMemberDeclaration(methodText)!; - - (string Cases, string Locals, string AssignedMask) InitCasesAndLocals() - { - var casesBuilder = new StringBuilder(); - var localsBuilder = new StringBuilder(); - long assignedMaskValue = 0; - for (int i = 0; i < members.Count; i++) - { - var m = members[i]; - string wrapperName; - var memberType = m.Type.WithNullableAnnotation(m.NullableAnnotation).ToDisplayString(); - if (TryGetExplicitWrapper(m, context, SerdeUsage.Deserialize, inProgress) is { } explicitWrap) - { - wrapperName = explicitWrap.ToString(); - } - else if (ImplementsSerde(m.Type, m.Type, context, SerdeUsage.Deserialize)) - { - wrapperName = memberType; - } - else if (TryGetAnyWrapper(m.Type, context, SerdeUsage.Deserialize, inProgress) is { } wrap) - { - wrapperName = wrap.ToString(); - } - else - { - // No built-in handling and doesn't implement IDeserialize, error - context.ReportDiagnostic(CreateDiagnostic( - DiagId.ERR_DoesntImplementInterface, - m.Locations[0], - m.Symbol, - memberType, - "Serde.IDeserialize")); - wrapperName = memberType; - } - var localName = GetLocalName(m); - localsBuilder.AppendLine($"{memberType} {localName} = default!;"); - casesBuilder.AppendLine($""" - case {i + 1}: - {localName} = d.GetNextValue<{memberType}, {wrapperName}>(); - {AssignedVarName} |= (({assignedVarType})1) << {i}; - break; - """); - if (m.IsNullable && !m.ThrowIfMissing) - { - assignedMaskValue |= 1L << i; - } - } - return (casesBuilder.ToString(), - localsBuilder.ToString(), - "0b" + Convert.ToString(assignedMaskValue, 2)); - } - } - /// /// If the type has a parameterless constructor then we will use that and just set /// each member in the initializer. If there is no parameterlss constructor, there diff --git a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.verified.cs index de37536e..31796ca0 100644 --- a/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/AllInOneTest.GeneratorTest/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.verified.cs @@ -10,28 +10,22 @@ partial record AllInOne { partial struct ColorEnumWrap : Serde.IDeserialize { - static Serde.Test.AllInOne.ColorEnum Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static Serde.Test.AllInOne.ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "Serde.Test.AllInOne.ColorEnum"; - - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => Serde.Test.AllInOne.ColorEnum.Red, - "blue" => Serde.Test.AllInOne.ColorEnum.Blue, - "green" => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => Serde.Test.AllInOne.ColorEnum.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => Serde.Test.AllInOne.ColorEnum.Blue, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => Serde.Test.AllInOne.ColorEnum.Red, + 1 => Serde.Test.AllInOne.ColorEnum.Blue, + 2 => Serde.Test.AllInOne.ColorEnum.Green, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } } diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteWrap.IDeserialize.verified.cs index 9f372f44..02da1fae 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorByteWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorByteWrap : Serde.IDeserialize { - static ColorByte Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorByte IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorByte"; - - ColorByte Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorByteSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => ColorByte.Red, - "green" => ColorByte.Green, - "blue" => ColorByte.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorByte Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => ColorByte.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => ColorByte.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => ColorByte.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorByte.Red, + 1 => ColorByte.Green, + 2 => ColorByte.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntWrap.IDeserialize.verified.cs index 57e3f189..51d05b73 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorIntWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorIntWrap : Serde.IDeserialize { - static ColorInt Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorInt IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorInt"; - - ColorInt Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorIntSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => ColorInt.Red, - "green" => ColorInt.Green, - "blue" => ColorInt.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorInt Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => ColorInt.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => ColorInt.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => ColorInt.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorInt.Red, + 1 => ColorInt.Green, + 2 => ColorInt.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongWrap.IDeserialize.verified.cs index 6d60eaa9..a10477ff 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorLongWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorLongWrap : Serde.IDeserialize { - static ColorLong Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorLong IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorLong"; - - ColorLong Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorLongSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => ColorLong.Red, - "green" => ColorLong.Green, - "blue" => ColorLong.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorLong Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => ColorLong.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => ColorLong.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => ColorLong.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorLong.Red, + 1 => ColorLong.Green, + 2 => ColorLong.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongWrap.IDeserialize.verified.cs index 79880486..700e0942 100644 --- a/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/DeserializeTests.EnumMember/ColorULongWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorULongWrap : Serde.IDeserialize { - static ColorULong Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorULong IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorULong"; - - ColorULong Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorULongSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => ColorULong.Red, - "green" => ColorULong.Green, - "blue" => ColorULong.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorULong Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => ColorULong.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => ColorULong.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => ColorULong.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorULong.Red, + 1 => ColorULong.Green, + 2 => ColorULong.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.IDeserialize.verified.cs index ab5adfba..37709dc2 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumFormat/ColorEnumWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorEnumWrap : Serde.IDeserialize { - static ColorEnum Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorEnum"; - - ColorEnum Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorEnumSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "Red" => ColorEnum.Red, - "Green" => ColorEnum.Green, - "Blue" => ColorEnum.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorEnum Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "Red"u8) => ColorEnum.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "Green"u8) => ColorEnum.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "Blue"u8) => ColorEnum.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorEnum.Red, + 1 => ColorEnum.Green, + 2 => ColorEnum.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.IDeserialize.verified.cs index 7504f2b8..37709dc2 100644 --- a/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/MemberFormatTests.EnumValues/ColorEnumWrap.IDeserialize.verified.cs @@ -6,27 +6,21 @@ partial struct ColorEnumWrap : Serde.IDeserialize { - static ColorEnum Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "ColorEnum"; - - ColorEnum Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = ColorEnumSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => ColorEnum.Red, - "green" => ColorEnum.Green, - "blue" => ColorEnum.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - ColorEnum Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => ColorEnum.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => ColorEnum.Green, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => ColorEnum.Blue, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => ColorEnum.Red, + 1 => ColorEnum.Green, + 2 => ColorEnum.Blue, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } \ No newline at end of file diff --git a/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelWrap.IDeserialize.verified.cs b/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelWrap.IDeserialize.verified.cs index 67e93ee2..e9c57c02 100644 --- a/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelWrap.IDeserialize.verified.cs +++ b/test/Serde.Generation.Test/test_output/WrapperTests.ImmutableArrayEnumDeserialize/Test.ChannelWrap.IDeserialize.verified.cs @@ -8,28 +8,22 @@ namespace Test { partial struct ChannelWrap : Serde.IDeserialize { - static Test.Channel Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static Test.Channel IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "Test.Channel"; - - Test.Channel Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = Test.ChannelSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "a" => Test.Channel.A, - "b" => Test.Channel.B, - "c" => Test.Channel.C, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - Test.Channel Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "a"u8) => Test.Channel.A, - _ when System.MemoryExtensions.SequenceEqual(s, "b"u8) => Test.Channel.B, - _ when System.MemoryExtensions.SequenceEqual(s, "c"u8) => Test.Channel.C, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => Test.Channel.A, + 1 => Test.Channel.B, + 2 => Test.Channel.C, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } } \ No newline at end of file diff --git a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs index d8311e39..6638ae8b 100644 --- a/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs +++ b/test/Serde.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs @@ -9,28 +9,22 @@ partial record AllInOne { partial struct ColorEnumWrap : Serde.IDeserialize { - static Serde.Test.AllInOne.ColorEnum Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static Serde.Test.AllInOne.ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "Serde.Test.AllInOne.ColorEnum"; - - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => Serde.Test.AllInOne.ColorEnum.Red, - "blue" => Serde.Test.AllInOne.ColorEnum.Blue, - "green" => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => Serde.Test.AllInOne.ColorEnum.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => Serde.Test.AllInOne.ColorEnum.Blue, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => Serde.Test.AllInOne.ColorEnum.Red, + 1 => Serde.Test.AllInOne.ColorEnum.Blue, + 2 => Serde.Test.AllInOne.ColorEnum.Green, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } } diff --git a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs index d8311e39..6638ae8b 100644 --- a/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs +++ b/test/Serde.Xml.Test/generated/SerdeGenerator/Serde.SerdeImplRoslynGenerator/Serde.Test.AllInOne.ColorEnumWrap.IDeserialize.cs @@ -9,28 +9,22 @@ partial record AllInOne { partial struct ColorEnumWrap : Serde.IDeserialize { - static Serde.Test.AllInOne.ColorEnum Serde.IDeserialize.Deserialize(IDeserializer deserializer) + static Serde.Test.AllInOne.ColorEnum IDeserialize.Deserialize(IDeserializer deserializer) { - var visitor = new SerdeVisitor(); - return deserializer.DeserializeString(visitor); - } - - private sealed class SerdeVisitor : Serde.IDeserializeVisitor - { - public string ExpectedTypeName => "Serde.Test.AllInOne.ColorEnum"; - - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitString(string s) => s switch + var typeInfo = Serde.Test.AllInOne.ColorEnumSerdeTypeInfo.TypeInfo; + var de = deserializer.DeserializeType(typeInfo); + int index; + if ((index = de.TryReadIndex(typeInfo, out var errorName)) == IDeserializeType.IndexNotFound) { - "red" => Serde.Test.AllInOne.ColorEnum.Red, - "blue" => Serde.Test.AllInOne.ColorEnum.Blue, - "green" => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + s)}; - Serde.Test.AllInOne.ColorEnum Serde.IDeserializeVisitor.VisitUtf8Span(System.ReadOnlySpan s) => s switch + throw new InvalidDeserializeValueException($"Unexpected value: {errorName}"); + } + + return index switch { - _ when System.MemoryExtensions.SequenceEqual(s, "red"u8) => Serde.Test.AllInOne.ColorEnum.Red, - _ when System.MemoryExtensions.SequenceEqual(s, "blue"u8) => Serde.Test.AllInOne.ColorEnum.Blue, - _ when System.MemoryExtensions.SequenceEqual(s, "green"u8) => Serde.Test.AllInOne.ColorEnum.Green, - _ => throw new InvalidDeserializeValueException("Unexpected enum field name: " + System.Text.Encoding.UTF8.GetString(s))}; + 0 => Serde.Test.AllInOne.ColorEnum.Red, + 1 => Serde.Test.AllInOne.ColorEnum.Blue, + 2 => Serde.Test.AllInOne.ColorEnum.Green, + _ => throw new InvalidDeserializeValueException($"Unexpected index: {index}")}; } } }