Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPC: nullable enable and update language version to 9. #1125

Merged
merged 6 commits into from Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
156 changes: 106 additions & 50 deletions src/MessagePack.GeneratorCore/CodeAnalysis/Definitions.cs
Expand Up @@ -9,48 +9,48 @@

namespace MessagePackCompiler.CodeAnalysis
{
public interface INameSpaceInfo
pCYSl5EDgo marked this conversation as resolved.
Show resolved Hide resolved
{
string? Namespace { get; }
}

public interface IResolverRegisterInfo
{
string FullName { get; }

string FormatterName { get; }
}

public class ObjectSerializationInfo : IResolverRegisterInfo
public class ObjectSerializationInfo : IResolverRegisterInfo, INameSpaceInfo
{
public string Name { get; set; }
public string Name { get; }

public string FullName { get; set; }
public string FullName { get; }

public string Namespace { get; set; }
public string? Namespace { get; }

public GenericTypeParameterInfo[] GenericTypeParameters { get; set; }
public GenericTypeParameterInfo[] GenericTypeParameters { get; }

public bool IsOpenGenericType { get; set; }
public bool IsOpenGenericType { get; }

public bool IsIntKey { get; set; }
public bool IsIntKey { get; }

public bool IsStringKey
{
get { return !this.IsIntKey; }
}

public bool IsClass { get; set; }

public bool IsStruct
{
get { return !this.IsClass; }
}
public bool IsClass { get; }

public MemberSerializationInfo[] ConstructorParameters { get; set; }
public MemberSerializationInfo[] ConstructorParameters { get; }

public MemberSerializationInfo[] Members { get; set; }
public MemberSerializationInfo[] Members { get; }

public bool HasIMessagePackSerializationCallbackReceiver { get; set; }
public bool HasIMessagePackSerializationCallbackReceiver { get; }

public bool NeedsCastOnBefore { get; set; }
public bool NeedsCastOnBefore { get; }

public bool NeedsCastOnAfter { get; set; }
public bool NeedsCastOnAfter { get; }

public string FormatterName => this.Namespace == null ? FormatterNameWithoutNameSpace : this.Namespace + "." + FormatterNameWithoutNameSpace;

Expand Down Expand Up @@ -79,7 +79,7 @@ public int MaxKey
}
}

public MemberSerializationInfo GetMember(int index)
public MemberSerializationInfo? GetMember(int index)
{
return this.Members.FirstOrDefault(x => x.IntKey == index);
}
Expand All @@ -89,6 +89,22 @@ public string GetConstructorString()
var args = string.Join(", ", this.ConstructorParameters.Select(x => "__" + x.Name + "__"));
return $"{this.FullName}({args})";
}

public ObjectSerializationInfo(bool isClass, bool isOpenGenericType, GenericTypeParameterInfo[] genericTypeParameterInfos, MemberSerializationInfo[] constructorParameters, bool isIntKey, MemberSerializationInfo[] members, string name, string fullName, string? @namespace, bool hasSerializationConstructor, bool needsCastOnAfter, bool needsCastOnBefore)
{
IsClass = isClass;
IsOpenGenericType = isOpenGenericType;
GenericTypeParameters = genericTypeParameterInfos;
ConstructorParameters = constructorParameters;
IsIntKey = isIntKey;
Members = members;
Name = name;
FullName = fullName;
Namespace = @namespace;
HasIMessagePackSerializationCallbackReceiver = hasSerializationConstructor;
NeedsCastOnAfter = needsCastOnAfter;
NeedsCastOnBefore = needsCastOnBefore;
}
}

public class GenericTypeParameterInfo
Expand All @@ -103,33 +119,44 @@ public GenericTypeParameterInfo(string name, string constraints)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
Constraints = constraints ?? throw new ArgumentNullException(nameof(name));
HasConstraints = !string.IsNullOrEmpty(constraints);
HasConstraints = constraints != string.Empty;
}
}

