Skip to content

Commit

Permalink
write open feature keys flatten and remove the flattening logi
Browse files Browse the repository at this point in the history
  • Loading branch information
vahidlazio committed Apr 23, 2024
1 parent 7bff054 commit dcc53b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 27 deletions.
19 changes: 0 additions & 19 deletions Sources/Confidence/ConfidenceValue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,6 @@ import Common

public typealias ConfidenceStruct = [String: ConfidenceValue]

public extension ConfidenceStruct {
func flattenOpenFeature() -> ConfidenceStruct {
var newStruct: ConfidenceStruct = [:]
let openFeatureStruct: ConfidenceValue? = self["open_feature"]
guard let openFeatureStruct: ConfidenceStruct = openFeatureStruct?.asStructure() else {
return self
}
// add open feature struct keys
for entry in openFeatureStruct {
newStruct[entry.key] = entry.value
}
// add all the rest keys
for entry in self where entry.key != "open_feature" {
newStruct[entry.key] = entry.value
}
return newStruct
}
}

public class ConfidenceValue: Equatable, Codable, CustomStringConvertible {
private let value: ConfidenceValueInternal
public var description: String {
Expand Down
17 changes: 9 additions & 8 deletions Sources/ConfidenceProvider/ConfidenceFeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {

Task {
do {
let context = confidence?.getContext()
.flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: initialContext)
let context = confidence?.getContext() ?? ConfidenceTypeMapper.from(ctx: initialContext)
let resolveResult = try await resolve(context: context)

// update cache with stored values
Expand Down Expand Up @@ -140,8 +139,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
self.updateConfidenceContext(context: newContext)
Task {
do {
let context = confidence?.getContext()
.flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: newContext)
let context = confidence?.getContext() ?? ConfidenceTypeMapper.from(ctx: newContext)
let resolveResult = try await resolve(context: context)

// update the storage
Expand All @@ -156,9 +154,12 @@ public class ConfidenceFeatureProvider: FeatureProvider {
}

private func updateConfidenceContext(context: EvaluationContext) {
confidence?.updateContextEntry(
key: "open_feature",
value: ConfidenceValue(structure: ConfidenceTypeMapper.from(ctx: context)))
for entry in ConfidenceTypeMapper.from(ctx: context) {
confidence?.updateContextEntry(
key: entry.key,
value: entry.value
)
}
}

public func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws
Expand Down Expand Up @@ -269,7 +270,7 @@ public class ConfidenceFeatureProvider: FeatureProvider {
throw OpenFeatureError.invalidContextError
}

let context = confidence?.getContext().flattenOpenFeature() ?? ConfidenceTypeMapper.from(ctx: ctx)
let context = confidence?.getContext() ?? ConfidenceTypeMapper.from(ctx: ctx)

do {
let resolverResult = try resolver.resolve(flag: path.flag, contextHash: context.hash())
Expand Down

0 comments on commit dcc53b9

Please sign in to comment.