fix(codegen): Removes extra rule arg for primitive serialization (#611)

This commit is contained in:
Kamron Batman 2021-05-23 23:47:12 -07:00 committed by GitHub
parent 82b0d03e19
commit 99c46f1b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 59 deletions

View file

@ -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
);

View file

@ -35,18 +35,13 @@ namespace SerializationGenerator
{
if (symbol.IsIpAddress(compilation))
{
ruleArguments = new[] { "IPAddress" };
ruleArguments = Array.Empty<string>();
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<string>();
}
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<SpecialType>(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<SpecialType>(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});");
}