@@ -18,52 +18,52 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1818 IncrementalValuesProvider < AdditionalText > textFiles =
1919 context . AdditionalTextsProvider . Where ( static file => Path . GetFileName ( file . Path ) . Equals ( "appsettings.json" ) ) ;
2020
21- IncrementalValuesProvider < ( List < SettingClassInfo > ? Classes , bool GenerateOptions , Diagnostic ? Diagnostic ) > settingsAndOptionsProvider =
21+ IncrementalValuesProvider < ( List < SettingClassInfo > ? Classes , SetSharpSettings SetSharpOptions , Diagnostic ? Diagnostic ) > settingsAndOptionsProvider =
2222 textFiles . Select ( ( text , cancellationToken ) =>
2323 {
2424 var content = text . GetText ( cancellationToken ) ;
2525 if ( content is null )
2626 {
27- return ( ( List < SettingClassInfo > ? ) null , true , null ) ;
27+ return ( ( List < SettingClassInfo > ? ) null , new SetSharpSettings ( ) , null ) ;
2828 }
2929
3030 try
3131 {
3232 var json = SetSharpJsonParser . Parse ( content . ToString ( ) ) ;
3333
34- bool generateOptions = ReadSetSharpSettingsIfProvided ( json ) ;
34+ var setSharpOptions = ReadSetSharpSettings ( json ) ;
3535
3636 var modelBuilder = new ConfigurationModelBuilder ( ) ;
3737 var classes = modelBuilder . BuildFrom ( json ) ;
38- return ( classes , generateOptions , ( Diagnostic ? ) null ) ;
38+ return ( classes , setSharpOptions , ( Diagnostic ? ) null ) ;
3939 }
4040 catch ( Exception e )
4141 {
4242 var diagnostic = Diagnostic . Create ( DiagnosticDescriptors . ParsingFailedError , Location . None , e . Message ) ;
43- return ( ( List < SettingClassInfo > ? ) null , true , diagnostic ) ;
43+ return ( ( List < SettingClassInfo > ? ) null , new SetSharpSettings ( ) , diagnostic ) ;
4444 }
4545 } ) ;
4646
4747 var combinedProvider = settingsAndOptionsProvider . Combine ( context . CompilationProvider ) ;
4848
4949 var finalProvider = combinedProvider . Select ( ( source , cancellationToken ) =>
5050 {
51- var ( ( classes , generateOptions , diagnostic ) , compilation ) = source ;
51+ var ( ( classes , setSharpOptions , diagnostic ) , compilation ) = source ;
5252
5353 // If parsing already failed, just pass the error through.
5454 if ( diagnostic != null )
5555 {
56- return new SourceGenerationModel ( null , false , diagnostic ) ;
56+ return new SourceGenerationModel ( null , setSharpOptions , diagnostic ) ;
5757 }
5858
59- var dependencyDiagnostic = CheckDependencies ( compilation , generateOptions ) ;
59+ var dependencyDiagnostic = CheckDependencies ( compilation , setSharpOptions . OptionPatternGenerationEnabled ) ;
6060
6161 if ( dependencyDiagnostic != null )
6262 {
63- return new SourceGenerationModel ( classes , generateOptions , dependencyDiagnostic ) ;
63+ return new SourceGenerationModel ( classes , setSharpOptions , dependencyDiagnostic ) ;
6464 }
6565
66- return new SourceGenerationModel ( classes , generateOptions , null ) ;
66+ return new SourceGenerationModel ( classes , setSharpOptions , null ) ;
6767 } ) ;
6868
6969 context . RegisterSourceOutput ( finalProvider , ( spc , model ) =>
@@ -82,7 +82,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
8282 spc . AddSource ( "AppSettings.g.cs" , SourceText . From ( pocoSourceCode , Encoding . UTF8 ) ) ;
8383 }
8484
85- if ( model . GenerateOptions && model . Classes is { Count : > 0 } )
85+ if ( model . SetSharpSettings . OptionPatternGenerationEnabled && model . Classes is { Count : > 0 } )
8686 {
8787 var extensionsSourceCode = OptionsPatternGenerator . Generate ( model . Classes ) ;
8888 spc . AddSource ( "OptionsExtensions.g.cs" , SourceText . From ( extensionsSourceCode , Encoding . UTF8 ) ) ;
@@ -91,20 +91,21 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
9191 } ) ;
9292 }
9393
94- private static bool ReadSetSharpSettingsIfProvided ( Dictionary < string , object > json )
94+ private static SetSharpSettings ReadSetSharpSettings ( Dictionary < string , object > json )
9595 {
96- if ( json . TryGetValue ( "SetSharp" , out var setSharpObject )
97- && setSharpObject is Dictionary < string , object > setSharpDict )
96+ var setSharpOptions = new SetSharpSettings ( ) ;
97+ var settingOption = SetSharpJsonReader . Read ( json , "SetSharp" ) ;
98+ if ( settingOption is not null and Dictionary < string , object > setSharpDict )
9899 {
100+
99101 if ( setSharpDict . TryGetValue ( "OptionPatternGenerationEnabled" , out var enabledValue )
100102 && bool . TryParse ( enabledValue ? . ToString ( ) , out var parsedBool ) )
101103 {
102- return parsedBool ;
104+ setSharpOptions . OptionPatternGenerationEnabled = parsedBool ;
103105 }
104- }
105106
106- // Default to true if the setting is not present.
107- return true ;
107+ }
108+ return setSharpOptions ;
108109 }
109110
110111 /// <summary>
0 commit comments