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:
parent
9afa4e4cab
commit
82b0d03e19
4 changed files with 36 additions and 11 deletions
|
|
@ -13,6 +13,7 @@
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||||
*************************************************************************/
|
*************************************************************************/
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Collections.Immutable;
|
using System.Collections.Immutable;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
@ -54,17 +55,16 @@ namespace SerializationGenerator
|
||||||
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
|
var jsonOptions = SerializableMigration.GetJsonSerializerOptions(context.Compilation);
|
||||||
// List of types that _will_ become ISerializable
|
// List of types that _will_ become ISerializable
|
||||||
var serializableList = receiver
|
var serializableList = receiver
|
||||||
.Fields
|
.ClassAndFields
|
||||||
.GroupBy(f => f.ContainingType, SymbolEqualityComparer.Default)
|
.Select(g => g.Key)
|
||||||
.Select(g => g.Key as INamedTypeSymbol)
|
|
||||||
.Where(t => t.WillBeSerializable(context))
|
.Where(t => t.WillBeSerializable(context))
|
||||||
.ToImmutableArray();
|
.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(
|
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
|
||||||
group.Key as INamedTypeSymbol,
|
kvp.Key,
|
||||||
group.ToList(),
|
kvp.Value,
|
||||||
context,
|
context,
|
||||||
migrationPath,
|
migrationPath,
|
||||||
jsonOptions,
|
jsonOptions,
|
||||||
|
|
@ -73,7 +73,7 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
if (classSource != null)
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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)
|
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ namespace SerializationGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
var propertyName = property.Name;
|
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)
|
public void GenerateSerializationMethod(StringBuilder source, string indent, SerializableProperty property)
|
||||||
|
|
|
||||||
|
|
@ -22,12 +22,29 @@ namespace SerializationGenerator
|
||||||
{
|
{
|
||||||
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
|
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 static HashSet<string> AttributeTypes { get; } = new();
|
||||||
|
|
||||||
public void OnVisitSyntaxNode(GeneratorSyntaxContext context)
|
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)
|
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
|
||||||
{
|
{
|
||||||
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
|
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
|
||||||
|
|
@ -39,7 +56,15 @@ namespace SerializationGenerator
|
||||||
|
|
||||||
if (fieldSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
|
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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue