Skip to content

Commit a4ddaa9

Browse files
committed
Add custom configuration objects
1 parent a2aa602 commit a4ddaa9

3 files changed

Lines changed: 68 additions & 12 deletions

File tree

Sources/ExtendedConfiguration/Application+Settings.swift

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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? {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
internal struct AppModel {
2+
var name: String
3+
var version: String
4+
}

Tests/ExtendedConfigurationTests/ExtendedConfigurationTests.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,36 @@ final class ExtendedConfigurationTests: XCTestCase {
6262
XCTAssertEqual("stringValue03", app.settings.getString(for: "group02.key03"))
6363
XCTAssertEqual(321, app.settings.getInt(for: "group02.key04"))
6464
}
65+
66+
func testCustomConfigurationShouldBeReturnedForKey() throws {
67+
// Arrange.
68+
try createSettingsFile(fileName: "configuration.json")
69+
let app = Application(.testing)
70+
defer { app.shutdown() }
71+
72+
// Act.
73+
app.settings.set(AppModel(name: "name01", version: "1.0.0"), forKey: "customkey")
74+
let appModel = app.settings.get(AppModel.self, for: "customkey")
75+
76+
// Assert.
77+
XCTAssertEqual("name01", appModel?.name)
78+
XCTAssertEqual("1.0.0", appModel?.version)
79+
}
80+
81+
func testCustomConfigurationShouldBeReturnedForType() throws {
82+
// Arrange.
83+
try createSettingsFile(fileName: "configuration.json")
84+
let app = Application(.testing)
85+
defer { app.shutdown() }
86+
87+
// Act.
88+
app.settings.set(AppModel(name: "name02", version: "2.0.0"), for: AppModel.self)
89+
let appModel = app.settings.get(AppModel.self)
90+
91+
// Assert.
92+
XCTAssertEqual("name02", appModel?.name)
93+
XCTAssertEqual("2.0.0", appModel?.version)
94+
}
6595

6696
private func createSettingsFile(fileName: String) throws {
6797
let content = """

0 commit comments

Comments
 (0)