Skip to content

Commit

Permalink
Merge pull request #941 from EdwardCooke/ec-nullability
Browse files Browse the repository at this point in the history
Fix bugs and add features

+semver:breaking
  • Loading branch information
EdwardCooke authored Jul 14, 2024
2 parents 760472f + 14accea commit 8808c6f
Show file tree
Hide file tree
Showing 114 changed files with 4,933 additions and 3,250 deletions.
3 changes: 2 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
*.sh text eol=lf
*.cs text eol=crlf
*.cs text eol=crlf
*.fs text eol=crlf
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The library has now been successfully used in multiple projects and is considere
* netstandard 2.0
* netstandard 2.1
* .NET 6.0
* .NET 7.0
* .NET 8.0
* .NET Framework 4.7

## Quick start
Expand Down
12 changes: 11 additions & 1 deletion YamlDotNet.Analyzers.StaticGenerator/ClassObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ public class ClassObject
public string GuidSuffix { get; }
public bool IsArray { get; }
public bool IsDictionary { get; }
public bool IsDictionaryOverride { get; }
public bool IsList { get; }
public bool IsListOverride { get; }
public ITypeSymbol ModuleSymbol { get; }
public List<IMethodSymbol> OnDeserializedMethods { get; }
public List<IMethodSymbol> OnDeserializingMethods { get; }
Expand All @@ -41,7 +43,13 @@ public class ClassObject
public List<IPropertySymbol> PropertySymbols { get; }
public string SanitizedClassName { get; }

public ClassObject(string sanitizedClassName, ITypeSymbol moduleSymbol, bool isDictionary = false, bool isList = false, bool isArray = false)
public ClassObject(string sanitizedClassName,
ITypeSymbol moduleSymbol,
bool isDictionary = false,
bool isList = false,
bool isArray = false,
bool isListOverride = false,
bool isDictionaryOverride = false)
{
FieldSymbols = new List<IFieldSymbol>();
PropertySymbols = new List<IPropertySymbol>();
Expand All @@ -50,12 +58,14 @@ public ClassObject(string sanitizedClassName, ITypeSymbol moduleSymbol, bool isD
IsDictionary = isDictionary;
IsList = isList;
IsArray = isArray;
IsListOverride = isListOverride;
ModuleSymbol = moduleSymbol;
OnDeserializedMethods = new List<IMethodSymbol>();
OnDeserializingMethods = new List<IMethodSymbol>();
OnSerializedMethods = new List<IMethodSymbol>();
OnSerializingMethods = new List<IMethodSymbol>();
SanitizedClassName = sanitizedClassName;
IsDictionaryOverride = isDictionaryOverride;
}
}
}
39 changes: 39 additions & 0 deletions YamlDotNet.Analyzers.StaticGenerator/EnumMappings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of YamlDotNet - A .NET library for YAML.
// Copyright (c) Antoine Aubry and contributors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
// of the Software, and to permit persons to whom the Software is furnished to do
// so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using Microsoft.CodeAnalysis;

namespace YamlDotNet.Analyzers.StaticGenerator
{
public class EnumMappings
{
public ITypeSymbol Type { get; set; }
public string ActualName { get; set; }
public string EnumMemberValue { get; set; }

public EnumMappings(ITypeSymbol type, string actualName, string enumMemberValue)
{
ActualName = actualName;
EnumMemberValue = enumMemberValue;
Type = type;
}
}
}
2 changes: 1 addition & 1 deletion YamlDotNet.Analyzers.StaticGenerator/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,6 @@ public void UnIndent()
_unindent();
}

