Skip to content

Commit

Permalink
Adds ability to use property with init-only setters
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianSauer committed Aug 11, 2024
1 parent 212a64d commit d8d8fc4
Show file tree
Hide file tree
Showing 6 changed files with 602 additions and 512 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<EnableNETAnalyzers>True</EnableNETAnalyzers>
<AnalysisLevel>latest-Recommended</AnalysisLevel>
<Version>4.0.0</Version>
<Version>4.1.0</Version>
<PackageReadmeFile>README.md</PackageReadmeFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<NoWarn>1701;1702;NU5128</NoWarn>
Expand Down
16 changes: 15 additions & 1 deletion AutomaticInterface/AutomaticInterface/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ InterfaceBuilder interfaceGenerator
var name = prop.Name;
var hasGet = prop.GetMethod?.DeclaredAccessibility == Accessibility.Public;
var hasSet = prop.SetMethod?.DeclaredAccessibility == Accessibility.Public;
var hasSet = GetSetKind(prop.SetMethod);
var isRef = prop.ReturnsByRef;
ActivateNullableIfNeeded(interfaceGenerator, type);
Expand All @@ -281,6 +281,20 @@ InterfaceBuilder interfaceGenerator
});
}

private static PropertySetKind GetSetKind(IMethodSymbol? setMethodSymbol)
{
return setMethodSymbol switch
{
null => PropertySetKind.NoSet,
{ IsInitOnly: true, DeclaredAccessibility: Accessibility.Public }
=> PropertySetKind.Init,
_
=> setMethodSymbol is { DeclaredAccessibility: Accessibility.Public }
? PropertySetKind.Always
: PropertySetKind.NoSet
};
}

private static bool HasIgnoreAttribute(ISymbol x)
{
return x.GetAttributes()
Expand Down
27 changes: 23 additions & 4 deletions AutomaticInterface/AutomaticInterface/InterfaceBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

Expand All @@ -8,7 +9,7 @@ internal sealed record PropertyInfo(
string Name,
string Ttype,
bool HasGet,
bool HasSet,
PropertySetKind SetKind,
bool IsRef,
string Documentation
);
Expand Down Expand Up @@ -50,7 +51,7 @@ public void AddPropertyToInterface(
string name,
string ttype,
bool hasGet,
bool hasSet,
PropertySetKind hasSet,
bool isRef,
string documentation
)
Expand Down Expand Up @@ -126,7 +127,7 @@ public string Build()
cb.AppendAndNormalizeMultipleLines(prop.Documentation);
var @ref = prop.IsRef ? "ref " : string.Empty;
var get = prop.HasGet ? "get; " : string.Empty;
var set = prop.HasSet ? "set; " : string.Empty;
var set = GetSet(prop.SetKind);
cb.AppendLine($"{@ref}{prop.Ttype} {prop.Name} {{ {get}{set}}}");
cb.AppendLine("");
}
Expand Down Expand Up @@ -159,6 +160,17 @@ public string Build()
return cb.Build();
}

private static string GetSet(PropertySetKind propSetKind)
{
return propSetKind switch
{
PropertySetKind.NoSet => string.Empty,
PropertySetKind.Always => "set; ",
PropertySetKind.Init => "init; ",
_ => throw new ArgumentOutOfRangeException(nameof(propSetKind), propSetKind, null)
};
}

private static void BuildMethod(CodeBuilder cb, MethodInfo method)
{
cb.AppendAndNormalizeMultipleLines(method.Documentation);
Expand Down Expand Up @@ -191,6 +203,13 @@ public override string ToString()
}
}

public enum PropertySetKind
{
NoSet = 0,
Always = 1,
Init = 2,
}

public class CodeBuilder
{
private readonly StringBuilder sb = new();
Expand Down
Loading

0 comments on commit d8d8fc4

Please sign in to comment.