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 7 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
91 changes: 65 additions & 26 deletions Sources/FeaturevisorSDK/Instance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,25 @@ public enum EvaluationReason: String {
case error
}

public struct Evaluation {
public struct Evaluation: Encodable {

private enum CodingKeys: String, CodingKey {
PatrykPiwowarczyk marked this conversation as resolved.
Show resolved Hide resolved
case featureKey
case reason
case bucketValue
case ruleKey
case error
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
Expand Down Expand Up @@ -81,6 +99,27 @@ 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)
if let error = error {
try container.encode(error.localizedDescription, forKey: .error)
}
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)
}
}

let emptyDatafile = DatafileContent(
Expand Down Expand Up @@ -370,7 +409,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", ["featureKey": evaluation])
PatrykPiwowarczyk marked this conversation as resolved.
Show resolved Hide resolved

return evaluation
}
Expand All @@ -383,7 +422,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -396,7 +435,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -417,7 +456,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", ["featureKey": evaluation])

return evaluation
}
Expand Down Expand Up @@ -494,7 +533,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -509,7 +548,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", ["featureKey": evaluation])

return evaluation
}
Expand Down Expand Up @@ -554,7 +593,7 @@ public class FeaturevisorInstance {
featureKey: featureKey,
reason: .disabled)

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

return evaluation
}
Expand All @@ -566,7 +605,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -578,7 +617,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -589,7 +628,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -600,7 +639,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -619,7 +658,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", ["featureKey": evaluation])

return evaluation
}
Expand Down Expand Up @@ -652,7 +691,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -672,7 +711,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -685,7 +724,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", ["featureKey": evaluation])

return evaluation
}
Expand Down Expand Up @@ -750,7 +789,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -763,7 +802,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -778,7 +817,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -791,7 +830,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -807,7 +846,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -825,7 +864,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -852,7 +891,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", ["featureKey": evaluation])

return evaluation
}
Expand Down Expand Up @@ -897,7 +936,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -913,7 +952,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", ["featureKey": evaluation])

return evaluation
}
Expand All @@ -930,7 +969,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", ["featureKey": evaluation])

return evaluation;
}
Expand Down
Loading