|
| 1 | +// |
| 2 | +// Copyright 2022, Optimizely, Inc. and contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import Foundation |
| 18 | + |
| 19 | +struct Holdout: Codable, ExperimentCore { |
| 20 | + enum Status: String, Codable { |
| 21 | + case draft = "Draft" |
| 22 | + case running = "Running" |
| 23 | + case concluded = "Concluded" |
| 24 | + case archived = "Archived" |
| 25 | + } |
| 26 | + |
| 27 | + var id: String |
| 28 | + var key: String |
| 29 | + var status: Status |
| 30 | + var layerId: String |
| 31 | + var variations: [Variation] |
| 32 | + var trafficAllocation: [TrafficAllocation] |
| 33 | + var audienceIds: [String] |
| 34 | + var audienceConditions: ConditionHolder? |
| 35 | + var includedFlags: [String] |
| 36 | + var excludedFlags: [String] |
| 37 | + |
| 38 | + enum CodingKeys: String, CodingKey { |
| 39 | + case id, key, status, layerId, variations, trafficAllocation, audienceIds, audienceConditions, includedFlags, excludedFlags |
| 40 | + } |
| 41 | + |
| 42 | + var variationsMap: [String: OptimizelyVariation] = [:] |
| 43 | + // replace with serialized string representation with audience names when ProjectConfig is ready |
| 44 | + var audiences: String = "" |
| 45 | + |
| 46 | + init(from decoder: Decoder) throws { |
| 47 | + let container = try decoder.container(keyedBy: CodingKeys.self) |
| 48 | + |
| 49 | + id = try container.decode(String.self, forKey: .id) |
| 50 | + key = try container.decode(String.self, forKey: .key) |
| 51 | + status = try container.decode(Status.self, forKey: .status) |
| 52 | + layerId = try container.decode(String.self, forKey: .layerId) |
| 53 | + variations = try container.decode([Variation].self, forKey: .variations) |
| 54 | + trafficAllocation = try container.decode([TrafficAllocation].self, forKey: .trafficAllocation) |
| 55 | + audienceIds = try container.decode([String].self, forKey: .audienceIds) |
| 56 | + audienceConditions = try container.decodeIfPresent(ConditionHolder.self, forKey: .audienceConditions) |
| 57 | + |
| 58 | + includedFlags = try container.decodeIfPresent([String].self, forKey: .includedFlags) ?? [] |
| 59 | + excludedFlags = try container.decodeIfPresent([String].self, forKey: .excludedFlags) ?? [] |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +extension Holdout: Equatable { |
| 64 | + static func == (lhs: Holdout, rhs: Holdout) -> Bool { |
| 65 | + return lhs.id == rhs.id && |
| 66 | + lhs.key == rhs.key && |
| 67 | + lhs.status == rhs.status && |
| 68 | + lhs.layerId == rhs.layerId && |
| 69 | + lhs.variations == rhs.variations && |
| 70 | + lhs.trafficAllocation == rhs.trafficAllocation && |
| 71 | + lhs.audienceIds == rhs.audienceIds && |
| 72 | + lhs.audienceConditions == rhs.audienceConditions && |
| 73 | + lhs.includedFlags == rhs.includedFlags && |
| 74 | + lhs.excludedFlags == rhs.excludedFlags |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +extension Holdout { |
| 79 | + var isActivated: Bool { |
| 80 | + return status == .running |
| 81 | + } |
| 82 | +} |
0 commit comments