feat(codegen): Adds InvalidateProperties (#616)

Adds invalidate properties to code genned fields.

Example:
```cs
[InvalidateProperties]
[EncodedInt]
private int _number; // Cliloc
```

Generates:
```cs
public int Number
{
    get => _number;
    set
    {
        if (value != _number)
        {
            _number = value;
            ((ISerializable)this).MarkDirty();
            InvalidateProperties();
        }
    }
}
```
This commit is contained in:
Kamron Batman 2021-05-24 22:49:00 -07:00 committed by GitHub
parent b20f3df595
commit 1ac91c3998
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 9 deletions

View file

@ -155,7 +155,7 @@ namespace SerializationGenerator
}
}
source.GenerateSerializableProperty(fieldSymbol);
source.GenerateSerializableProperty(fieldSymbol, compilation);
source.AppendLine();
var serializableProperty = SerializableMigrationRulesEngine.GenerateSerializableProperty(

View file

@ -26,6 +26,7 @@ namespace SerializationGenerator
public const string IPADDRESS_CLASS = "System.Net.IPAddress";
public const string KEYVALUEPAIR_STRUCT = "System.Collections.Generic.KeyValuePair";
public const string INVALIDATEPROPERTIES_ATTRIBUTE = "Server.InvalidatePropertiesAttribute";
public const string AFTERDESERIALIZATION_ATTRIBUTE = "Server.AfterDeserializationAttribute";
public const string SERIALIZABLE_ATTRIBUTE = "Server.SerializableAttribute";
public const string SERIALIZABLE_FIELD_ATTRIBUTE = "Server.SerializableFieldAttribute";

View file

@ -13,6 +13,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using System.Linq;
using System.Text;
using Microsoft.CodeAnalysis;
@ -22,11 +23,22 @@ namespace SerializationGenerator
{
public static void GenerateSerializableProperty(
this StringBuilder source,
IFieldSymbol fieldSymbol
IFieldSymbol fieldSymbol,
Compilation compilation
)
{
var fieldName = fieldSymbol.Name;
var invalidatePropertiesAttribute = fieldSymbol
.GetAttributes()
.OfType<AttributeData>()
.FirstOrDefault(
attr => attr.AttributeClass?.Equals(
compilation.GetTypeByMetadataName(INVALIDATEPROPERTIES_ATTRIBUTE),
SymbolEqualityComparer.Default
) ?? false
);
source.GeneratePropertyStart(AccessModifier.Public, fieldSymbol);
// Getter
@ -34,13 +46,17 @@ namespace SerializationGenerator
// Setter
source.GeneratePropertySetterStart(false);
source.AppendLine(
$@" if (value != {fieldName})
{{
((ISerializable)this).MarkDirty();
{fieldName} = value;
}}"
);
const string indent = " ";
source.AppendLine($"{indent}if(value != {fieldName})");
source.AppendLine($"{indent}{{");
source.AppendLine($"{indent} {fieldName} = value;");
source.AppendLine($"{indent} ((ISerializable)this).MarkDirty();");
if (invalidatePropertiesAttribute != null)
{
source.AppendLine($"{indent} InvalidateProperties();");
}
source.AppendLine($"{indent}}}");
source.GeneratePropertyGetSetEnd(false);
source.GeneratePropertyEnd();