@@ -7,7 +7,7 @@ namespace codeRR.Server.Infrastructure.Configuration
7
7
/// <summary>
8
8
/// Moves otherwise repeated conversions to a single place.
9
9
/// </summary>
10
- public static class DictionaryExtensions
10
+ public static class ConfigDictionaryExtensions
11
11
{
12
12
/// <summary>
13
13
/// Convert dictionary item to a boolean.
@@ -17,19 +17,25 @@ public static class DictionaryExtensions
17
17
/// <returns>Value</returns>
18
18
/// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
19
19
/// <exception cref="FormatException">Value is not a boolean. Includes key name and source value in the exception message.</exception>
20
- public static bool GetBoolean ( this IDictionary < string , string > dictionary , string name )
20
+ public static bool GetBoolean ( this IDictionary < string , string > dictionary , string name , bool ? defaultValue = false )
21
21
{
22
22
if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
23
23
if ( name == null ) throw new ArgumentNullException ( "name" ) ;
24
- string value ;
25
- if ( ! dictionary . TryGetValue ( name , out value ) )
26
- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
27
-
28
- bool boolValue ;
29
- if ( ! bool . TryParse ( value , out boolValue ) )
30
- throw new FormatException ( string . Format ( "Failed to convert '{0}' from value '{1}' to a boolean." , name ,
31
- value ) ) ;
32
- return boolValue ;
24
+
25
+ if ( ! dictionary . TryGetValue ( name , out var value ) )
26
+ {
27
+ if ( defaultValue != null )
28
+ return defaultValue . Value ;
29
+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
30
+ }
31
+
32
+ if ( bool . TryParse ( value , out var boolValue ) )
33
+ return boolValue ;
34
+
35
+ if ( defaultValue != null )
36
+ return defaultValue . Value ;
37
+
38
+ throw new FormatException ( $ "Failed to convert '{ name } ' from value '{ value } ' to a boolean.") ;
33
39
}
34
40
35
41
/// <summary>
@@ -41,19 +47,26 @@ public static bool GetBoolean(this IDictionary<string, string> dictionary, strin
41
47
/// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
42
48
/// <exception cref="FormatException">Value is not a boolean. Includes key name and source value in the exception message.</exception>
43
49
[ SuppressMessage ( "Microsoft.Naming" , "CA1720:IdentifiersShouldNotContainTypeNames" , MessageId = "integer" ) ]
44
- public static int GetInteger ( this IDictionary < string , string > dictionary , string name )
50
+ public static int GetInteger ( this IDictionary < string , string > dictionary , string name , int ? defaultValue = 0 )
45
51
{
46
52
if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
47
53
if ( name == null ) throw new ArgumentNullException ( "name" ) ;
48
- string value ;
49
- if ( ! dictionary . TryGetValue ( name , out value ) )
50
- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
51
-
52
- int intValue ;
53
- if ( ! int . TryParse ( value , out intValue ) )
54
- throw new FormatException ( string . Format ( "Failed to convert '{0}' from value '{1}' to an integer." ,
55
- name , value ) ) ;
56
- return intValue ;
54
+
55
+ if ( ! dictionary . TryGetValue ( name , out var value ) )
56
+ {
57
+ if ( defaultValue != null )
58
+ return defaultValue . Value ;
59
+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
60
+ }
61
+
62
+
63
+ if ( int . TryParse ( value , out var intValue ) )
64
+ return intValue ;
65
+
66
+ if ( defaultValue != null )
67
+ return defaultValue . Value ;
68
+
69
+ throw new FormatException ( $ "Failed to convert '{ name } ' from value '{ value } ' to an integer.") ;
57
70
}
58
71
59
72
/// <summary>
@@ -63,15 +76,18 @@ public static int GetInteger(this IDictionary<string, string> dictionary, string
63
76
/// <param name="name">Key</param>
64
77
/// <returns>Value</returns>
65
78
/// <exception cref="ArgumentException">If key is not present. Key name is included in the exception message.</exception>
66
- public static string GetString ( this IDictionary < string , string > dictionary , string name )
79
+ public static string GetString ( this IDictionary < string , string > dictionary , string name , bool requireParameter = true )
67
80
{
68
81
if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
69
82
if ( name == null ) throw new ArgumentNullException ( "name" ) ;
70
- string value ;
71
- if ( ! dictionary . TryGetValue ( name , out value ) )
72
- throw new ArgumentException ( string . Format ( "Failed to find key '{0}' in dictionary." , name ) ) ;
73
83
74
- return value ;
84
+ if ( dictionary . TryGetValue ( name , out var value ) )
85
+ return value ;
86
+
87
+ if ( requireParameter )
88
+ throw new ArgumentException ( $ "Failed to find key '{ name } ' in dictionary.") ;
89
+
90
+ return null ;
75
91
}
76
92
77
93
/// <summary>
@@ -86,8 +102,9 @@ public static string GetString(this IDictionary<string, string> dictionary, stri
86
102
{
87
103
if ( dictionary == null ) throw new ArgumentNullException ( "dictionary" ) ;
88
104
if ( name == null ) throw new ArgumentNullException ( "name" ) ;
89
- string value ;
90
- return ! dictionary . TryGetValue ( name , out value ) ? defaultValue : value ;
105
+ return ! dictionary . TryGetValue ( name , out var value )
106
+ ? defaultValue
107
+ : value ;
91
108
}
92
109
}
93
110
}
0 commit comments