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

Fix bugs and add features #941

Merged
merged 19 commits into from
Jul 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
08fbe44
Added checks when setting properties that if null, throws
EdwardCooke Jul 4, 2024
8c1dbad
Support really big yaml files
EdwardCooke Jul 5, 2024
9baf8fa
Support case insensitive property and field matching
EdwardCooke Jul 5, 2024
d9b7e0f
Added large yaml file benchmark
EdwardCooke Jul 6, 2024
879adb3
Support for required attributes
EdwardCooke Jul 6, 2024
2108d50
Support for some collection interfaces in the static analyzer
EdwardCooke Jul 7, 2024
276da6c
Support for using EnumMember attribute to specify the values in yaml
EdwardCooke Jul 7, 2024
6e64d6c
Fix non numeric outputs from floats in json compatible output
EdwardCooke Jul 8, 2024
858d405
Use ISO8601 for JSON Compatible serialization
EdwardCooke Jul 11, 2024
ca311d4
Pass in the serializer/deserializer to the yamltypeconverters for com…
EdwardCooke Jul 13, 2024
a59301b
Set converter based on attributes
EdwardCooke Jul 13, 2024
2d332ae
Line endings in a few files?
EdwardCooke Jul 14, 2024
b176139
Merge branch 'master' into ec-nullability
EdwardCooke Jul 14, 2024
e0abc6a
Remove net70, fixed up the fsharp unit tests
EdwardCooke Jul 14, 2024
39bb970
Make fsharp files crlf, seal the yamlconverter attribute
EdwardCooke Jul 14, 2024
c428589
Merge branch 'master' of github.com:aaubry/YamlDotNet into ec-nullabi…
EdwardCooke Jul 14, 2024
30cced2
Expose building typeinspector since it's used in other areas
EdwardCooke Jul 14, 2024
2c75a78
Missed a small merge conflict
EdwardCooke Jul 14, 2024
14accea
Finish removal of .net7
EdwardCooke Jul 14, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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