Skip to content

Commit

Permalink
refactor!: Add eval details to finally hook
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Demaria <[email protected]>
  • Loading branch information
fabriziodemaria committed Dec 20, 2024
1 parent b90d593 commit 81c9fa2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Sources/OpenFeature/Hook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol Hook {

func error<HookValue>(ctx: HookContext<HookValue>, error: Error, hints: [String: Any])

func finally<HookValue>(ctx: HookContext<HookValue>, hints: [String: Any])
func finally<HookValue>(ctx: HookContext<HookValue>, details: FlagEvaluationDetails<HookValue>, hints: [String: Any])

func supportsFlagValueType(flagValueType: FlagValueType) -> Bool
}
Expand All @@ -31,7 +31,7 @@ extension Hook {
// Default implementation
}

public func finally<HookValue>(ctx: HookContext<HookValue>, hints: [String: Any]) {
public func finally<HookValue>(ctx: HookContext<HookValue>, details: FlagEvaluationDetails<HookValue>, hints: [String: Any]) {
// Default implementation
}

Expand Down
8 changes: 6 additions & 2 deletions Sources/OpenFeature/HookSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,14 @@ class HookSupport {
}

func finallyHooks<T>(
flagValueType: FlagValueType, hookCtx: HookContext<T>, hooks: [any Hook], hints: [String: Any]
flagValueType: FlagValueType,
hookCtx: HookContext<T>,
details: FlagEvaluationDetails<T>,
hooks: [any Hook],
hints: [String: Any]
) {
hooks
.filter { $0.supportsFlagValueType(flagValueType: flagValueType) }
.forEach { $0.finally(ctx: hookCtx, hints: hints) }
.forEach { $0.finally(ctx: hookCtx, details: details, hints: hints) }
}
}
2 changes: 1 addition & 1 deletion Sources/OpenFeature/OpenFeatureClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ extension OpenFeatureClient {
flagValueType: T.flagValueType, hookCtx: hookCtx, error: error, hooks: mergedHooks, hints: hints)
}
hookSupport.finallyHooks(
flagValueType: T.flagValueType, hookCtx: hookCtx, hooks: mergedHooks, hints: hints)
flagValueType: T.flagValueType, hookCtx: hookCtx, details: details, hooks: mergedHooks, hints: hints)
return details
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenFeatureTests/Helpers/BooleanHookMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class BooleanHookMock: Hook {
self.addEval(self.prefix.isEmpty ? "error" : "\(self.prefix) error")
}

func finally<HookValue>(ctx: HookContext<HookValue>, hints: [String: Any]) {
func finally<HookValue>(ctx: HookContext<HookValue>, details: FlagEvaluationDetails<HookValue>, hints: [String: Any]) {
finallyCalled += 1
self.addEval(self.prefix.isEmpty ? "finally" : "\(self.prefix) finally")
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenFeatureTests/Helpers/IntHookMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class IntHookMock: Hook {
self.addEval(self.prefix.isEmpty ? "error" : "\(self.prefix) error")
}

func finally<HookValue>(ctx: HookContext<HookValue>, hints: [String: Any]) {
func finally<HookValue>(ctx: HookContext<HookValue>, details: FlagEvaluationDetails<HookValue>, hints: [String: Any]) {
finallyCalled += 1
self.addEval(self.prefix.isEmpty ? "finally" : "\(self.prefix) finally")
}
Expand Down
1 change: 1 addition & 0 deletions Tests/OpenFeatureTests/HookSupportTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class HookSupportTests: XCTestCase {
hookSupport.finallyHooks(
flagValueType: .boolean,
hookCtx: hookContext,
details: FlagEvaluationDetails(flagKey: "", value: false),
hooks: [hook],
hints: [:])

Expand Down

0 comments on commit 81c9fa2

Please sign in to comment.