Skip to content

Commit

Permalink
Auto-format source code
Browse files Browse the repository at this point in the history
  • Loading branch information
GitHub Actions Autoformatter committed Jan 26, 2025
1 parent a083d73 commit d67bab8
Showing 1 changed file with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ static string [] GetFlagsForTarget (Dictionary<string, (string AttributeFullName
.Select (kv => kv.Key)
.ToArray ();

static (string AttributeFullName, string AttributeName, BindingAttributeData Data)[] GetAttributesForTarget (
static (string AttributeFullName, string AttributeName, BindingAttributeData Data) [] GetAttributesForTarget (
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> dataAttribute,
AttributeTargets targets)
=> dataAttribute.Where (kv => kv.Value.Data.Target.HasFlag (targets))
.Select (kv => kv.Value)
.ToArray ();


static void WriteFlagProperty (TabbedStringBuilder sb, string flagName)
{
Expand All @@ -95,23 +95,23 @@ static void WriteAttributeProperty (TabbedStringBuilder sb,
(string AttributeFullName, string AttributeName, BindingAttributeData Data) attrData)
{
// add a property that will state if we have the attr, this will help with nullability
sb.AppendLine($"readonly bool _has{attrData.AttributeName} = false;");
sb.AppendLine ($"readonly bool _has{attrData.AttributeName} = false;");
sb.AppendLine ($"[MemberNotNullWhen (true, nameof ({attrData.AttributeName}))]");
using (var flagPropertyBlock = sb.CreateBlock ($"public bool Has{attrData.AttributeName}", block: true)) {
flagPropertyBlock.AppendLine ($"get => _has{attrData.AttributeName};");
flagPropertyBlock.AppendLine ($"private init => _has{attrData.AttributeName} = value;");
}
sb.AppendLine ();
sb.AppendLine($"readonly {attrData.Data.DataModelType}? _{attrData.AttributeName} = null;");
sb.AppendLine ();
sb.AppendLine ($"readonly {attrData.Data.DataModelType}? _{attrData.AttributeName} = null;");
// decorate to help with nullability
using (var attributePropertyBlock = sb.CreateBlock ($"public {attrData.Data.DataModelType}? {attrData.AttributeName}", block: true)) {
attributePropertyBlock.AppendLine ($"get => _{attrData.AttributeName};");
attributePropertyBlock.AppendLine ($"private init => _{attrData.AttributeName} = value;");
}
}

static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, string [] flags,
(string AttributeFullName, string AttributeName, BindingAttributeData Data)[] attributes)
static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, string [] flags,
(string AttributeFullName, string AttributeName, BindingAttributeData Data) [] attributes)
{
sb.Clear ();
sb.AppendLine ("// <auto-generated/>");
Expand Down Expand Up @@ -141,8 +141,8 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s
modelBlock.AppendLine ();
modelBlock.AppendLine ("readonly Dictionary<string, List<AttributeData>>? _attributesDictionary = null;");
using (var dictionaryPropertyBlock =
modelBlock.CreateBlock ("public Dictionary<string, List<AttributeData>>? AttributesDictionary",
block: true)) {
modelBlock.CreateBlock ("public Dictionary<string, List<AttributeData>>? AttributesDictionary",
block: true)) {
dictionaryPropertyBlock.AppendLine ("get => _attributesDictionary;");
using (var initBlock = dictionaryPropertyBlock.CreateBlock ("private init", block: true)) {
initBlock.AppendLine ("_attributesDictionary = value;");
Expand All @@ -153,7 +153,7 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s

foreach (var attributeData in attributes) {
// check if the attribute is present, if it is, set the value
ifBlock.AppendLine($"Has{attributeData.AttributeName} = _attributesDictionary.Has{attributeData.AttributeName} ();");
ifBlock.AppendLine ($"Has{attributeData.AttributeName} = _attributesDictionary.Has{attributeData.AttributeName} ();");
using (var attrIfBlock = ifBlock.CreateBlock ($"if (Has{attributeData.AttributeName})", block: true)) {
attrIfBlock.AppendLine ($"{attributeData.AttributeName} = _attributesDictionary.Get{attributeData.AttributeName} ();");
}
Expand All @@ -165,8 +165,8 @@ static void WriteDataModelExtension (TabbedStringBuilder sb, string dataModel, s
}

static void GenerateModelExtension (TabbedStringBuilder sb, string dataModel,
Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> attributes,
Dictionary<string, (string AttributeFullName, AttributeTargets Targets)> flags,
Dictionary<string, (string AttributeFullName, string AttributeName, BindingAttributeData Data)> attributes,
AttributeTargets targets,
SourceProductionContext context)
{
Expand All @@ -183,7 +183,7 @@ static AttributeTargets GetTarget (ISymbol symbol)
// loop over attrs, if we find the BindingFlagAttribute, return the target
foreach (var attr in attrData) {
if (attr.AttributeClass?.Name == BindingFlagData.Name
&& BindingFlagData.TryParse (attr, out var data)) {
&& BindingFlagData.TryParse (attr, out var data)) {
return data.Value.Target;
}
}
Expand All @@ -197,7 +197,7 @@ static AttributeTargets GetTarget (ISymbol symbol)
// loop over attrs, if we find the BindingFlagAttribute, return the target
foreach (var attr in attrData) {
if (attr.AttributeClass?.Name == BindingAttributeData.Name
&& BindingAttributeData.TryParse (attr, out var data)) {
&& BindingAttributeData.TryParse (attr, out var data)) {
return data;
}
}
Expand Down Expand Up @@ -229,7 +229,7 @@ void GenerateCode (SourceProductionContext context, Compilation compilation,
}

// all flags are collected, generate the code
var sb = new TabbedStringBuilder (new());
var sb = new TabbedStringBuilder (new ());
GenerateDictionaryExtension (sb, flags, dataAttributes);

// Add the source code to the compilation.
Expand Down Expand Up @@ -269,8 +269,8 @@ static void GenerateDictionaryExtension (TabbedStringBuilder sb,
// loop over the flags and generate a helper static method to retrieve it from a attribute data dict
foreach (var (methodName, attributeName) in flags) {
using (var methodBlock = classBlock.CreateBlock (
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendLine ($"return self.ContainsKey ({attributeName.AttributeFullName});");
}

Expand All @@ -281,16 +281,16 @@ static void GenerateDictionaryExtension (TabbedStringBuilder sb,
foreach (var (methodName, attributeInfo) in dataAttributes) {
// property to check if the attribute is present
using (var methodBlock = classBlock.CreateBlock (
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static bool {methodName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendLine ($"return self.ContainsKey ({attributeInfo.AttributeFullName});");
}

classBlock.AppendLine ();
// property to access the attribute
using (var methodBlock = classBlock.CreateBlock (
$"public static {attributeInfo.Data.DataModelType}? Get{attributeInfo.AttributeName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
$"public static {attributeInfo.Data.DataModelType}? Get{attributeInfo.AttributeName} (this Dictionary<string, List<AttributeData>> self)",
block: true)) {
methodBlock.AppendRaw (
$@"if (self.{methodName} ()) {{
var data = self.GetAttribute<{attributeInfo.Data.DataModelType}> ({attributeInfo.AttributeFullName}, {attributeInfo.Data.DataModelType}.TryParse);
Expand Down

0 comments on commit d67bab8

Please sign in to comment.