Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update Evaluation struct to conform to encodable. Update logger. #34

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 98 additions & 43 deletions Sources/FeaturevisorSDK/Instance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,30 @@ public enum EvaluationReason: String {
case error
}

public struct Evaluation {
public struct Evaluation: Codable {
private enum CodingKeys: String, CodingKey {
PatrykPiwowarczyk marked this conversation as resolved.
Show resolved Hide resolved
case featureKey
case reason
case bucketValue
case ruleKey
case enabled
case traffic
case sticky
case initial
case variation
case variationValue
case variableKey
case variableValue
case variableSchema
}

// required
public let featureKey: FeatureKey
public let reason: EvaluationReason

// common
public let bucketValue: BucketValue?
public let ruleKey: RuleKey?
public let error: Error?
public let enabled: Bool?
public let traffic: Traffic?
public let sticky: OverrideFeature?
Expand All @@ -51,25 +66,24 @@ public struct Evaluation {
public let variableSchema: VariableSchema?

public init(
featureKey: FeatureKey,
reason: EvaluationReason,
bucketValue: BucketValue? = nil,
ruleKey: RuleKey? = nil,
error: Error? = nil,
enabled: Bool? = nil,
traffic: Traffic? = nil,
sticky: OverrideFeature? = nil,
initial: OverrideFeature? = nil,
variation: Variation? = nil,
variationValue: VariationValue? = nil,
variableKey: VariableKey? = nil,
variableValue: VariableValue? = nil,
variableSchema: VariableSchema? = nil) {
featureKey: FeatureKey,
reason: EvaluationReason,
bucketValue: BucketValue? = nil,
ruleKey: RuleKey? = nil,
enabled: Bool? = nil,
traffic: Traffic? = nil,
sticky: OverrideFeature? = nil,
initial: OverrideFeature? = nil,
variation: Variation? = nil,
variationValue: VariationValue? = nil,
variableKey: VariableKey? = nil,
variableValue: VariableValue? = nil,
variableSchema: VariableSchema? = nil
) {
PatrykPiwowarczyk marked this conversation as resolved.
Show resolved Hide resolved
self.featureKey = featureKey
self.reason = reason
self.bucketValue = bucketValue
self.ruleKey = ruleKey
self.error = error
self.enabled = enabled
self.traffic = traffic
self.sticky = sticky
Expand All @@ -80,6 +94,47 @@ public struct Evaluation {
self.variableValue = variableValue
self.variableSchema = variableSchema
}

public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)

try container.encode(featureKey, forKey: .featureKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All fields which are optional should be encoded using encodeIfPresent

try container.encode(reason.rawValue, forKey: .reason)
try container.encode(bucketValue, forKey: .bucketValue)
try container.encode(ruleKey, forKey: .ruleKey)
try container.encode(enabled, forKey: .enabled)
try container.encode(traffic, forKey: .traffic)
try container.encode(sticky, forKey: .sticky)
try container.encode(initial, forKey: .initial)
try container.encode(variation, forKey: .variation)
try container.encode(variationValue, forKey: .variationValue)
try container.encode(variableKey, forKey: .variableKey)
try container.encode(variableValue, forKey: .variableValue)
try container.encode(variableSchema, forKey: .variableSchema)
}

public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

featureKey = try container.decode(FeatureKey.self, forKey: .featureKey)
PatrykPiwowarczyk marked this conversation as resolved.
Show resolved Hide resolved
reason = try EvaluationReason(rawValue: container.decode(String.self, forKey: .reason)) ?? .error
bucketValue = try container.decodeIfPresent(BucketValue.self, forKey: .bucketValue)
ruleKey = try? container.decodeIfPresent(RuleKey.self, forKey: .ruleKey)
enabled = try? container.decodeIfPresent(Bool.self, forKey: .enabled)
traffic = try? container.decodeIfPresent(Traffic.self, forKey: .traffic)
sticky = try? container.decodeIfPresent(OverrideFeature.self, forKey: .sticky)
initial = try? container.decodeIfPresent(OverrideFeature.self, forKey: .initial)
variation = try? container.decodeIfPresent(Variation.self, forKey: .variation)
variationValue = try? container.decodeIfPresent(VariationValue.self, forKey: .variationValue)
variableKey = try? container.decodeIfPresent(VariableKey.self, forKey: .variableKey)
variableValue = try? container.decodeIfPresent(VariableValue.self, forKey: .variableValue)
variableSchema = try? container.decodeIfPresent(VariableSchema.self, forKey: .variableSchema)
}

func toDictionary() -> [String: Any] {
guard let data = try? JSONEncoder().encode(self) else { return [:] }
return (try? JSONSerialization.jsonObject(with: data, options: .allowFragments)).flatMap { $0 as? [String: Any]} ?? [:]
}
}

let emptyDatafile = DatafileContent(
Expand Down Expand Up @@ -387,7 +442,7 @@ public class FeaturevisorInstance {
enabled: stickyFeature.enabled,
sticky: stickyFeature)

logger.debug("using sticky enabled", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using sticky enabled", evaluation.toDictionary())

return evaluation
}
Expand All @@ -400,7 +455,7 @@ public class FeaturevisorInstance {
enabled: initialFeature.enabled,
initial: initialFeature)

logger.debug("using initial enabled", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using initial enabled", evaluation.toDictionary())

return evaluation
}
Expand All @@ -413,7 +468,7 @@ public class FeaturevisorInstance {
featureKey: featureKey,
reason: .notFound)

logger.warn("feature not found", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.warn("feature not found", evaluation.toDictionary())

return evaluation
}
Expand All @@ -434,7 +489,7 @@ public class FeaturevisorInstance {
reason: .forced,
enabled: force.enabled)

logger.debug("forced enabled found", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("forced enabled found", evaluation.toDictionary())

return evaluation
}
Expand Down Expand Up @@ -511,7 +566,7 @@ public class FeaturevisorInstance {
bucketValue: bucketValue,
enabled: false)

logger.debug("not matched", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("not matched", evaluation.toDictionary())

return evaluation
}
Expand All @@ -526,7 +581,7 @@ public class FeaturevisorInstance {
enabled: matchedTrafficEnabled,
traffic: matchedTraffic)

logger.debug("override from rule", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("override from rule", evaluation.toDictionary())

return evaluation
}
Expand Down Expand Up @@ -570,8 +625,8 @@ public class FeaturevisorInstance {
evaluation = Evaluation(
featureKey: featureKey,
reason: .disabled)

logger.debug("feature is disabled", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("feature is disabled", evaluation.toDictionary())

return evaluation
}
Expand All @@ -583,7 +638,7 @@ public class FeaturevisorInstance {
reason: .sticky,
variationValue: variationValue)

logger.debug("using sticky variation", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using sticky variation", evaluation.toDictionary())

return evaluation
}
Expand All @@ -595,7 +650,7 @@ public class FeaturevisorInstance {
reason: .initial,
variationValue: variationValue)

logger.debug("using initial variation", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using initial variation", evaluation.toDictionary())

return evaluation
}
Expand All @@ -606,7 +661,7 @@ public class FeaturevisorInstance {
featureKey: featureKey,
reason: .notFound)

logger.warn("feature not found", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.warn("feature not found", evaluation.toDictionary())

return evaluation
}
Expand All @@ -617,7 +672,7 @@ public class FeaturevisorInstance {
featureKey: featureKey,
reason: .noVariations)

logger.warn("no variations", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.warn("no variations", evaluation.toDictionary())

return evaluation
}
Expand All @@ -636,7 +691,7 @@ public class FeaturevisorInstance {
reason: .forced,
variation: variation)

logger.debug("forced variation found", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("forced variation found", evaluation.toDictionary())

return evaluation
}
Expand Down Expand Up @@ -669,7 +724,7 @@ public class FeaturevisorInstance {
ruleKey: matchedTraffic.key,
variation: variation)

logger.debug("override from rule", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("override from rule", evaluation.toDictionary())

return evaluation
}
Expand All @@ -689,7 +744,7 @@ public class FeaturevisorInstance {
bucketValue: bucketValue,
variation: variation)

logger.debug("allocated variation", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("allocated variation", evaluation.toDictionary())

return evaluation
}
Expand All @@ -702,7 +757,7 @@ public class FeaturevisorInstance {
reason: .error,
bucketValue: bucketValue)

logger.debug("no matched variation", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("no matched variation", evaluation.toDictionary())

return evaluation
}
Expand Down Expand Up @@ -767,7 +822,7 @@ public class FeaturevisorInstance {
if flag.enabled == false {
evaluation = Evaluation(featureKey: featureKey, reason: .disabled)

logger.debug("feature is disabled", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("feature is disabled", evaluation.toDictionary())

return evaluation
}
Expand All @@ -780,7 +835,7 @@ public class FeaturevisorInstance {
variableKey: variableKey,
variableValue: variableValue)

logger.debug("using sticky variable", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using sticky variable", evaluation.toDictionary())

return evaluation
}
Expand All @@ -795,7 +850,7 @@ public class FeaturevisorInstance {
variableKey: variableKey,
variableValue: variableValue)

logger.debug("using initial variable", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using initial variable", evaluation.toDictionary())

return evaluation
}
Expand All @@ -808,7 +863,7 @@ public class FeaturevisorInstance {
reason: .notFound,
variableKey: variableKey)

logger.warn("feature not found in datafile", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.warn("feature not found in datafile", evaluation.toDictionary())

return evaluation
}
Expand All @@ -824,7 +879,7 @@ public class FeaturevisorInstance {
reason: .notFound,
variableKey: variableKey)

logger.warn("variable schema not found", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.warn("variable schema not found", evaluation.toDictionary())

return evaluation
}
Expand All @@ -842,7 +897,7 @@ public class FeaturevisorInstance {
variableValue: variableValue,
variableSchema: variableSchema)

logger.debug("forced variable", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("forced variable", evaluation.toDictionary())

return evaluation
}
Expand All @@ -869,7 +924,7 @@ public class FeaturevisorInstance {
variableValue: variableValue,
variableSchema: variableSchema)

logger.debug("override from rule", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("override from rule", evaluation.toDictionary())

return evaluation
}
Expand Down Expand Up @@ -914,7 +969,7 @@ public class FeaturevisorInstance {
variableValue: override.value,
variableSchema: variableSchema)

logger.debug("variable override", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("variable override", evaluation.toDictionary())

return evaluation
}
Expand All @@ -930,7 +985,7 @@ public class FeaturevisorInstance {
variableValue: variableFromVariationValue,
variableSchema: variableSchema)

logger.debug("allocated variable", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("allocated variable", evaluation.toDictionary())

return evaluation
}
Expand All @@ -947,7 +1002,7 @@ public class FeaturevisorInstance {
variableValue: variableSchema.defaultValue,
variableSchema: variableSchema)

logger.debug("using default value", ["featureKey": featureKey]) // TODO: Log evaluation object. Make it encodable
logger.debug("using default value", evaluation.toDictionary())

return evaluation;
}
Expand Down
Loading