@@ -12,14 +12,14 @@ namespace SqlStreamStore.Server
1212{
1313 internal class SqlStreamStoreServerConfiguration
1414 {
15- private readonly IConfigurationRoot _configuration ;
15+ private readonly ConfigurationData _configuration ;
1616 private Dictionary < string , ( string source , string value ) > _values ;
1717
18- public bool UseCanonicalUris => _configuration . GetValue < bool > ( "use-canonical-uris" ) ;
19- public LogEventLevel LogLevel => _configuration . GetValue ( "log-level" , LogEventLevel . Information ) ;
20- public string ConnectionString => _configuration . GetValue < string > ( "connection-string" ) ;
21- public string Schema => _configuration . GetValue < string > ( "schema" ) ;
22- public string Provider => _configuration . GetValue < string > ( "provider" ) ;
18+ public bool UseCanonicalUris => _configuration . UseCanonicalUris ;
19+ public LogEventLevel LogLevel => _configuration . LogLevel ;
20+ public string ConnectionString => _configuration . ConnectionString ;
21+ public string Schema => _configuration . Schema ;
22+ public string Provider => _configuration . Provider ;
2323
2424 public SqlStreamStoreServerConfiguration (
2525 IDictionary environment ,
@@ -37,42 +37,98 @@ void Log(string logName, IDictionary<string, string> data)
3737 foreach ( var ( key , value ) in data )
3838 {
3939 _values [ key ] = ( logName , value ) ;
40- Console . WriteLine ( $ "{ key } came from { logName } ") ;
4140 }
4241 }
4342
44- _configuration = new ConfigurationBuilder ( )
45- . Add ( new CommandLineConfigurationSource ( args , Log ) )
46- . Add ( new EnvironmentVariablesConfigurationSource ( environment , Log ) )
47- . Build ( ) ;
43+ _configuration = new ConfigurationData (
44+ new ConfigurationBuilder ( )
45+ . Add ( new DefaultConfigurationSource ( Log ) )
46+ . Add ( new CommandLineConfigurationSource ( args , Log ) )
47+ . Add ( new EnvironmentVariablesConfigurationSource ( environment , Log ) )
48+ . Build ( ) ) ;
4849 }
4950
5051 public override string ToString ( )
5152 {
5253 const string delimiter = " | " ;
5354
54- var column0Width = _values . Keys . Count > 0 ? _values . Keys . Max ( x => x . Length ) : 0 ;
55- var column1Width = _values . Values . Count > 0 ? _values . Values . Max ( _ => _ . value . Length ) : 0 ;
56- var column2Witdth = _values . Values . Count > 0 ? _values . Values . Max ( _ => _ . source . Length ) : 0 ;
55+ var column0Width = _values . Keys . Count > 0 ? _values . Keys . Max ( x => x ? . Length ?? 0 ) : 0 ;
56+ var column1Width = _values . Values . Count > 0 ? _values . Values . Max ( _ => _ . value ? . Length ?? 0 ) : 0 ;
57+ var column2Width = _values . Values . Count > 0 ? _values . Values . Max ( _ => _ . source ? . Length ?? 0 ) : 0 ;
5758
5859 StringBuilder Append ( StringBuilder builder , string column0 , string column1 , string column2 ) =>
5960 builder
60- . Append ( column0 . PadRight ( column0Width , ' ' ) )
61+ . Append ( ( column0 ?? string . Empty ) . PadRight ( column0Width , ' ' ) )
6162 . Append ( delimiter )
62- . Append ( column1 . PadRight ( column1Width , ' ' ) )
63+ . Append ( ( column1 ?? string . Empty ) . PadRight ( column1Width , ' ' ) )
6364 . Append ( delimiter )
6465 . AppendLine ( column2 ) ;
6566
6667 return _values . Keys . OrderBy ( x => x ) . Aggregate (
6768 Append ( new StringBuilder ( ) . AppendLine ( "SQL Stream Store Configuration:" ) , "Argument" , "Value" , "Source" )
68- . AppendLine ( new string ( '-' , column0Width + column1Width + column2Witdth ) ) ,
69+ . AppendLine ( new string ( '-' , column0Width + column1Width + column2Width ) ) ,
6970 ( builder , key ) => Append (
7071 builder ,
7172 key ,
7273 _values [ key ] . value ,
7374 _values [ key ] . source ) ) . ToString ( ) ;
7475 }
7576
77+ private static string Computerize ( string value ) =>
78+ string . Join (
79+ string . Empty ,
80+ ( value ? . Replace ( "-" , "_" ) . ToLowerInvariant ( )
81+ ?? string . Empty ) . Split ( '_' )
82+ . Select ( x => new string ( x . Select ( ( c , i ) => i == 0 ? char . ToUpper ( c ) : c ) . ToArray ( ) ) ) ) ;
83+
84+ private class ConfigurationData
85+ {
86+ private readonly IConfigurationRoot _configuration ;
87+
88+ public bool UseCanonicalUris => _configuration . GetValue < bool > ( nameof ( UseCanonicalUris ) ) ;
89+ public LogEventLevel LogLevel => _configuration . GetValue ( nameof ( LogLevel ) , LogEventLevel . Information ) ;
90+ public string ConnectionString => _configuration . GetValue < string > ( nameof ( ConnectionString ) ) ;
91+ public string Schema => _configuration . GetValue < string > ( nameof ( Schema ) ) ;
92+ public string Provider => _configuration . GetValue < string > ( nameof ( Provider ) ) ;
93+
94+ public ConfigurationData ( IConfigurationRoot configuration )
95+ {
96+ if ( configuration == null ) throw new ArgumentNullException ( nameof ( configuration ) ) ;
97+ _configuration = configuration ;
98+ }
99+ }
100+
101+ private class DefaultConfigurationSource : ConfigurationProvider , IConfigurationSource
102+ {
103+ private readonly Action < string , IDictionary < string , string > > _log ;
104+
105+ public DefaultConfigurationSource ( Action < string , IDictionary < string , string > > log )
106+ {
107+ if ( log == null )
108+ {
109+ throw new ArgumentNullException ( nameof ( log ) ) ;
110+ }
111+
112+ _log = log ;
113+ }
114+
115+ public IConfigurationProvider Build ( IConfigurationBuilder builder ) => this ;
116+
117+ public override void Load ( )
118+ {
119+ Data = new Dictionary < string , string >
120+ {
121+ [ nameof ( ConnectionString ) ] = default ,
122+ [ nameof ( Provider ) ] = "inmemory" ,
123+ [ nameof ( LogLevel ) ] = nameof ( LogEventLevel . Information ) ,
124+ [ nameof ( Schema ) ] = default ,
125+ [ nameof ( UseCanonicalUris ) ] = default
126+ } ;
127+
128+ _log ( nameof ( DefaultConfigurationSource ) , Data ) ;
129+ }
130+ }
131+
76132 private class CommandLineConfigurationSource : IConfigurationSource
77133 {
78134 private readonly IEnumerable < string > _args ;
@@ -121,6 +177,8 @@ public override void Load()
121177 {
122178 base . Load ( ) ;
123179
180+ Data = Data . Keys . ToDictionary ( Computerize , x => Data [ x ] ) ;
181+
124182 _log ( nameof ( CommandLineConfigurationSource ) , Data ) ;
125183 }
126184 }
@@ -186,7 +244,7 @@ public override void Load()
186244 where key . StartsWith ( _prefix )
187245 select new
188246 {
189- key = key . Remove ( 0 , _prefix . Length ) . Replace ( '_' , '-' ) . ToLowerInvariant ( ) ,
247+ key = Computerize ( key . Remove ( 0 , _prefix . Length ) ) ,
190248 value = ( string ) entry . Value
191249 } )
192250 . ToDictionary ( x => x . key , x => x . value ) ;
0 commit comments