Skip to content

Commit da4ec3e

Browse files
committed
fix: project and environment can now accept unknown keys with type Any
1 parent 6f3143f commit da4ec3e

File tree

1 file changed

+24
-11
lines changed

1 file changed

+24
-11
lines changed

DevCycle/Models/UserConfig.swift

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,21 @@ enum UserConfigError: Error {
1212
}
1313

1414
public struct UserConfig {
15-
var environment: KeyedProperty
15+
var project: Project
16+
var environment: Environment
1617
var featureVariationMap: [String: String]
1718
var features: [String: Feature]
1819
var variables: [String: Variable]
19-
var project: KeyedProperty
2020

2121
init(from dictionary: [String:Any]) throws {
22-
guard let environment = dictionary["environment"] as? [String: String] else { throw UserConfigError.MissingInConfig("environment") }
23-
guard let project = dictionary["project"] as? [String: String] else { throw UserConfigError.MissingInConfig("project") }
22+
guard let environment = dictionary["environment"] as? [String: Any] else { throw UserConfigError.MissingInConfig("environment") }
23+
guard let project = dictionary["project"] as? [String: Any] else { throw UserConfigError.MissingInConfig("project") }
2424
guard let featureVariationMap = dictionary["featureVariationMap"] as? [String: String] else { throw UserConfigError.MissingInConfig("featureVariationMap") }
2525
guard var featureMap = dictionary["features"] as? [String: Any] else { throw UserConfigError.MissingInConfig("features") }
2626
guard var variablesMap = dictionary["variables"] as? [String: Any] else { throw UserConfigError.MissingInConfig("variables") }
27-
self.environment = try KeyedProperty(from: environment, name: "environment")
28-
self.project = try KeyedProperty(from: project, name: "project")
27+
28+
self.project = try Project(from: project)
29+
self.environment = try Environment(from: environment)
2930
self.featureVariationMap = featureVariationMap
3031

3132
let featureKeys = Array(featureMap.keys)
@@ -52,15 +53,27 @@ public struct UserConfig {
5253
}
5354
}
5455

55-
public struct KeyedProperty {
56-
var key: String
56+
public struct Project {
5757
var _id: String
58+
var key: String
5859

59-
init (from dictionary: [String: String], name: String) throws {
60-
guard let key = dictionary["key"] else { throw UserConfigError.MissingProperty("key in \(name)") }
61-
guard let id = dictionary["_id"] else { throw UserConfigError.MissingProperty("_id in \(name)") }
60+
init (from dictionary: [String: Any]) throws {
61+
guard let key = dictionary["key"] as? String else { throw UserConfigError.MissingProperty("key in Project") }
62+
guard let id = dictionary["_id"] as? String else { throw UserConfigError.MissingProperty("_id in Project") }
63+
self._id = id
6264
self.key = key
65+
}
66+
}
67+
68+
public struct Environment {
69+
var _id: String
70+
var key: String
71+
72+
init (from dictionary: [String: Any]) throws {
73+
guard let key = dictionary["key"] as? String else { throw UserConfigError.MissingProperty("key in Environment") }
74+
guard let id = dictionary["_id"] as? String else { throw UserConfigError.MissingProperty("_id in Environment") }
6375
self._id = id
76+
self.key = key
6477
}
6578
}
6679

0 commit comments

Comments
 (0)