Skip to content

Commit

Permalink
Jwallace/null check (#21)
Browse files Browse the repository at this point in the history
* null check on generated code

* Update for pickier Rider analyzer

---------

Co-authored-by: Jim W <[email protected]>
  • Loading branch information
Seekatar and jimAtLoyal authored May 28, 2024
1 parent c871d70 commit defbfa3
Show file tree
Hide file tree
Showing 31 changed files with 65 additions and 63 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## [0.3.3] 2024-05-28

### Fixed

- Nullable syntax update for more picky Rider analyzer

## [0.3.2] 2024-05-27

### Added
Expand Down
10 changes: 3 additions & 7 deletions src/OptionToStringGenerator/OptionGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public static partial class ClassExtensions
// method signature
sb.Append($" {classAccessibility} static string OptionsToString(this ").Append(className).Append(
$$""""
o, string extraIndent = "")
? o, string extraIndent = "")
{
return $@"
"""");
Expand Down Expand Up @@ -337,19 +337,15 @@ public static partial class ClassExtensions
}
}

//if (attributeCount == 0
// && member.Type.TypeKind == TypeKind.Class
// && member.Type.GetAttributes().FirstOrDefault(o => o.AttributeClass?.Name.Equals($"Seektar.OptionToStringGenerator.{nameof(OptionsToStringAttribute)}") ?? false) != null)

// nested Options?
// nested Options?
if (attributeCount == 0
&& member.Type.TypeKind == TypeKind.Class
&& member.Type.SpecialType != SpecialType.System_String
&& member.Type.GetAttributes().FirstOrDefault(o => string.Equals(o.AttributeClass?.Name, nameof(OptionsToStringAttribute))
&& string.Equals(o.AttributeClass?.ContainingNamespace?.ToString(), "Seekatar.OptionToStringGenerator")) != null)
{
Debug.WriteLine("Found OptionsToStringAttribute");
formatParameters += $",formatMethod:(o) => o.OptionsToString(\"{indent}\"),noQuote:true";
formatParameters += $",formatMethod:(o) => o?.OptionsToString(\"{indent}\") ?? \"null\",noQuote:true";
}

