Skip to content

Commit

Permalink
MPC: nullable enable and update language version to 9. (#1125)
Browse files Browse the repository at this point in the history
* Refactoring enable nullable

* Relax nullable

* refactor:  is IArrayTypeSymbol pattern match

* Typo: NameSpace -> Namespace

* Typo: INameSpaceInfo -> INamespaceInfo

* Fix: INamespaceInfo
  • Loading branch information
pCYSl5EDgo committed Nov 20, 2020
1 parent 7efe772 commit 010ed71
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 570 deletions.
156 changes: 106 additions & 50 deletions src/MessagePack.GeneratorCore/CodeAnalysis/Definitions.cs
Expand Up @@ -9,48 +9,48 @@

namespace MessagePackCompiler.CodeAnalysis
{
public interface INamespaceInfo
{
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; }
}
}

0 comments on commit 010ed71

Please sign in to comment.