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:
Kamron Batman 2021-05-26 13:20:52 -07:00 committed by GitHub
parent 1ac91c3998
commit 3c6356e8fa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 152 additions and 100 deletions

View file

@ -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)
{