if (!ignored)
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unit/Snapshots/UnitTests.BadTitle.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this BadTitleOptions o, string extraIndent = "")
public static string OptionsToString(this BadTitleOptions? o, string extraIndent = "")
{
return $@"HiThisdoesntexist:
{extraIndent} Name : {Format(o?.Name)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.EscapeOptions o, string extraIndent = "")
public static string OptionsToString(this Test.EscapeOptions? o, string extraIndent = "")
{
return $@"Test.EscapeOptions:
{extraIndent} Name : {Format(o?.Name,regex:".*{(\\w*)}.*")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.FormatOptions o, string extraIndent = "")
public static string OptionsToString(this Test.FormatOptions? o, string extraIndent = "")
{
return $@"Test.FormatOptions:
{extraIndent} PlainInt : {Format(o?.PlainInt,formatMethod:(o) => o?.ToString("N0"))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.FormatOptionsJson o, string extraIndent = "")
public static string OptionsToString(this Test.FormatOptionsJson? o, string extraIndent = "")
{
return $@"{{
""Test.FormatOptionsJson"" : {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.FormattingOptions o, string extraIndent = "")
public static string OptionsToString(this Test.FormattingOptions? o, string extraIndent = "")
{
return $@"Test.FormattingOptions:
{extraIndent} IntProp - {Format(o?.IntProp)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.ChildOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.ChildOptions? o, string extraIndent = "")
{
return $@"Test.ChildOptions:
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:2,suffixLen:0)}
{extraIndent} ParentSecret : {Format(o?.ParentSecret,prefixLen:0,suffixLen:0)}
";
}
internal static string OptionsToString(this Test.ChildOnlyOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.ChildOnlyOptions? o, string extraIndent = "")
{
return $@"Test.ChildOnlyOptions:
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:2,suffixLen:0)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.InternalOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.InternalOptions? o, string extraIndent = "")
{
return $@"Test.InternalOptions:
{extraIndent} Name : {Format(o?.Name)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.JsonOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.JsonOptions? o, string extraIndent = "")
{
return $@"{{
""Test.JsonOptions"" : {{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.MaskingOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.MaskingOptions? o, string extraIndent = "")
{
return $@"Test.MaskingOptions:
{extraIndent} Masked : {Format(o?.Masked,prefixLen:0,suffixLen:0)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.Next.Level.NamespaceTest o, string extraIndent = "")
internal static string OptionsToString(this Test.Next.Level.NamespaceTest? o, string extraIndent = "")
{
return $@"Test.Next.Level.NamespaceTest:
{extraIndent} StringProperty : {Format(o?.StringProperty)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.InnerOption o, string extraIndent = "")
internal static string OptionsToString(this Test.InnerOption? o, string extraIndent = "")
{
return $@"Test.InnerOption:
{extraIndent} StringProp : {Format(o?.StringProp,prefixLen:3,suffixLen:0)}
{extraIndent} IntProp : {Format(o?.IntProp)}
{extraIndent} DoubleProp : {Format(o?.DoubleProp)}
";
}
internal static string OptionsToString(this Test.InnerOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.InnerOptions? o, string extraIndent = "")
{
return $@"Test.InnerOptions:
{extraIndent} StringProp : {Format(o?.StringProp,prefixLen:3,suffixLen:0)}
{extraIndent} IntProp : {Format(o?.IntProp)}
{extraIndent} DoubleProp : {Format(o?.DoubleProp)}
";
}
internal static string OptionsToString(this Test.OuterOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.OuterOptions? o, string extraIndent = "")
{
return $@"Test.OuterOptions:
{extraIndent} Secret : {Format(o?.Secret,prefixLen:0,suffixLen:6)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.Wrapper.NestedOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.Wrapper.NestedOptions? o, string extraIndent = "")
{
return $@"Test.Wrapper.NestedOptions:
{extraIndent} IntProp : {Format(o?.IntProp)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.InnerOption o, string extraIndent = "")
internal static string OptionsToString(this Test.InnerOption? o, string extraIndent = "")
{
return $@"Test.InnerOption:
{extraIndent} StringProp : {Format(o?.StringProp,prefixLen:3,suffixLen:0)}
{extraIndent} IntProp : {Format(o?.IntProp)}
{extraIndent} DoubleProp : {Format(o?.DoubleProp)}
";
}
internal static string OptionsToString(this Test.InnerOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.InnerOptions? o, string extraIndent = "")
{
return $@"Test.InnerOptions:
{extraIndent} StringProp : {Format(o?.StringProp,prefixLen:3,suffixLen:0)}
{extraIndent} IntProp : {Format(o?.IntProp)}
{extraIndent} DoubleProp : {Format(o?.DoubleProp)}
";
}
internal static string OptionsToString(this Test.OuterOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.OuterOptions? o, string extraIndent = "")
{
return $@"Test.OuterOptions:
{extraIndent} Secret : {Format(o?.Secret,prefixLen:0,suffixLen:6)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.ObjectMasking o, string extraIndent = "")
internal static string OptionsToString(this Test.ObjectMasking? o, string extraIndent = "")
{
return $@"Test.ObjectMasking:
{extraIndent} AnObject : {Format(o?.AnObject,regex:"User Id=([^;]+).*Password=([^;]+)",ignoreCase:true)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.PropertyTestClass o, string extraIndent = "")
internal static string OptionsToString(this Test.PropertyTestClass? o, string extraIndent = "")
{
return $@"Test.PropertyTestClass:
{extraIndent} Number : {Format(o?.Number)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.ProviderOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.ProviderOptions? o, string extraIndent = "")
{
return $@"Test.ProviderOptions:
{extraIndent} DoubleProp : {Format(o?.DoubleProp,formatMethod:(o) => Test.ProviderOptions.MyDoubleNoQuotes(o),noQuote:true)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.PublicOptions o, string extraIndent = "")
public static string OptionsToString(this Test.PublicOptions? o, string extraIndent = "")
{
return $@"Test.PublicOptions:
{extraIndent} PlainText : {Format(o?.PlainText)}
Expand Down Expand Up @@ -32,7 +32,7 @@ namespace Seekatar.OptionToStringGenerator
{extraIndent} Color : {Format(o?.Color)}
";
}
public static string OptionsToString(this Test.PublicOptionsSorted o, string extraIndent = "")
public static string OptionsToString(this Test.PublicOptionsSorted? o, string extraIndent = "")
{
return $@"Test.PublicOptionsSorted:
{extraIndent} AMaskedObject : {Format(o?.AMaskedObject,regex:"AClass\\:\\s+(.*)")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.TitleOptions o, string extraIndent = "")
public static string OptionsToString(this Test.TitleOptions? o, string extraIndent = "")
{
return $@"TitleOptions_{o?.StringProp}_{o?.IntProp}:
{extraIndent} IntProp : {Format(o?.IntProp)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,47 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.GrandchildA o, string extraIndent = "")
internal static string OptionsToString(this Test.GrandchildA? o, string extraIndent = "")
{
return $@"Test.GrandchildA:
{extraIndent} GrandchildSecret : {Format(o?.GrandchildSecret,prefixLen:0,suffixLen:10)}
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:0,suffixLen:6)}
";
}
internal static string OptionsToString(this Test.GrandchildB o, string extraIndent = "")
internal static string OptionsToString(this Test.GrandchildB? o, string extraIndent = "")
{
return $@"Test.GrandchildB:
{extraIndent} GrandchildSecret : {Format(o?.GrandchildSecret,prefixLen:0,suffixLen:10)}
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:0,suffixLen:6)}
";
}
internal static string OptionsToString(this Test.GrandchildC o, string extraIndent = "")
internal static string OptionsToString(this Test.GrandchildC? o, string extraIndent = "")
{
return $@"Test.GrandchildC:
{extraIndent} GrandchildSecret : {Format(o?.GrandchildSecret,prefixLen:0,suffixLen:10)}
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:0,suffixLen:6)}
";
}
internal static string OptionsToString(this Test.Parent o, string extraIndent = "")
internal static string OptionsToString(this Test.Parent? o, string extraIndent = "")
{
return $@"Test.Parent:
{extraIndent} Secret : {Format(o?.Secret,prefixLen:0,suffixLen:6)}
{extraIndent} Grandchild : {Format(o?.Grandchild,formatMethod:(o) => o.OptionsToString(" "),noQuote:true)}
{extraIndent} GrandchildB : {Format(o?.GrandchildB,formatMethod:(o) => o.OptionsToString(" "),noQuote:true)}
{extraIndent} GrandchildC : {Format(o?.GrandchildC,formatMethod:(o) => o.OptionsToString(" "),noQuote:true)}
{extraIndent} Grandchild : {Format(o?.Grandchild,formatMethod:(o) => o?.OptionsToString(" ") ?? "null",noQuote:true)}
{extraIndent} GrandchildB : {Format(o?.GrandchildB,formatMethod:(o) => o?.OptionsToString(" ") ?? "null",noQuote:true)}
{extraIndent} GrandchildC : {Format(o?.GrandchildC,formatMethod:(o) => o?.OptionsToString(" ") ?? "null",noQuote:true)}
";
}
internal static string OptionsToString(this Test.NestedOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.NestedOptions? o, string extraIndent = "")
{
return $@"Test.NestedOptions:
{extraIndent} NestedSecret : {Format(o?.NestedSecret,prefixLen:3,suffixLen:0)}
";
}
internal static string OptionsToString(this Test.ParentOfNested o, string extraIndent = "")
internal static string OptionsToString(this Test.ParentOfNested? o, string extraIndent = "")
{
return $@"Test.ParentOfNested:
{extraIndent} Secret : {Format(o?.Secret,prefixLen:3,suffixLen:0)}
{extraIndent} Nested : {Format(o?.Nested,formatMethod:(o) => o.OptionsToString(" "),noQuote:true)}
{extraIndent} Nested : {Format(o?.Nested,formatMethod:(o) => o?.OptionsToString(" ") ?? "null",noQuote:true)}
";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.PropertyChildOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.PropertyChildOptions? o, string extraIndent = "")
{
return $@"Test.PropertyChildOptions:
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:2,suffixLen:0)}
{extraIndent} ParentSecret : {Format(o?.ParentSecret,prefixLen:0,suffixLen:0)}
";
}
internal static string OptionsToString(this Test.PropertyChildOnlyOptions o, string extraIndent = "")
internal static string OptionsToString(this Test.PropertyChildOnlyOptions? o, string extraIndent = "")
{
return $@"Test.PropertyChildOnlyOptions:
{extraIndent} ChildSecret : {Format(o?.ChildSecret,prefixLen:2,suffixLen:0)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.IOptionsSimple o, string extraIndent = "")
internal static string OptionsToString(this Test.IOptionsSimple? o, string extraIndent = "")
{
return $@"Test.IOptionsSimple:
{extraIndent} Secret : {Format(o?.Secret,prefixLen:0,suffixLen:0)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.Next.Level.PropertyNamespaceTestRecord o, string extraIndent = "")
internal static string OptionsToString(this Test.Next.Level.PropertyNamespaceTestRecord? o, string extraIndent = "")
{
return $@"Custom Title {o?.NameA}:
{extraIndent}> NumberA - {Format(o?.NumberA)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.PropertyPublicOptions o, string extraIndent = "")
public static string OptionsToString(this Test.PropertyPublicOptions? o, string extraIndent = "")
{
return $@"Test.PropertyPublicOptions:
{extraIndent} PlainText : {Format(o.PlainText)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this Test.PropertyPublicOptions o, string extraIndent = "")
public static string OptionsToString(this Test.PropertyPublicOptions? o, string extraIndent = "")
{
return $@"Test.PropertyPublicOptions:
{extraIndent} PlainText : {Format(o?.PlainText)}
Expand Down Expand Up @@ -32,7 +32,7 @@ namespace Seekatar.OptionToStringGenerator
{extraIndent} Color : {Format(o?.Color)}
";
}
public static string OptionsToString(this Test.PropertyPublicOptionsSorted o, string extraIndent = "")
public static string OptionsToString(this Test.PropertyPublicOptionsSorted? o, string extraIndent = "")
{
return $@"Test.PropertyPublicOptionsSorted:
{extraIndent} AMaskedObject : {Format(o?.AMaskedObject,regex:"AClass\\:\\s+(.*)")}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
internal static string OptionsToString(this Test.PropertyTestRecord o, string extraIndent = "")
internal static string OptionsToString(this Test.PropertyTestRecord? o, string extraIndent = "")
{
return $@"Custom Title {o?.Name}:
{extraIndent}> Number - {Format(o?.Number)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Seekatar.OptionToStringGenerator
{
public static partial class ClassExtensions
{
public static string OptionsToString(this MyAppOptions o, string extraIndent = "")
public static string OptionsToString(this MyAppOptions? o, string extraIndent = "")
{
return $@"MyAppOptions:
{extraIndent} Name : {Format(o?.Name)}
Expand Down
Loading

0 comments on commit defbfa3

Please sign in to comment.