-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: get variation details by variation type (#90)
- Loading branch information
1 parent
0b6fa89
commit 5a79914
Showing
15 changed files
with
1,611 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import Foundation | ||
|
||
public struct BKTEvaluationDetails<T:Equatable>: Equatable { | ||
public let featureId: String | ||
public let featureVersion: Int | ||
public let userId: String | ||
public let variationId: String | ||
public let variationName: String | ||
public let variationValue: T | ||
public let reason: Reason | ||
|
||
public enum Reason: String, Codable, Hashable { | ||
case target = "TARGET" | ||
case rule = "RULE" | ||
case `default` = "DEFAULT" | ||
case client = "CLIENT" | ||
case offVariation = "OFF_VARIATION" | ||
case prerequisite = "PREREQUISITE" | ||
|
||
public static func fromString(value: String) -> Reason { | ||
return Reason(rawValue: value) ?? .client | ||
} | ||
} | ||
|
||
public static func == (lhs: BKTEvaluationDetails<T>, rhs: BKTEvaluationDetails<T>) -> Bool { | ||
return lhs.featureId == rhs.featureId && | ||
lhs.featureVersion == rhs.featureVersion && | ||
lhs.userId == rhs.userId && | ||
lhs.variationId == rhs.variationId && | ||
lhs.variationName == rhs.variationName && | ||
lhs.reason == rhs.reason && | ||
lhs.variationValue == rhs.variationValue | ||
} | ||
|
||
static func newDefaultInstance(featureId: String, userId: String, defaultValue: T) -> BKTEvaluationDetails<T> { | ||
return BKTEvaluationDetails( | ||
featureId: featureId, | ||
featureVersion: 0, | ||
userId: userId, | ||
variationId: "", | ||
variationName: "", | ||
variationValue: defaultValue, | ||
reason: .client | ||
) | ||
} | ||
|
||
public init(featureId: String, | ||
featureVersion: Int, | ||
userId: String, | ||
variationId: String, | ||
variationName: String, | ||
variationValue: T, | ||
reason: Reason) { | ||
self.featureId = featureId | ||
self.featureVersion = featureVersion | ||
self.userId = userId | ||
self.variationId = variationId | ||
self.variationName = variationName | ||
self.variationValue = variationValue | ||
self.reason = reason | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import Foundation | ||
|
||
/** | ||
* BKTValue Represents a JSON node value | ||
*/ | ||
public enum BKTValue: Equatable, Codable, Hashable { | ||
case boolean(Bool) | ||
case string(String) | ||
case number(Double) | ||
case list([BKTValue]) | ||
case dictionary([String: BKTValue]) | ||
case null | ||
|
||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
if let stringValue = try? container.decode(String.self) { | ||
self = .string(stringValue) | ||
} else if let doubleValue = try? container.decode(Double.self) { | ||
self = .number(doubleValue) | ||
} else if let boolValue = try? container.decode(Bool.self) { | ||
self = .boolean(boolValue) | ||
} else if let objectValue = try? container.decode([String: BKTValue].self) { | ||
self = .dictionary(objectValue) | ||
} else if let arrayValue = try? container.decode([BKTValue].self) { | ||
self = .list(arrayValue) | ||
} else if container.decodeNil() { | ||
self = .null | ||
} else { | ||
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Cannot decode BKTValue") | ||
} | ||
} | ||
|
||
// Encode the JSON based on its type | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
switch self { | ||
case .string(let value): | ||
try container.encode(value) | ||
case .number(let value): | ||
try container.encode(value) | ||
case .dictionary(let value): | ||
try container.encode(value) | ||
case .list(let value): | ||
try container.encode(value) | ||
case .boolean(let value): | ||
try container.encode(value) | ||
case .null: | ||
try container.encodeNil() | ||
} | ||
} | ||
|
||
public func asBoolean() -> Bool? { | ||
if case let .boolean(bool) = self { | ||
return bool | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func asString() -> String? { | ||
if case let .string(string) = self { | ||
return string | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func asInteger() -> Int? { | ||
if case let .number(double) = self { | ||
return Int(double) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func asDouble() -> Double? { | ||
if case let .number(double) = self { | ||
return double | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func asList() -> [BKTValue]? { | ||
if case let .list(values) = self { | ||
return values | ||
} | ||
|
||
return nil | ||
} | ||
|
||
public func asDictionary() -> [String: BKTValue]? { | ||
if case let .dictionary(values) = self { | ||
return values | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
extension BKTValue: CustomStringConvertible { | ||
public var description: String { | ||
switch self { | ||
case .boolean(let value): | ||
return "\(value)" | ||
case .string(let value): | ||
return value | ||
case .number(let value): | ||
return "\(value)" | ||
case .list(value: let values): | ||
return "\(values.map { value in value.description })" | ||
case .dictionary(value: let values): | ||
return "\(values.mapValues { value in value.description })" | ||
case .null: | ||
return "null" | ||
} | ||
} | ||
} |
Oops, something went wrong.