fix(codegen): Fixes serializing classes with no fields (#609)

Fixes code genning classes with no fields.
Fixes code genning primitives.
FIxes code genning uo types.
This commit is contained in:
Kamron Batman 2021-05-23 21:47:18 -07:00 committed by GitHub
parent 9afa4e4cab
commit 82b0d03e19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 11 deletions

View file

@ -22,12 +22,29 @@ namespace SerializationGenerator
{
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
{
public List<IFieldSymbol> Fields { get; } = new();
#pragma warning disable RS1024
public Dictionary<INamedTypeSymbol, List<IFieldSymbol>> ClassAndFields { get; } = new(SymbolEqualityComparer.Default);
#pragma warning restore RS1024
public static HashSet<string> AttributeTypes { get; } = new();
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
{
if (context.Node is ClassDeclarationSyntax { AttributeLists: { Count: > 0 } } classDeclarationSyntax)
{
if (context.SemanticModel.GetDeclaredSymbol(classDeclarationSyntax) is not INamedTypeSymbol classSymbol)
{
return;
}
if (!ClassAndFields.ContainsKey(classSymbol))
{
ClassAndFields.Add(classSymbol, new List<IFieldSymbol>());
}
return;
}
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
{
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
@ -39,7 +56,15 @@ namespace SerializationGenerator
if (fieldSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
{
Fields.Add(fieldSymbol);
var classSymbol = fieldSymbol.ContainingType;
if (ClassAndFields.TryGetValue(classSymbol, out var fieldsList))
{
fieldsList.Add(fieldSymbol);
}
else
{
ClassAndFields.Add(classSymbol, new List<IFieldSymbol> { fieldSymbol });
}
}
}
}