fix(codegen): Adds access modifiers to serializable fields (#633)
- [X] Adds options for SerializableField. Example: ```cs [SerializableField(0, getter: "protected", setter: "protected", isVritual: true)] private int _someField; ``` Defaults: `getter: "public", setter: "public", isVirtual: false`
This commit is contained in:
parent
72d3966541
commit
803b4a33cb
33 changed files with 183 additions and 164 deletions
|
|
@ -21,7 +21,6 @@ using System.Text;
|
|||
using System.Text.Json;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializableMigration;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
|
|
@ -128,7 +127,7 @@ namespace SerializationGenerator
|
|||
const string indent = " ";
|
||||
|
||||
source.GenerateClassField(
|
||||
AccessModifier.Private,
|
||||
Accessibility.Private,
|
||||
InstanceModifier.Const,
|
||||
"int",
|
||||
"_version",
|
||||
|
|
@ -154,8 +153,6 @@ namespace SerializationGenerator
|
|||
continue;
|
||||
}
|
||||
|
||||
var order = (int)serializableFieldAttr.ConstructorArguments[0].Value!;
|
||||
|
||||
foreach (var attr in allAttributes)
|
||||
{
|
||||
if (!SymbolEqualityComparer.Default.Equals(attr.AttributeClass, serializableFieldAttrAttribute))
|
||||
|
|
@ -182,9 +179,22 @@ namespace SerializationGenerator
|
|||
}
|
||||
}
|
||||
|
||||
var attrCtorArgs = serializableFieldAttr.ConstructorArguments;
|
||||
|
||||
var order = (int)attrCtorArgs[0].Value!;
|
||||
var getterAccessor = Helpers.GetAccessibility(attrCtorArgs[1].Value!.ToString());
|
||||
var setterAccessor = Helpers.GetAccessibility(attrCtorArgs[2].Value!.ToString());
|
||||
var virtualProperty = (bool)attrCtorArgs[3].Value!;
|
||||
|
||||
if (fieldOrPropertySymbol is IFieldSymbol fieldSymbol)
|
||||
{
|
||||
source.GenerateSerializableProperty(fieldSymbol, compilation);
|
||||
source.GenerateSerializableProperty(
|
||||
compilation,
|
||||
fieldSymbol,
|
||||
getterAccessor,
|
||||
setterAccessor,
|
||||
virtualProperty
|
||||
);
|
||||
source.AppendLine();
|
||||
}
|
||||
|
||||
|
|
@ -206,22 +216,22 @@ namespace SerializationGenerator
|
|||
{
|
||||
// long ISerializable.SavePosition { get; set; } = -1;
|
||||
source.GenerateAutoProperty(
|
||||
AccessModifier.None,
|
||||
Accessibility.NotApplicable,
|
||||
"long",
|
||||
"ISerializable.SavePosition",
|
||||
AccessModifier.None,
|
||||
AccessModifier.None,
|
||||
Accessibility.NotApplicable,
|
||||
Accessibility.NotApplicable,
|
||||
indent,
|
||||
defaultValue: "-1"
|
||||
);
|
||||
|
||||
// BufferWriter ISerializable.SaveBuffer { get; set; }
|
||||
source.GenerateAutoProperty(
|
||||
AccessModifier.None,
|
||||
Accessibility.NotApplicable,
|
||||
"BufferWriter",
|
||||
"ISerializable.SaveBuffer",
|
||||
AccessModifier.None,
|
||||
AccessModifier.None,
|
||||
Accessibility.NotApplicable,
|
||||
Accessibility.NotApplicable,
|
||||
indent
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializableMigration;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
|
|
@ -39,7 +38,7 @@ namespace SerializationGenerator
|
|||
|
||||
source.GenerateMethodStart(
|
||||
"Deserialize",
|
||||
AccessModifier.Public,
|
||||
Accessibility.Public,
|
||||
isOverride,
|
||||
"void",
|
||||
ImmutableArray.Create<(ITypeSymbol, string)>((genericReaderInterface, "reader"))
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
using System.Linq;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
|
|
@ -24,8 +23,11 @@ namespace SerializationGenerator
|
|||
{
|
||||
public static void GenerateSerializableProperty(
|
||||
this StringBuilder source,
|
||||
Compilation compilation,
|
||||
IFieldSymbol fieldSymbol,
|
||||
Compilation compilation
|
||||
Accessibility getter,
|
||||
Accessibility? setter,
|
||||
bool isVirtual
|
||||
)
|
||||
{
|
||||
var fieldName = fieldSymbol.Name;
|
||||
|
|
@ -40,27 +42,38 @@ namespace SerializationGenerator
|
|||
) ?? false
|
||||
);
|
||||
|
||||
source.GeneratePropertyStart(AccessModifier.Public, fieldSymbol);
|
||||
const string indent = " ";
|
||||
const string propertyIndent = " ";
|
||||
|
||||
var propertyAccessor = setter > getter ? setter : getter;
|
||||
var getterAccessor = getter == propertyAccessor ? Accessibility.NotApplicable : getter;
|
||||
|
||||
source.GeneratePropertyStart(indent, propertyAccessor.Value, isVirtual, fieldSymbol);
|
||||
|
||||
// Getter
|
||||
source.GeneratePropertyGetterReturnsField(fieldSymbol);
|
||||
source.GeneratePropertyGetterReturnsField(propertyIndent, fieldSymbol, getterAccessor);
|
||||
|
||||
// Setter
|
||||
source.GeneratePropertySetterStart(false);
|
||||
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)
|
||||
if (setter != null)
|
||||
{
|
||||
source.AppendLine($"{indent} InvalidateProperties();");
|
||||
}
|
||||
source.AppendLine($"{indent}}}");
|
||||
source.GeneratePropertyGetSetEnd(false);
|
||||
var setterAccessor = setter == propertyAccessor ? Accessibility.NotApplicable : setter;
|
||||
|
||||
source.GeneratePropertyEnd();
|
||||
// Setter
|
||||
source.GeneratePropertySetterStart(propertyIndent, false, setterAccessor.Value);
|
||||
const string innerIndent = " ";
|
||||
source.AppendLine($"{innerIndent}if (value != {fieldName})");
|
||||
source.AppendLine($"{innerIndent}{{");
|
||||
source.AppendLine($"{innerIndent} {fieldName} = value;");
|
||||
source.AppendLine($"{innerIndent} ((ISerializable)this).MarkDirty();");
|
||||
|
||||
if (invalidatePropertiesAttribute != null)
|
||||
{
|
||||
source.AppendLine($"{innerIndent} InvalidateProperties();");
|
||||
}
|
||||
source.AppendLine($"{innerIndent}}}");
|
||||
source.GeneratePropertyGetSetEnd(propertyIndent, false);
|
||||
}
|
||||
|
||||
source.GeneratePropertyEnd(indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,7 +16,6 @@
|
|||
using System.Collections.Immutable;
|
||||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
|
|
@ -34,7 +33,7 @@ namespace SerializationGenerator
|
|||
|
||||
source.GenerateConstructorStart(
|
||||
className,
|
||||
AccessModifier.Public,
|
||||
Accessibility.Public,
|
||||
new []{ (serialType, "serial") }.ToImmutableArray(),
|
||||
isOverride ? _baseParameters : ImmutableArray<string>.Empty
|
||||
);
|
||||
|
|
|
|||
|
|
@ -17,7 +17,6 @@ using System.Collections.Immutable;
|
|||
using System.Text;
|
||||
using Microsoft.CodeAnalysis;
|
||||
using SerializableMigration;
|
||||
using SourceGeneration;
|
||||
|
||||
namespace SerializationGenerator
|
||||
{
|
||||
|
|
@ -35,7 +34,7 @@ namespace SerializationGenerator
|
|||
|
||||
source.GenerateMethodStart(
|
||||
"Serialize",
|
||||
AccessModifier.Public,
|
||||
Accessibility.Public,
|
||||
isOverride,
|
||||
"void",
|
||||
ImmutableArray.Create<(ITypeSymbol, string)>((genericWriterInterface, "writer"))
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue