@@ -24,10 +24,10 @@ extension Application {
2424 }
2525
2626 public struct Configuration {
27- public var items : [ String : Any ] = [ : ]
28-
29- public mutating func appendOrReplace ( key : String , value : Any ) {
30- self . items [ key ] = value
27+ internal var items : [ String : Any ] = [ : ]
28+
29+ public subscript ( index : String ) -> Any ? {
30+ self . items [ index ]
3131 }
3232 }
3333
@@ -63,7 +63,7 @@ extension Application {
6363 }
6464
6565 jsonObject. forEach { ( key: String , value: Any ) in
66- self . append ( jsonKey : key , jsonValue : value )
66+ self . set ( value , forKey : key )
6767 }
6868
6969 break
@@ -73,27 +73,49 @@ extension Application {
7373 case . withPrefix( let prefix) :
7474 ProcessInfo . processInfo. environment. forEach { ( key: String , value: String ) in
7575 if key. starts ( with: prefix) {
76- self . configuration. appendOrReplace ( key: key , value: value )
76+ self . configuration. items [ key] = value
7777 }
7878 }
7979 default :
8080 ProcessInfo . processInfo. environment. forEach { ( key: String , value: String ) in
81- self . configuration. appendOrReplace ( key: key , value: value )
81+ self . configuration. items [ key] = value
8282 }
8383 }
8484
8585 break
8686 }
8787 }
88-
89- private func append ( jsonKey : String , jsonValue : Any ) {
90- if let jsonObject = jsonValue as? [ String : Any ] {
88+
89+ public func set ( _ setting : Any , forKey jsonKey : String ) {
90+ if let jsonObject = setting as? [ String : Any ] {
9191 jsonObject. forEach { ( key: String , value: Any ) in
92- self . append ( jsonKey : jsonKey + " . " + key, jsonValue : value )
92+ self . set ( value , forKey : jsonKey + " . " + key)
9393 }
9494 } else {
95- self . configuration. appendOrReplace ( key: jsonKey, value: jsonValue)
95+ self . configuration. items [ jsonKey] = setting
96+ }
97+ }
98+
99+ public func set< T> ( _ setting: Any , for type: T . Type ) {
100+ let key = String ( describing: T . self)
101+ self . set ( setting, forKey: key)
102+ }
103+
104+ public func get< T> ( _ type: T . Type ) -> T ? {
105+ let key = String ( describing: T . self)
106+ return self . get ( type, for: key)
107+ }
108+
109+ public func get< T> ( _ type: T . Type , for key: String ) -> T ? {
110+ return self . configuration. items [ key] as? T
111+ }
112+
113+ public func get< T> ( _ type: T . Type , for key: String , withDefault defaultValue: T ) -> T ? {
114+ if let value = self . configuration. items [ key] as? T {
115+ return value
96116 }
117+
118+ return defaultValue
97119 }
98120
99121 public func getString( for key: String ) -> String ? {
0 commit comments