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

@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
@ -54,17 +55,16 @@ namespace SerializationGenerator
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
// List of types that _will_ become ISerializable
var serializableList = receiver
.Fields
.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default)
.Select(g => g.Key as INamedTypeSymbol)
.ClassAndFields
.Select(g => g.Key)
.Where(t => t.WillBeSerializable(context))
.ToImmutableArray();
foreach (IGrouping<ISymbol, IFieldSymbol> group in receiver.Fields.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default))
foreach (var kvp in receiver.ClassAndFields)
{
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
group.Key as INamedTypeSymbol,
group.ToList(),
kvp.Key,
kvp.Value,
context,
migrationPath,
jsonOptions,
@ -73,7 +73,7 @@ namespace SerializationGenerator
if (classSource != null)
{
context.AddSource($"{group.Key.Name}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
context.AddSource($"{kvp.Key.ToDisplayString()}.Serialization.cs", SourceText.From(classSource, Encoding.UTF8));
}
}
}

View file

@ -126,7 +126,7 @@ namespace SerializationGenerator
};
}
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}()");
source.AppendLine($"{indent}{propertyName} = reader.{readMethod}();");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)

View file

@ -56,7 +56,7 @@ namespace SerializationGenerator
}
var propertyName = property.Name;
source.AppendLine($"{indent}{propertyName} = reader.Read{property.RuleArguments[0]}()");
source.AppendLine($"{indent}{propertyName} = reader.Read{property.RuleArguments[0]}();");
}
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)

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 });
}
}
}
}