From 99c46f1b116e2455390dba61fa487e64c83c355f Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 23 May 2021 23:47:12 -0700 Subject: [PATCH] fix(codegen): Removes extra rule arg for primitive serialization (#611) --- ...ializableEntityGeneration.MetadataTypes.cs | 4 +- .../Rules/PrimitiveTypeMigrationRule.cs | 92 +++++++------------ 2 files changed, 37 insertions(+), 59 deletions(-) diff --git a/Projects/SerializationGenerator/SerializableEntityGeneration/SerializableEntityGeneration.MetadataTypes.cs b/Projects/SerializationGenerator/SerializableEntityGeneration/SerializableEntityGeneration.MetadataTypes.cs index 840cd14de..e992f1fa2 100644 --- a/Projects/SerializationGenerator/SerializableEntityGeneration/SerializableEntityGeneration.MetadataTypes.cs +++ b/Projects/SerializationGenerator/SerializableEntityGeneration/SerializableEntityGeneration.MetadataTypes.cs @@ -23,7 +23,7 @@ namespace SerializationGenerator { public const string LIST_CLASS = "System.Collections.Generic.List`1"; public const string HASHSET_CLASS = "System.Collections.Generic.HashSet`1"; - public const string IP_CLASS = "System.Net.IPAddress"; + public const string IPADDRESS_CLASS = "System.Net.IPAddress"; public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair"; public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute"; @@ -124,7 +124,7 @@ namespace SerializationGenerator public static bool IsIpAddress(this ISymbol symbol, Compilation compilation) => symbol.Equals( - compilation.GetTypeByMetadataName(IP_CLASS), + compilation.GetTypeByMetadataName(IPADDRESS_CLASS), SymbolEqualityComparer.Default ); diff --git a/Projects/SerializationGenerator/SerializableMigration/Rules/PrimitiveTypeMigrationRule.cs b/Projects/SerializationGenerator/SerializableMigration/Rules/PrimitiveTypeMigrationRule.cs index 37c5042be..c4bb15cd1 100644 --- a/Projects/SerializationGenerator/SerializableMigration/Rules/PrimitiveTypeMigrationRule.cs +++ b/Projects/SerializationGenerator/SerializableMigration/Rules/PrimitiveTypeMigrationRule.cs @@ -35,18 +35,13 @@ namespace SerializationGenerator { if (symbol.IsIpAddress(compilation)) { - ruleArguments = new[] { "IPAddress" }; + ruleArguments = Array.Empty(); return true; } - if (symbol is not ITypeSymbol typeSymbol) - { - ruleArguments = null; - return false; - } - if ( - typeSymbol.SpecialType is + symbol is not ITypeSymbol typeSymbol || + typeSymbol.SpecialType is not SpecialType.System_Boolean or SpecialType.System_SByte or SpecialType.System_Int16 or @@ -63,21 +58,20 @@ namespace SerializationGenerator SpecialType.System_DateTime ) { - ruleArguments = new[] { typeSymbol.SpecialType.ToString() }; - return true; + ruleArguments = null; + return false; } - if (typeSymbol.SpecialType == SpecialType.System_DateTime) + if (typeSymbol.SpecialType == SpecialType.System_DateTime && attributes.Any(a => a.IsDeltaDateTime(compilation))) { - ruleArguments = attributes.Any(a => a.IsDeltaDateTime(compilation)) - ? new[] { typeSymbol.SpecialType.ToString(), "DeltaTime" } - : new[] { typeSymbol.SpecialType.ToString() }; - - return true; + ruleArguments = new[] { "DeltaTime" }; + } + else + { + ruleArguments = Array.Empty(); } - ruleArguments = null; - return false; + return true; } public void GenerateDeserializationMethod(StringBuilder source, string indent, SerializableProperty property) @@ -90,41 +84,31 @@ namespace SerializationGenerator } var propertyName = property.Name; - var ruleType = property.RuleArguments[0]; string readMethod; - if (ruleType == "IPAddress") - { - readMethod = "ReadIPAddress"; - } - else - { - if (!Enum.TryParse(ruleType, out var specialType)) - { - throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})"); - } + const string ipAddress = SerializableEntityGeneration.IPADDRESS_CLASS; - readMethod = specialType switch - { - SpecialType.System_Boolean => "ReadBool", - SpecialType.System_SByte => "ReadSByte", - SpecialType.System_Int16 => "ReadShort", - SpecialType.System_Int32 => "ReadInt", - SpecialType.System_Int64 => "ReadLong", - SpecialType.System_Byte => "ReadByte", - SpecialType.System_UInt16 => "ReadUShort", - SpecialType.System_UInt32 => "ReadUInt", - SpecialType.System_UInt64 => "ReadULong", - SpecialType.System_Single => "ReadFloat", - SpecialType.System_Double => "ReadDouble", - SpecialType.System_String => "ReadString", - SpecialType.System_Decimal => "ReadDecimal", - SpecialType.System_DateTime => property.RuleArguments.Length >= 2 && - property.RuleArguments[1] == "DeltaTime" ? - "ReadDeltaTime" : - "ReadDateTime" - }; - } + readMethod = property.Type switch + { + "bool" => "ReadBool", + "sbyte" => "ReadSByte", + "short" => "ReadShort", + "int" => "ReadInt", + "long" => "ReadLong", + "byte" => "ReadByte", + "ushort" => "ReadUShort", + "uint" => "ReadUInt", + "ulong" => "ReadULong", + "float" => "ReadFloat", + "double" => "ReadDouble", + "string" => "ReadString", + "decimal" => "ReadDecimal", + ipAddress => "ReadIPAddress", + "System.DateTime" => property.RuleArguments.Length >= 1 && + property.RuleArguments[0] == "DeltaTime" ? + "ReadDeltaTime" : + "ReadDateTime" + }; source.AppendLine($"{indent}{propertyName} = reader.{readMethod}();"); } @@ -139,14 +123,8 @@ namespace SerializationGenerator } var propertyName = property.Name; - var ruleType = property.RuleArguments[0]; - if (!Enum.TryParse(ruleType, out var specialType)) - { - throw new ArgumentException($"Invalid rule state for property {propertyName} ({ruleType})"); - } - - if (specialType == SpecialType.System_DateTime && property.RuleArguments[1] == "DeltaTime") + if (property.Type == "System.DateTime" && property.RuleArguments.Length >= 1 && property.RuleArguments[0] == "DeltaTime") { source.AppendLine($"{indent}writer.WriteDeltaTime({propertyName});"); }