-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathProject.swift
More file actions
142 lines (122 loc) · 5.78 KB
/
Project.swift
File metadata and controls
142 lines (122 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// Copyright 2019-2021, Optimizely, Inc. and contributors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
/// Optimizely region identifiers
public enum Region: String, Codable, Equatable {
case US
case EU
}
protocol ProjectProtocol {
func evaluateAudience(audienceId: String, user: OptimizelyUserContext) throws -> Bool
}
// [REF]: datafile schema
// https://github.com/optimizely/optimizely/blob/43454b726a2a8aab7dcd953999cf8e1902b09d4d/src/www/services/datafile_generator/schema.json
struct Project: Codable, Equatable {
// V2
var version: String
var projectId: String
var experiments: [Experiment]
var audiences: [Audience]
var groups: [Group]
var attributes: [Attribute]
var accountId: String
var events: [Event]
var revision: String
// V3
var anonymizeIP: Bool
// V4
var rollouts: [Rollout]
var integrations: [Integration]?
var typedAudiences: [Audience]?
var featureFlags: [FeatureFlag]
var botFiltering: Bool?
var sendFlagDecisions: Bool?
var sdkKey: String?
var environmentKey: String?
// Holdouts
var holdouts: [Holdout]
// Region
var region: Region?
let logger = OPTLoggerFactory.getLogger()
// Required since logger is not decodable
enum CodingKeys: String, CodingKey {
// V2
case version, projectId, experiments, audiences, groups, attributes, accountId, events, revision
// V3
case anonymizeIP
// V4
case rollouts, integrations, typedAudiences, featureFlags, botFiltering, sendFlagDecisions, sdkKey, environmentKey, holdouts, region
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
// V2
version = try container.decode(String.self, forKey: .version)
projectId = try container.decode(String.self, forKey: .projectId)
experiments = try container.decode([Experiment].self, forKey: .experiments)
audiences = try container.decode([Audience].self, forKey: .audiences)
groups = try container.decode([Group].self, forKey: .groups)
attributes = try container.decode([Attribute].self, forKey: .attributes)
accountId = try container.decode(String.self, forKey: .accountId)
events = try container.decode([Event].self, forKey: .events)
revision = try container.decode(String.self, forKey: .revision)
// V3
anonymizeIP = try container.decode(Bool.self, forKey: .anonymizeIP)
// V4
rollouts = try container.decode([Rollout].self, forKey: .rollouts)
integrations = try container.decodeIfPresent([Integration].self, forKey: .integrations)
typedAudiences = try container.decodeIfPresent([Audience].self, forKey: .typedAudiences)
featureFlags = try container.decode([FeatureFlag].self, forKey: .featureFlags)
botFiltering = try container.decodeIfPresent(Bool.self, forKey: .botFiltering)
sendFlagDecisions = try container.decodeIfPresent(Bool.self, forKey: .sendFlagDecisions)
sdkKey = try container.decodeIfPresent(String.self, forKey: .sdkKey)
environmentKey = try container.decodeIfPresent(String.self, forKey: .environmentKey)
// Holdouts - defaults to empty array if key is not present
holdouts = try container.decodeIfPresent([Holdout].self, forKey: .holdouts) ?? []
// Region - defaults to US if not present
region = try container.decodeIfPresent(Region.self, forKey: .region)
}
// Required since logger is not equatable
static func == (lhs: Project, rhs: Project) -> Bool {
return lhs.version == rhs.version && lhs.projectId == rhs.projectId && lhs.experiments == rhs.experiments && lhs.holdouts == rhs.holdouts &&
lhs.audiences == rhs.audiences && lhs.groups == rhs.groups && lhs.attributes == rhs.attributes &&
lhs.accountId == rhs.accountId && lhs.events == rhs.events && lhs.revision == rhs.revision &&
lhs.anonymizeIP == rhs.anonymizeIP && lhs.rollouts == rhs.rollouts &&
lhs.integrations == rhs.integrations && lhs.typedAudiences == rhs.typedAudiences &&
lhs.featureFlags == rhs.featureFlags && lhs.botFiltering == rhs.botFiltering &&
lhs.sendFlagDecisions == rhs.sendFlagDecisions && lhs.sdkKey == rhs.sdkKey &&
lhs.environmentKey == rhs.environmentKey && lhs.region == rhs.region
}
}
extension Project: ProjectProtocol {
func evaluateAudience(audienceId: String, user: OptimizelyUserContext) throws -> Bool {
guard let audience = getAudience(id: audienceId) else {
throw OptimizelyError.conditionNoMatchingAudience(audienceId)
}
logger.d { () -> String in
return LogMessage.audienceEvaluationStarted(audienceId, Utils.getConditionString(conditions: audience.conditionHolder)).description
}
let result = try audience.evaluate(project: self, user: user)
logger.d(.audienceEvaluationResult(audienceId, result.description))
return result
}
}
// MARK: - Utils
extension Project {
func getAudience(id: String) -> Audience? {
return typedAudiences?.filter { $0.id == id }.first ??
audiences.filter { $0.id == id }.first
}
}