public abstract void Write(ClassSyntaxReceiver classSyntaxReceiver);
public abstract void Write(SerializableSyntaxReceiver classSyntaxReceiver);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ public ObjectAccessorFileGenerator(Action<string, bool> Write, Action indent, Ac
{
}

public override void Write(ClassSyntaxReceiver classSyntaxReceiver)
public override void Write(SerializableSyntaxReceiver syntaxReceiver)
{
foreach (var o in classSyntaxReceiver.Classes)
foreach (var o in syntaxReceiver.Classes)
{
var classObject = o.Value;
Write($"class {classObject.SanitizedClassName}_{classObject.GuidSuffix} : YamlDotNet.Serialization.IObjectAccessor");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,24 @@

namespace YamlDotNet.Analyzers.StaticGenerator
{
public class ClassSyntaxReceiver : ISyntaxContextReceiver
public class SerializableSyntaxReceiver : ISyntaxContextReceiver
{
public List<string> Log { get; } = new();
public Dictionary<string, ClassObject> Classes { get; } = new Dictionary<string, ClassObject>();
public Dictionary<ITypeSymbol, List<EnumMappings>> EnumMappings { get; } = new Dictionary<ITypeSymbol, List<EnumMappings>>(SymbolEqualityComparer.Default);
public INamedTypeSymbol? YamlStaticContextType { get; set; }

public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is ClassDeclarationSyntax classDeclarationSyntax)
if (context.Node is EnumDeclarationSyntax enumDeclarationSyntax)
{
var enumSymbol = context.SemanticModel.GetDeclaredSymbol(enumDeclarationSyntax)!;
if (enumSymbol.GetAttributes().Any(attribute => attribute.AttributeClass?.ToDisplayString() == "YamlDotNet.Serialization.YamlSerializableAttribute"))
{
HandleEnum(enumSymbol);
}
}
else if (context.Node is ClassDeclarationSyntax classDeclarationSyntax)
{
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax)!;
if (classSymbol.GetAttributes().Any())
Expand All @@ -53,7 +62,10 @@ public void OnVisitSyntaxNode(GeneratorSyntaxContext context)

foreach (var type in types.OfType<INamedTypeSymbol>())
{
AddSerializableClass(type);
if (type.TypeKind == TypeKind.Class)
{
AddSerializableClass(type);
}
}
}

Expand Down Expand Up @@ -98,6 +110,7 @@ private void AddSerializableClass(INamedTypeSymbol? classSymbol)
{
classObject.PropertySymbols.Add(propertySymbol);
CheckForSupportedGeneric(propertySymbol.Type);
HandleEnum(propertySymbol.Type);
}
}
else if (member is IFieldSymbol fieldSymbol)
Expand All @@ -106,6 +119,7 @@ private void AddSerializableClass(INamedTypeSymbol? classSymbol)
{
classObject.FieldSymbols.Add(fieldSymbol);
CheckForSupportedGeneric(fieldSymbol.Type);
HandleEnum(fieldSymbol.Type);
}
}
else if (member is IMethodSymbol methodSymbol)
Expand Down Expand Up @@ -163,6 +177,46 @@ private void CheckForSupportedGeneric(ITypeSymbol type)
Classes.Add(sanitizedTypeName, new ClassObject(sanitizedTypeName, (INamedTypeSymbol)type, isList: true));
CheckForSupportedGeneric(((INamedTypeSymbol)type).TypeArguments[0]);
}
// Overrides for lists
else if (typeName.StartsWith("System.Collections.Generic.ICollection") ||
typeName.StartsWith("System.Collections.Generic.IEnumerable") ||
typeName.StartsWith("System.Collections.Generic.IList") ||
typeName.StartsWith("System.Collections.Generic.IReadOnlyCollection") ||
typeName.StartsWith("System.Collections.Generic.IReadOnlyList"))
{
Classes.Add(sanitizedTypeName, new ClassObject(sanitizedTypeName, (INamedTypeSymbol)type, isListOverride: true));
CheckForSupportedGeneric(((INamedTypeSymbol)type).TypeArguments[0]);
}
else if (typeName.StartsWith("System.Collections.Generic.IReadOnlyDictionary"))
{
Classes.Add(sanitizedTypeName, new ClassObject(sanitizedTypeName, (INamedTypeSymbol)type, isDictionaryOverride: true));
CheckForSupportedGeneric(((INamedTypeSymbol)type).TypeArguments[1]);
}
}

