Added AccessModifier
MODIFY
WalletWasabi.Fluent.Generators/AutoNotifyGenerator.cs
WalletWasabi.Fluent.Generators/AutoNotifyGenerator.cs
@@ -23,10 +23,26 @@ namespace WalletWasabi.Fluent
public AutoNotifyAttribute()
{
}
- public string PropertyName { get; set; }
+
+ public string? PropertyName { get; set; }
+
+ public AccessModifier Modifier { get; set; } = AccessModifier.Public;
}
}";
+ private const string ModifierText = @"// <auto-generated />
+
+namespace WalletWasabi.Fluent
+{
+ public enum AccessModifier
+ {
+ None = 0,
+ Public = 1,
+ Protected = 2,
+ Private = 3,
+ Internal = 4
+ }
+}";
public void Initialize(GeneratorInitializationContext context)
{
// System.Diagnostics.Debugger.Launch();
@@ -35,6 +51,7 @@ namespace WalletWasabi.Fluent
public void Execute(GeneratorExecutionContext context)
{
+ context.AddSource("AccessModifier", SourceText.From(ModifierText, Encoding.UTF8));
context.AddSource("AutoNotifyAttribute", SourceText.From(AttributeText, Encoding.UTF8));
if (!(context.SyntaxReceiver is SyntaxReceiver receiver))
@@ -43,7 +60,9 @@ namespace WalletWasabi.Fluent
}
var options = (context.Compilation as CSharpCompilation)?.SyntaxTrees[0].Options as CSharpParseOptions;
- var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
+ var compilation = context.Compilation.AddSyntaxTrees(
+ CSharpSyntaxTree.ParseText(SourceText.From(ModifierText, Encoding.UTF8), options),
+ CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
var attributeSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.AutoNotifyAttribute");
if (attributeSymbol is null)
@@ -170,12 +189,54 @@ namespace {namespaceName}
return;
}
+ var overridenModifierOpt = attributeData.NamedArguments.SingleOrDefault(kvp => kvp.Key == "Modifier").Value;
+ var modifier = ChooseModifier(overridenModifierOpt);
+
+ if (modifier is null)
+ {
source.Append($@"
public {fieldType} {propertyName}
+ {{
+ get => {fieldName};
+ }}");
+ }
+ else
+ {
+ source.Append($@"
+ {modifier} {fieldType} {propertyName}
{{
get => {fieldName};
set => this.RaiseAndSetIfChanged(ref {fieldName}, value);
}}");
+ }
+
+
+ static string? ChooseModifier(TypedConstant overridenModifierOpt)
+ {
+ if (!overridenModifierOpt.IsNull && overridenModifierOpt.Value is not null)
+ {
+ var value = (int)overridenModifierOpt.Value;
+ return value switch
+ {
+ // None
+ 0 => null,
+ // Public
+ 1 => "public",
+ // Protected
+ 2 => "protected",
+ // Private
+ 3 => "private",
+ // Internal
+ 4 => "internal",
+ // Default
+ _ => "public"
+ };
+ }
+ else
+ {
+ return "public";
+ }
+ }
static string? ChooseName(string fieldName, TypedConstant overridenNameOpt)
{

MODIFY
WalletWasabi.Fluent.Generators/PropertyGenerator.cs
WalletWasabi.Fluent.Generators/PropertyGenerator.cs
@@ -14,17 +14,9 @@ namespace WalletWasabi.Fluent.Generators
private const string AttributeText = @"// <auto-generated />
#nullable enable
using System;
+
namespace WalletWasabi.Fluent
{
- public enum PropertyModifier
- {
- None = 0,
- Public = 1,
- Protected = 2,
- Private = 3,
- Internal = 4
- }
-
[AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = true)]
public sealed class PropertyAttribute : Attribute
{
@@ -49,6 +41,20 @@ namespace WalletWasabi.Fluent
}
}";
+ private const string ModifierText = @"// <auto-generated />
+
+namespace WalletWasabi.Fluent
+{
+ public enum PropertyModifier
+ {
+ None = 0,
+ Public = 1,
+ Protected = 2,
+ Private = 3,
+ Internal = 4
+ }
+}";
+
public void Initialize(GeneratorInitializationContext context)
{
// System.Diagnostics.Debugger.Launch();
@@ -57,6 +63,10 @@ namespace WalletWasabi.Fluent
public void Execute(GeneratorExecutionContext context)
{
+ if (context.Compilation.GetTypeByMetadataName("WalletWasabi.Fluent.PropertyModifier") == null)
+ {
+ context.AddSource("PropertyModifier", SourceText.From(ModifierText, Encoding.UTF8));
+ }
context.AddSource("PropertyAttribute", SourceText.From(AttributeText, Encoding.UTF8));
if (!(context.SyntaxReceiver is SyntaxReceiver receiver))
@@ -65,7 +75,9 @@ namespace WalletWasabi.Fluent
}
var options = (context.Compilation as CSharpCompilation)?.SyntaxTrees[0].Options as CSharpParseOptions;
- var compilation = context.Compilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
+ var compilation = context.Compilation.AddSyntaxTrees(
+ CSharpSyntaxTree.ParseText(SourceText.From(ModifierText, Encoding.UTF8), options),
+ CSharpSyntaxTree.ParseText(SourceText.From(AttributeText, Encoding.UTF8), options));
var attributeSymbol = compilation.GetTypeByMetadataName("WalletWasabi.Fluent.PropertyAttribute");
if (attributeSymbol is null)
@@ -164,7 +176,7 @@ namespace {namespaceName}
.GetAttributes()
.Where(ad => ad?.AttributeClass?.Equals(attributeSymbol, SymbolEqualityComparer.Default) ?? false);
- static string ToModifier(int value)
+ static string ChooseModifier(int value)
{
return value switch
{
@@ -209,12 +221,12 @@ namespace {namespaceName}
source.Append($@"
public {propertyType} {propertyName}
{{
- {ToModifier(getterModifier.Value)}get => this.{fieldName};");
+ {ChooseModifier(getterModifier.Value)}get => this.{fieldName};");
if (!isReadOnly.Value)
{
source.Append($@"
- {ToModifier(setterModifier.Value)}set => this.RaiseAndSetIfChanged(ref {fieldName}, value);");
+ {ChooseModifier(setterModifier.Value)}set => this.RaiseAndSetIfChanged(ref {fieldName}, value);");
}
source.Append($@"

Added AccessModifier