feat(codegen): Adds support for partial opt-in with already existing properties (#617)
- [X] Fixes an issue with ordering of properties in serialization.
- [X] Adds opt-in with existing properties.
Example:
```cs
private int _myExistingField;
[SerializableField(1)]
public int MyExistingProperty
{
get => _myExistingField;
set
{
if (value == 0)
{
Parent = null;
}
if (value != _myExistingField)
{
((ISerializable)this).MarkDirty();
_myExistingField = value;
}
}
}
```
This commit is contained in:
parent
1ac91c3998
commit
3c6356e8fa
16 changed files with 152 additions and 100 deletions
|
|
@ -13,7 +13,6 @@
|
|||
* 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;
|
||||
|
|
@ -64,7 +63,7 @@ namespace SerializationGenerator
|
|||
{
|
||||
string classSource = SerializableEntityGeneration.GenerateSerializationPartialClass(
|
||||
kvp.Key,
|
||||
kvp.Value,
|
||||
kvp.Value.ToImmutableArray(),
|
||||
context,
|
||||
migrationPath,
|
||||
jsonOptions,
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Linq;
|
||||
|
|
@ -44,7 +45,7 @@ namespace SerializationGenerator
|
|||
|
||||
var versionValue = classSymbol.GetAttributes()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
|
||||
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableEntityAttribute)
|
||||
)?.ConstructorArguments.FirstOrDefault().Value;
|
||||
|
||||
return versionValue != null;
|
||||
|
|
@ -52,7 +53,7 @@ namespace SerializationGenerator
|
|||
|
||||
public static string GenerateSerializationPartialClass(
|
||||
INamedTypeSymbol classSymbol,
|
||||
IList<IFieldSymbol> fields,
|
||||
ImmutableArray<ISymbol> fieldsAndProperties,
|
||||
GeneratorExecutionContext context,
|
||||
string migrationPath,
|
||||
JsonSerializerOptions jsonSerializerOptions,
|
||||
|
|
@ -85,11 +86,11 @@ namespace SerializationGenerator
|
|||
|
||||
var serializableAttribute = classSymbol.GetAttributes()
|
||||
.FirstOrDefault(
|
||||
attr => attr.AttributeClass?.Equals(serializableEntityAttribute, SymbolEqualityComparer.Default) ?? false
|
||||
attr => SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableEntityAttribute)
|
||||
);
|
||||
|
||||
var version = (int)serializableAttribute?.ConstructorArguments[0].Value!;
|
||||
var encodedVersion = (bool)serializableAttribute?.ConstructorArguments[1].Value!;
|
||||
var encodedVersion = (bool)serializableAttribute.ConstructorArguments[1].Value!;
|
||||
|
||||
var namespaceName = classSymbol.ContainingNamespace.ToDisplayString();
|
||||
var className = classSymbol.Name;
|
||||
|
|
@ -115,61 +116,86 @@ namespace SerializationGenerator
|
|||
);
|
||||
source.AppendLine();
|
||||
|
||||
var serializableProperties = new List<SerializableProperty>();
|
||||
var serializablePropertySet = new SortedSet<SerializableProperty>();
|
||||
|
||||
foreach (IFieldSymbol fieldSymbol in fields)
|
||||
foreach (var fieldOrPropertySymbol in fieldsAndProperties)
|
||||
{
|
||||
var allAttributes = fieldSymbol.GetAttributes();
|
||||
var allAttributes = fieldOrPropertySymbol.GetAttributes();
|
||||
|
||||
var hasAttribute = allAttributes
|
||||
.Any(
|
||||
var serializableFieldAttr = allAttributes
|
||||
.FirstOrDefault(
|
||||
attr =>
|
||||
SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttribute)
|
||||
);
|
||||
|
||||
if (hasAttribute)
|
||||
if (serializableFieldAttr == null)
|
||||
{
|
||||
foreach (var attr in allAttributes)
|
||||
continue;
|
||||
}
|
||||
|
||||
var order = (int)serializableFieldAttr.ConstructorArguments[0].Value!;
|
||||
|
||||
foreach (var attr in allAttributes)
|
||||
{
|
||||
if (!SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttrAttribute))
|
||||
{
|
||||
if (!SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttrAttribute))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (attr.AttributeClass == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var ctorArgs = attr.ConstructorArguments;
|
||||
var attrTypeArg = ctorArgs[0];
|
||||
|
||||
if (attrTypeArg.Kind == TypedConstantKind.Primitive && attrTypeArg.Value is string attrStr)
|
||||
{
|
||||
source.AppendLine($" {attrStr}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var attrType = (ITypeSymbol)attrTypeArg.Value;
|
||||
source.GenerateAttribute(attrType.Name, ctorArgs[1].Values);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (attr.AttributeClass == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var ctorArgs = attr.ConstructorArguments;
|
||||
var attrTypeArg = ctorArgs[0];
|
||||
|
||||
if (attrTypeArg.Kind == TypedConstantKind.Primitive && attrTypeArg.Value is string attrStr)
|
||||
{
|
||||
source.AppendLine($" {attrStr}");
|
||||
}
|
||||
else
|
||||
{
|
||||
var attrType = (ITypeSymbol)attrTypeArg.Value;
|
||||
source.GenerateAttribute(attrType.Name, ctorArgs[1].Values);
|
||||
}
|
||||
}
|
||||
|
||||
string propertyName;
|
||||
ITypeSymbol propertyType;
|
||||
|
||||
if (fieldOrPropertySymbol is IFieldSymbol fieldSymbol)
|
||||
{
|
||||
source.GenerateSerializableProperty(fieldSymbol, compilation);
|
||||
source.AppendLine();
|
||||
|
||||
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
fieldSymbol.GetPropertyName(),
|
||||
fieldSymbol.Type,
|
||||
allAttributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
serializableProperties.Add(serializableProperty);
|
||||
propertyName = fieldSymbol.GetPropertyName();
|
||||
propertyType = fieldSymbol.Type;
|
||||
}
|
||||
else if (fieldOrPropertySymbol is IPropertySymbol propertySymbol)
|
||||
{
|
||||
propertyName = fieldOrPropertySymbol.Name;
|
||||
propertyType = propertySymbol.Type;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception($"Invalid node {fieldOrPropertySymbol.Name}. Expecting a field or property node.");
|
||||
}
|
||||
|
||||
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(
|
||||
compilation,
|
||||
propertyName,
|
||||
propertyType,
|
||||
order,
|
||||
allAttributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
||||
serializablePropertySet.Add(serializableProperty);
|
||||
}
|
||||
|
||||
var serializableProperties = serializablePropertySet.ToImmutableArray();
|
||||
|
||||
// If we are not inheriting ISerializable, then we need to define some stuff
|
||||
if (!isOverride)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ namespace SerializationGenerator
|
|||
int version,
|
||||
bool encodedVersion,
|
||||
List<SerializableMetadata> migrations,
|
||||
List<SerializableProperty> properties
|
||||
ImmutableArray<SerializableProperty> properties
|
||||
)
|
||||
{
|
||||
var genericReaderInterface = compilation.GetTypeByMetadataName(GENERIC_READER_INTERFACE);
|
||||
|
|
@ -106,12 +106,11 @@ namespace SerializationGenerator
|
|||
m.ReturnsVoid &&
|
||||
m.Parameters.Length == 0 &&
|
||||
m.GetAttributes()
|
||||
.OfType<AttributeData>()
|
||||
.Any(
|
||||
attr => attr.AttributeClass?.Equals(
|
||||
compilation.GetTypeByMetadataName(AFTERDESERIALIZATION_ATTRIBUTE),
|
||||
SymbolEqualityComparer.Default
|
||||
) ?? false
|
||||
attr => SymbolEqualityComparer.Default.Equals(
|
||||
attr.AttributeClass,
|
||||
compilation.GetTypeByMetadataName(AFTERDESERIALIZATION_ATTRIBUTE)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -13,7 +13,6 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
|
|
@ -27,7 +26,7 @@ namespace SerializationGenerator
|
|||
Compilation compilation,
|
||||
bool isOverride,
|
||||
bool encodedVersion,
|
||||
List<SerializableProperty> properties
|
||||
ImmutableArray<SerializableProperty> properties
|
||||
)
|
||||
{
|
||||
var genericWriterInterface = compilation.GetTypeByMetadataName(GENERIC_WRITER_INTERFACE);
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
"ArrayEntry",
|
||||
arrayTypeSymbol.ElementType,
|
||||
0,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
"SetEntry",
|
||||
setTypeSymbol,
|
||||
0,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
"key",
|
||||
typeArguments[0],
|
||||
0,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
|
@ -52,6 +53,7 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
"value",
|
||||
typeArguments[1],
|
||||
1,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
|
|
|||
|
|
@ -44,6 +44,7 @@ namespace SerializationGenerator
|
|||
compilation,
|
||||
"ListEntry",
|
||||
listTypeSymbol,
|
||||
0,
|
||||
attributes,
|
||||
serializableTypes
|
||||
);
|
||||
|
|
|
|||
|
|
@ -13,20 +13,20 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableMetadata
|
||||
public record SerializableMetadata
|
||||
{
|
||||
[JsonPropertyName("version")]
|
||||
public int Version { get; set; }
|
||||
public int Version { get; init; }
|
||||
|
||||
[JsonPropertyName("type")]
|
||||
public string Type { get; set; }
|
||||
public string Type { get; init; }
|
||||
|
||||
[JsonPropertyName("properties")]
|
||||
public List<SerializableProperty> Properties { get; set; }
|
||||
public ImmutableArray<SerializableProperty> Properties { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -49,6 +49,7 @@ namespace SerializationGenerator
|
|||
Compilation compilation,
|
||||
string propertyName,
|
||||
ISymbol propertyType,
|
||||
int order,
|
||||
ImmutableArray<AttributeData> attributes,
|
||||
ImmutableArray<INamedTypeSymbol> serializableTypes
|
||||
)
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ using System.Text.Json.Serialization;
|
|||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializableProperty
|
||||
public record SerializableProperty
|
||||
{
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; init; }
|
||||
|
|
@ -30,5 +30,8 @@ namespace SerializationGenerator
|
|||
|
||||
[JsonPropertyName("ruleArguments")]
|
||||
public string[] RuleArguments { get; init; }
|
||||
|
||||
[JsonIgnore]
|
||||
public int Order { get; init; }
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
/*************************************************************************
|
||||
* ModernUO *
|
||||
* Copyright 2019-2021 - ModernUO Development Team *
|
||||
* Email: hi@modernuo.com *
|
||||
* File: SerializablePropertyComparer.cs *
|
||||
* *
|
||||
* This program is free software: you can redistribute it and/or modify *
|
||||
* it under the terms of the GNU General Public License as published by *
|
||||
* the Free Software Foundation, either version 3 of the License, or *
|
||||
* (at your option) any later version. *
|
||||
* *
|
||||
* You should have received a copy of the GNU General Public License *
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
||||
*************************************************************************/
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
public class SerializablePropertyComparer : IComparer<SerializableProperty>
|
||||
{
|
||||
public int Compare(SerializableProperty x, SerializableProperty y)
|
||||
{
|
||||
if (Equals(x, y))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (Equals(null, y))
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (Equals(null, x))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
return x.Order.CompareTo(y.Order);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ namespace SerializationGenerator
|
|||
public class SerializerSyntaxReceiver : ISyntaxContextReceiver
|
||||
{
|
||||
#pragma warning disable RS1024
|
||||
public Dictionary<INamedTypeSymbol, List<IFieldSymbol>> ClassAndFields { get; } = new(SymbolEqualityComparer.Default);
|
||||
public Dictionary<INamedTypeSymbol, List<ISymbol>> ClassAndFields { get; } = new(SymbolEqualityComparer.Default);
|
||||
#pragma warning restore RS1024
|
||||
|
||||
public static HashSet<string> AttributeTypes { get; } = new();
|
||||
|
|
@ -39,7 +39,7 @@ namespace SerializationGenerator
|
|||
|
||||
if (classSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString()) && !ClassAndFields.ContainsKey(classSymbol)))
|
||||
{
|
||||
ClassAndFields.Add(classSymbol, new List<IFieldSymbol>());
|
||||
ClassAndFields.Add(classSymbol, new List<ISymbol>());
|
||||
}
|
||||
|
||||
return;
|
||||
|
|
@ -47,25 +47,35 @@ namespace SerializationGenerator
|
|||
|
||||
if (context.Node is FieldDeclarationSyntax { AttributeLists: { Count: > 0 } } fieldDeclarationSyntax)
|
||||
{
|
||||
foreach (VariableDeclaratorSyntax variable in fieldDeclarationSyntax.Declaration.Variables)
|
||||
foreach (var variable in fieldDeclarationSyntax.Declaration.Variables)
|
||||
{
|
||||
if (context.SemanticModel.GetDeclaredSymbol(variable) is not IFieldSymbol fieldSymbol)
|
||||
if (context.SemanticModel.GetDeclaredSymbol(variable) is IFieldSymbol fieldSymbol)
|
||||
{
|
||||
return;
|
||||
AddFieldOrProperty(fieldSymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (context.Node is PropertyDeclarationSyntax { AttributeLists: { Count: > 0 } } propertyDeclarationSyntax)
|
||||
{
|
||||
if (context.SemanticModel.GetDeclaredSymbol(propertyDeclarationSyntax) is IPropertySymbol propertySymbol)
|
||||
{
|
||||
AddFieldOrProperty(propertySymbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fieldSymbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
|
||||
{
|
||||
var classSymbol = fieldSymbol.ContainingType;
|
||||
if (ClassAndFields.TryGetValue(classSymbol, out var fieldsList))
|
||||
{
|
||||
fieldsList.Add(fieldSymbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClassAndFields.Add(classSymbol, new List<IFieldSymbol> { fieldSymbol });
|
||||
}
|
||||
}
|
||||
private void AddFieldOrProperty(ISymbol symbol)
|
||||
{
|
||||
if (symbol.GetAttributes().Any(ad => AttributeTypes.Contains(ad.AttributeClass?.ToDisplayString())))
|
||||
{
|
||||
var classSymbol = symbol.ContainingType;
|
||||
if (ClassAndFields.TryGetValue(classSymbol, out var fieldsList))
|
||||
{
|
||||
fieldsList.Add(symbol);
|
||||
}
|
||||
else
|
||||
{
|
||||
ClassAndFields.Add(classSymbol, new List<ISymbol> { symbol });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,19 +23,6 @@ namespace SerializationGenerator
|
|||
{
|
||||
public static partial class SourceGeneration
|
||||
{
|
||||
public static void GenerateUsings(this StringBuilder source, IImmutableList<ITypeSymbol> typesUsed)
|
||||
{
|
||||
var enumerable = typesUsed
|
||||
.Select(t => t.ContainingNamespace.Name)
|
||||
.Distinct()
|
||||
.OrderByDescending(t => t);
|
||||
|
||||
foreach (var t in enumerable)
|
||||
{
|
||||
source.Insert(0, $"using {t}{Environment.NewLine}");
|
||||
}
|
||||
}
|
||||
|
||||
public static void GenerateNamespaceStart(this StringBuilder source, string namespaceName)
|
||||
{
|
||||
source.AppendLine($@"namespace {namespaceName}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue