Skip to content

Commit 3560de9

Browse files
wip: make holdouts array empty if no key presented
1 parent 5404d97 commit 3560de9

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Sources/Data Model/Project.swift

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ struct Project: Codable, Equatable {
4747
var sdkKey: String?
4848
var environmentKey: String?
4949
// Holdouts
50-
// This would be non optional once API is completed
51-
var holdouts: [Holdout]?
50+
var holdouts: [Holdout]
5251
let logger = OPTLoggerFactory.getLogger()
5352

5453
// Required since logger is not decodable
@@ -63,6 +62,37 @@ struct Project: Codable, Equatable {
6362
case holdouts
6463
}
6564

65+
init(from decoder: Decoder) throws {
66+
let container = try decoder.container(keyedBy: CodingKeys.self)
67+
68+
// V2
69+
version = try container.decode(String.self, forKey: .version)
70+
projectId = try container.decode(String.self, forKey: .projectId)
71+
experiments = try container.decode([Experiment].self, forKey: .experiments)
72+
audiences = try container.decode([Audience].self, forKey: .audiences)
73+
groups = try container.decode([Group].self, forKey: .groups)
74+
attributes = try container.decode([Attribute].self, forKey: .attributes)
75+
accountId = try container.decode(String.self, forKey: .accountId)
76+
events = try container.decode([Event].self, forKey: .events)
77+
revision = try container.decode(String.self, forKey: .revision)
78+
79+
// V3
80+
anonymizeIP = try container.decode(Bool.self, forKey: .anonymizeIP)
81+
82+
// V4
83+
rollouts = try container.decode([Rollout].self, forKey: .rollouts)
84+
integrations = try container.decodeIfPresent([Integration].self, forKey: .integrations)
85+
typedAudiences = try container.decodeIfPresent([Audience].self, forKey: .typedAudiences)
86+
featureFlags = try container.decode([FeatureFlag].self, forKey: .featureFlags)
87+
botFiltering = try container.decodeIfPresent(Bool.self, forKey: .botFiltering)
88+
sendFlagDecisions = try container.decodeIfPresent(Bool.self, forKey: .sendFlagDecisions)
89+
sdkKey = try container.decodeIfPresent(String.self, forKey: .sdkKey)
90+
environmentKey = try container.decodeIfPresent(String.self, forKey: .environmentKey)
91+
92+
// Holdouts - defaults to empty array if key is not present
93+
holdouts = try container.decodeIfPresent([Holdout].self, forKey: .holdouts) ?? []
94+
}
95+
6696
// Required since logger is not equatable
6797
static func == (lhs: Project, rhs: Project) -> Bool {
6898
return lhs.version == rhs.version && lhs.projectId == rhs.projectId && lhs.experiments == rhs.experiments && lhs.holdouts == rhs.holdouts &&

Sources/Data Model/ProjectConfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class ProjectConfig {
7878

7979
holdoutIdMap = {
8080
var map = [String : Holdout]()
81-
project.holdouts?.forEach { map[$0.id] = $0 }
81+
project.holdouts.forEach { map[$0.id] = $0 }
8282
return map
8383
}()
8484

0 commit comments

Comments
 (0)