From 321d3bff73eaf8f733ee55b02be96cbe395c22ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franti=C5=A1ek=20Boh=C3=A1=C4=8Dek?= Date: Sun, 2 Jan 2022 19:49:55 +0100 Subject: [PATCH] feat: accept types and variable names in inline converter generators instead of packet parameters --- .../Extensions/TypeSymbolExtensions.cs | 21 ++++++++++++ .../Extensions/TypeSyntaxExtensions.cs | 17 +++++++++- .../BasicInlineConverterGenerator.cs | 27 +++++++++------ .../BoolInlineConverterGenerator.cs | 28 +++++----------- .../EnumInlineConverterGenerator.cs | 33 +++++++++++-------- .../FallbackInlineConverterGenerator.cs | 28 +++++++++++----- .../IInlineConverterGenerator.cs | 20 +++++++---- .../StringInlineConverterGenerator.cs | 24 ++++---------- .../InlineTypeConverterGenerator.cs | 15 +++++---- 9 files changed, 131 insertions(+), 82 deletions(-) diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs index f05c7a1..8d264cd 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSymbolExtensions.cs @@ -5,6 +5,7 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; namespace NosSmooth.PacketSerializersGenerator.Extensions; @@ -30,7 +31,27 @@ public static class TypeSymbolExtensions return false; } + if (typeSymbol.ToString().EndsWith("?")) + { + return true; + } + // cannot determine if not nullable from reference type. return null; } + + /// + /// Gets the type name with ? if it is nullable. + /// + /// The type. + /// The actual name. + public static string GetActualType(this ITypeSymbol typeSymbol) + { + if (typeSymbol.IsNullable() ?? false) + { + return typeSymbol.ToString().TrimEnd('?') + '?'; + } + + return typeSymbol.ToString(); + } } \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs index d95bbbc..bceed49 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/Extensions/TypeSyntaxExtensions.cs @@ -20,6 +20,21 @@ public static class TypeSyntaxExtensions /// Whether the type syntax is nullable. public static bool IsNullable(this TypeSyntax typeSyntax) { - return typeSyntax is NullableTypeSyntax; + return typeSyntax is NullableTypeSyntax || typeSyntax.ToString().EndsWith("?"); + } + + /// + /// Gets the type name with ? if it is nullable. + /// + /// The type. + /// The actual name. + public static string GetActualType(this TypeSyntax typeSyntax) + { + if (typeSyntax.IsNullable()) + { + return typeSyntax.ToString().TrimEnd('?') + '?'; + } + + return typeSyntax.ToString(); } } \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs index 82bb0e0..51ea414 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BasicInlineConverterGenerator.cs @@ -5,6 +5,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; using NosSmooth.PacketSerializersGenerator.Extensions; @@ -23,16 +25,15 @@ public class BasicInlineConverterGenerator : IInlineConverterGenerator public static IReadOnlyList HandleTypes => new[] { "long", "ulong", "int", "uint", "short", "ushort", "byte", "sbyte" }; /// - public bool ShouldHandle(ParameterInfo parameter) - => HandleTypes.Contains(parameter.Parameter.Type!.ToString()); + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) + => HandleTypes.Contains(typeSyntax?.ToString().TrimEnd('?')) || HandleTypes.Contains(typeSymbol?.ToString().TrimEnd('?')); /// - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet) + public IError? GenerateSerializerPart(IndentedTextWriter textWriter, string variableName, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - if (parameter.Nullable) + if ((typeSyntax is not null && typeSyntax.IsNullable()) || (typeSymbol is not null && (typeSymbol.IsNullable() ?? false))) { - textWriter.WriteLine("if (obj is null)"); + textWriter.WriteLine($"if ({variableName} is null)"); textWriter.WriteLine("{"); textWriter.WriteLine("builder.Append('-');"); textWriter.WriteLine("}"); @@ -40,16 +41,22 @@ public class BasicInlineConverterGenerator : IInlineConverterGenerator } textWriter.WriteLine("{"); textWriter.WriteLine - ($"builder.Append(obj.{parameter.Name});"); + ($"builder.Append(({(typeSymbol?.ToString() ?? typeSyntax!.ToString()).TrimEnd('?')}){variableName});"); textWriter.WriteLine("}"); return null; } /// - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - var type = parameter.Parameter.Type!.ToString().Trim('?'); + var type = typeSyntax is not null + ? typeSyntax.ToString().TrimEnd('?') + : typeSymbol?.ToString(); + if (type is null) + { + throw new Exception("TypeSyntax or TypeSymbol has to be non null."); + } + textWriter.WriteLine($"{Constants.HelperClass}.ParseBasic{type}(this, stringEnumerator);"); return null; } diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs index a04bba4..e73f654 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/BoolInlineConverterGenerator.cs @@ -5,6 +5,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; using NosSmooth.PacketSerializersGenerator.Extensions; @@ -15,16 +17,15 @@ namespace NosSmooth.PacketSerializersGenerator.InlineConverterGenerators; public class BoolInlineConverterGenerator : IInlineConverterGenerator { /// - public bool ShouldHandle(ParameterInfo parameter) - => parameter.Parameter.Type!.ToString() == "bool"; + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) + => typeSyntax?.ToString().TrimEnd('?') == "bool" || typeSymbol?.ToString().TrimEnd('?') == "bool"; /// - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet) + public IError? GenerateSerializerPart(IndentedTextWriter textWriter, string variableName, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - if (parameter.Nullable) + if ((typeSyntax?.IsNullable() ?? false) || (typeSymbol?.IsNullable() ?? false)) { - textWriter.WriteLine($"if (obj.{parameter.Name} is null)"); + textWriter.WriteLine($"if ({variableName} is null)"); textWriter.WriteLine("{"); textWriter.Indent++; textWriter.WriteLine("builder.Append('-');"); @@ -34,27 +35,16 @@ public class BoolInlineConverterGenerator : IInlineConverterGenerator } textWriter.WriteLine("{"); textWriter.Indent++; - textWriter.WriteLine($"builder.Append(obj.{parameter.Name} ? '1' : '0');"); + textWriter.WriteLine($"builder.Append({variableName} ? '1' : '0');"); textWriter.Indent--; textWriter.WriteLine("}"); return null; } /// - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; textWriter.WriteLine($"{Constants.HelperClass}.ParseBool(stringEnumerator);"); - /*string isLastString = packet.Parameters.IsLast ? "true" : "false"; - textWriter.WriteMultiline($@" -var {parameter.GetResultVariableName()} = stringEnumerator.GetNextToken(); -var {parameter.GetErrorVariableName()} = CheckDeserializationResult({parameter.GetResultVariableName()}, ""{parameter.Name}"", stringEnumerator, {isLastString}); -if ({parameter.GetErrorVariableName()} is not null) -{{ - return Result<{packet.Name}?>.FromError({parameter.GetErrorVariableName()}, {parameter.GetResultVariableName()}); -}} -{parameter.GetNullableType()} {parameter.GetNullableVariableName()} = {parameter.GetResultVariableName()}.Entity.Token == ""1"" ? true : ({parameter.GetResultVariableName()}.Entity.Token == ""-"" ? null : false); -");*/ return null; } diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs index e7de905..421e678 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/EnumInlineConverterGenerator.cs @@ -6,6 +6,7 @@ using System.CodeDom.Compiler; using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; using NosSmooth.PacketSerializersGenerator.Extensions; @@ -26,15 +27,20 @@ public class EnumInlineConverterGenerator : IInlineConverterGenerator } /// - public bool ShouldHandle(ParameterInfo parameter) - => parameter.Type.TypeKind == TypeKind.Enum; + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) + => typeSymbol?.TypeKind == TypeKind.Enum; /// - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet) + public IError? GenerateSerializerPart + ( + IndentedTextWriter textWriter, + string variableName, + TypeSyntax? typeSyntax, + ITypeSymbol? typeSymbol + ) { - var parameter = packet.Parameters.Current; - var underlyingType = ((INamedTypeSymbol)parameter.Type).EnumUnderlyingType!.ToString(); - if (parameter.Nullable) + var underlyingType = ((INamedTypeSymbol)typeSymbol!).EnumUnderlyingType!.ToString(); + if ((typeSyntax?.IsNullable() ?? false) || (typeSymbol?.IsNullable() ?? false)) { textWriter.WriteLine("if (obj is null)"); textWriter.WriteLine("{"); @@ -44,23 +50,24 @@ public class EnumInlineConverterGenerator : IInlineConverterGenerator } textWriter.WriteLine("{"); textWriter.WriteLine - ($"builder.Append(({underlyingType})obj.{parameter.Name});"); + ($"builder.Append(({underlyingType}){variableName});"); textWriter.WriteLine("}"); return null; } /// - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - if (_enumTypes.All(x => x.ToString() != parameter.Type.ToString())) + if (_enumTypes.All(x => x.ToString() != typeSymbol!.ToString())) { - _enumTypes.Add(parameter.Type); + _enumTypes.Add(typeSymbol!); } textWriter.WriteLine - ($"{Constants.HelperClass}.ParseEnum{parameter.GetActualType().Replace('.', '_')}(this, stringEnumerator);"); + ( + $"{Constants.HelperClass}.ParseEnum{typeSymbol?.ToString().TrimEnd('?').Replace('.', '_')}(this, stringEnumerator);" + ); return null; } @@ -98,4 +105,4 @@ public static Result<{type}?> ParseEnum{type.ToString().Replace('.', '_')}(IType ); } } -} +} \ No newline at end of file diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/FallbackInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/FallbackInlineConverterGenerator.cs index 331442f..694b827 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/FallbackInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/FallbackInlineConverterGenerator.cs @@ -5,9 +5,9 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; -using NosSmooth.PacketSerializersGenerator.Data; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Errors; -using NosSmooth.PacketSerializersGenerator.Extensions; namespace NosSmooth.PacketSerializersGenerator.InlineConverterGenerators; @@ -15,24 +15,34 @@ namespace NosSmooth.PacketSerializersGenerator.InlineConverterGenerators; public class FallbackInlineConverterGenerator : IInlineConverterGenerator { /// - public bool ShouldHandle(ParameterInfo parameter) + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { return true; } /// - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet) + public IError? GenerateSerializerPart + ( + IndentedTextWriter textWriter, + string variableName, + TypeSyntax? typeSyntax, + ITypeSymbol? typeSymbol + ) { - var parameter = packet.Parameters.Current; - textWriter.WriteLine($"_typeConverterRepository.Serialize<{parameter.GetActualType()}>(obj.{parameter.Name}, builder);"); + textWriter.WriteLine + ( + $"_typeConverterRepository.Serialize<{(typeSyntax?.ToString() ?? typeSymbol!.ToString()).TrimEnd('?')}?>({variableName}, builder);" + ); return null; } /// - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - textWriter.WriteLine($"_typeConverterRepository.Deserialize<{parameter.GetNullableType()}>(stringEnumerator);"); + textWriter.WriteLine + ( + $"_typeConverterRepository.Deserialize<{(typeSyntax?.ToString() ?? typeSymbol!.ToString()).TrimEnd('?')}?>(stringEnumerator);" + ); return null; } diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/IInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/IInlineConverterGenerator.cs index 212f848..f331dbd 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/IInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/IInlineConverterGenerator.cs @@ -5,6 +5,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; @@ -18,25 +20,29 @@ public interface IInlineConverterGenerator /// /// Whether the given parameter should be handled by this. /// - /// The parameter. - /// Whethet to handle. - public bool ShouldHandle(ParameterInfo parameter); + /// The type syntax. + /// The type symbol. + /// Whether to handle. + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol); /// /// Generate the serializer part. /// /// The text writer to write to. - /// The packet. + /// The name of the variable. + /// The type syntax. + /// The type symbol. /// An error, if any. - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet); + public IError? GenerateSerializerPart(IndentedTextWriter textWriter, string variableName, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol); /// /// Generate the deserializer part. /// /// The text writer to write to. - /// The packet. + /// The type syntax. + /// The type symbol. /// An error, if any. - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet); + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol); /// /// Generate helper methods to HelperClass. diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/StringInlineConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/StringInlineConverterGenerator.cs index c8d8b3a..546c96d 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/StringInlineConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineConverterGenerators/StringInlineConverterGenerator.cs @@ -5,6 +5,8 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System.CodeDom.Compiler; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp.Syntax; using NosSmooth.PacketSerializersGenerator.Data; using NosSmooth.PacketSerializersGenerator.Errors; using NosSmooth.PacketSerializersGenerator.Extensions; @@ -15,32 +17,20 @@ namespace NosSmooth.PacketSerializersGenerator.InlineConverterGenerators; public class StringInlineConverterGenerator : IInlineConverterGenerator { /// - public bool ShouldHandle(ParameterInfo parameter) - => parameter.Parameter.Type!.ToString() == "string"; + public bool ShouldHandle(TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) + => typeSyntax?.ToString().TrimEnd('?') == "string" || typeSymbol?.ToString().TrimEnd('?') == "string"; /// - public IError? GenerateSerializerPart(IndentedTextWriter textWriter, PacketInfo packet) + public IError? GenerateSerializerPart(IndentedTextWriter textWriter, string variableName, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - var parameter = packet.Parameters.Current; - textWriter.WriteLine($"builder.Append(obj.{parameter.Name} ?? \"-\");"); + textWriter.WriteLine($"builder.Append({variableName} ?? \"-\");"); return null; } /// - public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) + public IError? CallDeserialize(IndentedTextWriter textWriter, TypeSyntax? typeSyntax, ITypeSymbol? typeSymbol) { - // var parameter = packet.Parameters.Current; - // string isLastString = packet.Parameters.IsLast ? "true" : "false"; textWriter.WriteLine($"{Constants.HelperClass}.ParseString(stringEnumerator);"); - /*textWriter.WriteMultiline($@" -var {parameter.GetResultVariableName()} = stringEnumerator.GetNextToken(); -var {parameter.GetErrorVariableName()} = CheckDeserializationResult({parameter.GetResultVariableName()}, ""{parameter.Name}"", stringEnumerator, {isLastString}); -if ({parameter.GetErrorVariableName()} is not null) -{{ - return Result<{packet.Name}?>.FromError({parameter.GetErrorVariableName()}, {parameter.GetResultVariableName()}); -}} -var {parameter.GetNullableVariableName()} = {parameter.GetResultVariableName()}.Entity.Token == ""-"" ? null : {parameter.GetResultVariableName()}.Entity.Token; -");*/ return null; } diff --git a/Core/NosSmooth.PacketSerializersGenerator/InlineTypeConverterGenerator.cs b/Core/NosSmooth.PacketSerializersGenerator/InlineTypeConverterGenerator.cs index 193ab17..2c8e45c 100644 --- a/Core/NosSmooth.PacketSerializersGenerator/InlineTypeConverterGenerator.cs +++ b/Core/NosSmooth.PacketSerializersGenerator/InlineTypeConverterGenerator.cs @@ -39,19 +39,20 @@ public class InlineTypeConverterGenerator /// An error, if any. public IError? CallDeserialize(IndentedTextWriter textWriter, PacketInfo packet) { + var parameter = packet.Parameters.Current; var shouldGenerateInline = packet.GenerateAttribute.GetIndexedValue(0); if (shouldGenerateInline) { foreach (var generator in _typeGenerators) { - if (generator.ShouldHandle(packet.Parameters.Current)) + if (generator.ShouldHandle(parameter.Parameter.Type, parameter.Type)) { - return generator.CallDeserialize(textWriter, packet); + return generator.CallDeserialize(textWriter, parameter.Parameter.Type, parameter.Type); } } } - return _fallbackInlineConverterGenerator.CallDeserialize(textWriter, packet); + return _fallbackInlineConverterGenerator.CallDeserialize(textWriter, parameter.Parameter.Type, parameter.Type); } /// @@ -62,18 +63,20 @@ public class InlineTypeConverterGenerator /// An error, if any. public IError? Serialize(IndentedTextWriter textWriter, PacketInfo packet) { + var parameter = packet.Parameters.Current; + var variableName = "obj." + parameter.Name; var shouldGenerateInline = packet.GenerateAttribute.GetIndexedValue(0); if (shouldGenerateInline) { foreach (var generator in _typeGenerators) { - if (generator.ShouldHandle(packet.Parameters.Current)) + if (generator.ShouldHandle(parameter.Parameter.Type, parameter.Type)) { - return generator.GenerateSerializerPart(textWriter, packet); + return generator.GenerateSerializerPart(textWriter, variableName, parameter.Parameter.Type, parameter.Type); } } } - return _fallbackInlineConverterGenerator.GenerateSerializerPart(textWriter, packet); + return _fallbackInlineConverterGenerator.GenerateSerializerPart(textWriter, variableName, parameter.Parameter.Type, parameter.Type); } } \ No newline at end of file -- 2.49.0