public class MemberSerializationInfo
{
public bool IsProperty { get; set; }
public bool IsProperty { get; }

public bool IsField { get; set; }
public bool IsWritable { get; }

public bool IsWritable { get; set; }
public bool IsReadable { get; }

public bool IsReadable { get; set; }
public int IntKey { get; }

public int IntKey { get; set; }
public string StringKey { get; }

public string StringKey { get; set; }
public string Type { get; }

public string Type { get; set; }
public string Name { get; }

public string Name { get; set; }
public string ShortTypeName { get; }

public string ShortTypeName { get; set; }
public string? CustomFormatterTypeName { get; }

public string CustomFormatterTypeName { get; set; }
private readonly HashSet<string> primitiveTypes = new (Generator.ShouldUseFormatterResolverHelper.PrimitiveTypes);

private readonly HashSet<string> primitiveTypes = new HashSet<string>(Generator.ShouldUseFormatterResolverHelper.PrimitiveTypes);
public MemberSerializationInfo(bool isProperty, bool isWritable, bool isReadable, int intKey, string stringKey, string name, string type, string shortTypeName, string? customFormatterTypeName)
{
IsProperty = isProperty;
IsWritable = isWritable;
IsReadable = isReadable;
IntKey = intKey;
StringKey = stringKey;
Type = type;
Name = name;
ShortTypeName = shortTypeName;
CustomFormatterTypeName = customFormatterTypeName;
}

public string GetSerializeMethodString()
{
Expand All @@ -139,7 +166,7 @@ public string GetSerializeMethodString()
}
else if (this.primitiveTypes.Contains(this.Type))
{
return $"writer.Write(value.{this.Name})";
return "writer.Write(value." + this.Name + ")";
}
else
{
Expand All @@ -156,7 +183,7 @@ public string GetDeserializeMethodString()
else if (this.primitiveTypes.Contains(this.Type))
{
string suffix = this.Type == "byte[]" ? "?.ToArray()" : string.Empty;
return $"reader.Read{this.ShortTypeName.Replace("[]", "s")}()" + suffix;
return $"reader.Read{this.ShortTypeName!.Replace("[]", "s")}()" + suffix;
}
else
{
Expand All @@ -165,55 +192,84 @@ public string GetDeserializeMethodString()
}
}

public class EnumSerializationInfo : IResolverRegisterInfo
public class EnumSerializationInfo : IResolverRegisterInfo, INameSpaceInfo
{
public string Namespace { get; set; }
public EnumSerializationInfo(string? @namespace, string name, string fullName, string underlyingType)
{
Namespace = @namespace;
Name = name;
FullName = fullName;
UnderlyingType = underlyingType;
}

public string? Namespace { get; }

public string Name { get; set; }
public string Name { get; }

public string FullName { get; set; }
public string FullName { get; }

public string UnderlyingType { get; set; }
public string UnderlyingType { get; }

public string FormatterName => (this.Namespace == null ? this.Name : this.Namespace + "." + this.Name) + "Formatter";
}

public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable<GenericSerializationInfo>
{
public string FullName { get; set; }
public string FullName { get; }

public string FormatterName { get; set; }
public string FormatterName { get; }

public bool IsOpenGenericType { get; set; }
public bool IsOpenGenericType { get; }

public bool Equals(GenericSerializationInfo other)
public bool Equals(GenericSerializationInfo? other)
{
return this.FullName.Equals(other.FullName);
return this.FullName.Equals(other?.FullName);
}

public override int GetHashCode()
{
return this.FullName.GetHashCode();
}

public GenericSerializationInfo(string fullName, string formatterName, bool isOpenGenericType)
{
FullName = fullName;
FormatterName = formatterName;
IsOpenGenericType = isOpenGenericType;
}
}

public class UnionSerializationInfo : IResolverRegisterInfo
public class UnionSerializationInfo : IResolverRegisterInfo, INameSpaceInfo
{
public string Namespace { get; set; }
public string? Namespace { get; }

public string Name { get; set; }
public string Name { get; }

public string FullName { get; set; }
public string FullName { get; }

public string FormatterName => (this.Namespace == null ? this.Name : this.Namespace + "." + this.Name) + "Formatter";

public UnionSubTypeInfo[] SubTypes { get; set; }
public UnionSubTypeInfo[] SubTypes { get; }

public UnionSerializationInfo(string? @namespace, string name, string fullName, UnionSubTypeInfo[] subTypes)
{
Namespace = @namespace;
Name = name;
FullName = fullName;
SubTypes = subTypes;
}
}

public class UnionSubTypeInfo
{
public string Type { get; set; }
public UnionSubTypeInfo(int key, string type)
{
Key = key;
Type = type;
}

public int Key { get; }

public int Key { get; set; }
public string Type { get; }
}
}