private void HandleEnum(ITypeSymbol type)
{
if (type.TypeKind == TypeKind.Enum)
{
var enumMembers = type.GetMembers();
var mappings = new List<EnumMappings>();
foreach (var member in enumMembers)
{
if (member.Kind == SymbolKind.Field)
{
var enumMember = member.GetAttributes().FirstOrDefault(x => x.AttributeClass!.ToDisplayString() == "System.Runtime.Serialization.EnumMemberAttribute");
var memberName = member.Name!;
var memberValue = memberName;
if (enumMember != null)
{
var argument = enumMember.NamedArguments.FirstOrDefault(x => x.Key == "Value");
memberValue = (string)argument.Value.Value!;
}
mappings.Add(new EnumMappings(type, memberName, memberValue));
}
}
this.EnumMappings[type] = mappings;
}
}
}
}
21 changes: 7 additions & 14 deletions YamlDotNet.Analyzers.StaticGenerator/StaticContextFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ public StaticContextFile(Action<string, bool> write, Action indent, Action unind
{
}

public override void Write(ClassSyntaxReceiver classSyntaxReceiver)
public override void Write(SerializableSyntaxReceiver syntaxReceiver)
{
Write($"public partial class {classSyntaxReceiver.YamlStaticContextType?.Name ?? "StaticContext"} : YamlDotNet.Serialization.StaticContext");
Write($"public partial class {syntaxReceiver.YamlStaticContextType?.Name ?? "StaticContext"} : YamlDotNet.Serialization.StaticContext");
Write("{"); Indent();
Write("private readonly YamlDotNet.Serialization.ObjectFactories.StaticObjectFactory _objectFactory;");
Write("private readonly YamlDotNet.Serialization.ITypeResolver _typeResolver;");
Write("private readonly YamlDotNet.Serialization.ITypeInspector _typeInspector;");
Write($"public {classSyntaxReceiver.YamlStaticContextType?.Name ?? "StaticContext"}()");
Write($"public {syntaxReceiver.YamlStaticContextType?.Name ?? "StaticContext"}()");
Write("{"); Indent();
Write("_objectFactory = new StaticObjectFactory();");
Write("_typeResolver = new StaticTypeResolver(this);");
Expand All @@ -48,26 +48,19 @@ public override void Write(ClassSyntaxReceiver classSyntaxReceiver)
Write("public override YamlDotNet.Serialization.ITypeResolver GetTypeResolver() => _typeResolver;");
Write("public override bool IsKnownType(Type type)");
Write("{"); Indent();
foreach (var o in classSyntaxReceiver.Classes)
foreach (var o in syntaxReceiver.Classes)
{
var classObject = o.Value;
if (classObject.IsArray)
{
Write($"if (type == typeof({classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)})) return true;");
}
else if (classObject.IsList)
{
Write($"if (type == typeof({classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)})) return true;");
}
else if (classObject.IsDictionary)
if (classObject.IsArray || classObject.IsList || classObject.IsDictionary || classObject.IsDictionaryOverride || classObject.IsListOverride)
{
Write($"if (type == typeof({classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)})) return true;");
}
else
{
Write($"if (type == typeof({o.Value.ModuleSymbol.GetFullName().Replace("?", string.Empty)})) return true;");
//always support a array, list and dictionary of the type
//always support an array, ienumerable<T>, list and dictionary of the type
Write($"if (type == typeof({classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}[])) return true;");
Write($"if (type == typeof(System.Collections.Generic.IEnumerable<{classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}>)) return true;");
Write($"if (type == typeof(System.Collections.Generic.List<{classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}>)) return true;");
Write($"if (type == typeof(System.Collections.Generic.Dictionary<string, {classObject.ModuleSymbol.GetFullName().Replace("?", string.Empty)}>)) return true;");
}
Expand Down
Loading

0 comments on commit 8808c6f

Please sign in to comment.