From dae6a9abe64e31e3949925710a61aad3ec80228a Mon Sep 17 00:00:00 2001 From: Fabrizio Demaria Date: Fri, 21 Jul 2023 14:37:35 +0200 Subject: [PATCH] Different Metadata protocols client/provider --- Sources/OpenFeature/Client.swift | 2 +- .../OpenFeature/{Metadata.swift => ClientMetadata.swift} | 2 +- Sources/OpenFeature/FeatureProvider.swift | 2 +- Sources/OpenFeature/HookContext.swift | 4 ++-- Sources/OpenFeature/NoOpProvider.swift | 4 ++-- Sources/OpenFeature/OpenFeatureAPI.swift | 2 +- Sources/OpenFeature/OpenFeatureClient.swift | 9 +++++---- Sources/OpenFeature/ProviderMetadata.swift | 5 +++++ .../OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift | 4 ++-- Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift | 4 ++-- Tests/OpenFeatureTests/ValueTests.swift | 2 +- 11 files changed, 23 insertions(+), 17 deletions(-) rename Sources/OpenFeature/{Metadata.swift => ClientMetadata.swift} (60%) create mode 100644 Sources/OpenFeature/ProviderMetadata.swift diff --git a/Sources/OpenFeature/Client.swift b/Sources/OpenFeature/Client.swift index eb4d710..083f28e 100644 --- a/Sources/OpenFeature/Client.swift +++ b/Sources/OpenFeature/Client.swift @@ -2,7 +2,7 @@ import Foundation /// Interface used to resolve flags of varying types. public protocol Client: Features { - var metadata: Metadata { get } + var metadata: ClientMetadata { get } /// The hooks associated to this client. var hooks: [any Hook] { get } diff --git a/Sources/OpenFeature/Metadata.swift b/Sources/OpenFeature/ClientMetadata.swift similarity index 60% rename from Sources/OpenFeature/Metadata.swift rename to Sources/OpenFeature/ClientMetadata.swift index 08d7207..1e723fa 100644 --- a/Sources/OpenFeature/Metadata.swift +++ b/Sources/OpenFeature/ClientMetadata.swift @@ -1,5 +1,5 @@ import Foundation -public protocol Metadata { +public protocol ClientMetadata { var name: String? { get } } diff --git a/Sources/OpenFeature/FeatureProvider.swift b/Sources/OpenFeature/FeatureProvider.swift index 2818ac2..0b2ca5a 100644 --- a/Sources/OpenFeature/FeatureProvider.swift +++ b/Sources/OpenFeature/FeatureProvider.swift @@ -3,7 +3,7 @@ import Foundation /// The interface implemented by upstream flag providers to resolve flags for their service. public protocol FeatureProvider { var hooks: [any Hook] { get } - var metadata: Metadata { get } + var metadata: ProviderMetadata { get } /// Called by OpenFeatureAPI whenever the new Provider is registered func initialize(initialContext: EvaluationContext?) async diff --git a/Sources/OpenFeature/HookContext.swift b/Sources/OpenFeature/HookContext.swift index e463ac7..d25974a 100644 --- a/Sources/OpenFeature/HookContext.swift +++ b/Sources/OpenFeature/HookContext.swift @@ -6,6 +6,6 @@ public struct HookContext { var type: FlagValueType var defaultValue: T var ctx: EvaluationContext? - var clientMetadata: Metadata? - var providerMetadata: Metadata? + var clientMetadata: ClientMetadata? + var providerMetadata: ProviderMetadata? } diff --git a/Sources/OpenFeature/NoOpProvider.swift b/Sources/OpenFeature/NoOpProvider.swift index 7ddeccf..f5d41bf 100644 --- a/Sources/OpenFeature/NoOpProvider.swift +++ b/Sources/OpenFeature/NoOpProvider.swift @@ -4,7 +4,7 @@ import Foundation class NoOpProvider: FeatureProvider { public static let passedInDefault = "Passed in default" - var metadata: Metadata = NoOpMetadata(name: "No-op provider") + var metadata: ProviderMetadata = NoOpMetadata(name: "No-op provider") var hooks: [any Hook] = [] func onContextSet(oldContext: EvaluationContext?, newContext: EvaluationContext) { @@ -62,7 +62,7 @@ class NoOpProvider: FeatureProvider { } extension NoOpProvider { - struct NoOpMetadata: Metadata { + struct NoOpMetadata: ProviderMetadata { var name: String? } } diff --git a/Sources/OpenFeature/OpenFeatureAPI.swift b/Sources/OpenFeature/OpenFeatureAPI.swift index e36c71c..080d0f6 100644 --- a/Sources/OpenFeature/OpenFeatureAPI.swift +++ b/Sources/OpenFeature/OpenFeatureAPI.swift @@ -42,7 +42,7 @@ public class OpenFeatureAPI { return self._context } - public func getProviderMetadata() -> Metadata? { + public func getProviderMetadata() -> ProviderMetadata? { return self.getProvider()?.metadata } diff --git a/Sources/OpenFeature/OpenFeatureClient.swift b/Sources/OpenFeature/OpenFeatureClient.swift index 1adb3dd..cdd933f 100644 --- a/Sources/OpenFeature/OpenFeatureClient.swift +++ b/Sources/OpenFeature/OpenFeatureClient.swift @@ -8,7 +8,7 @@ public class OpenFeatureClient: Client { private(set) var name: String? private(set) var version: String? - private(set) public var metadata: Metadata + private(set) public var metadata: ClientMetadata private(set) public var hooks: [any Hook] = [] private var hookSupport = HookSupport() @@ -18,7 +18,7 @@ public class OpenFeatureClient: Client { self.openFeatureApi = openFeatureApi self.name = name self.version = version - self.metadata = ClientMetadata(name: name) + self.metadata = Metadata(name: name) } public func addHooks(_ hooks: any Hook...) { @@ -57,7 +57,7 @@ extension OpenFeatureClient { } extension OpenFeatureClient { - public struct ClientMetadata: Metadata { + public struct Metadata: ClientMetadata { public var name: String? } } @@ -99,7 +99,8 @@ extension OpenFeatureClient { details = evalDetails try hookSupport.afterHooks( - flagValueType: T.flagValueType, hookCtx: hookCtx, details: evalDetails, hooks: mergedHooks, hints: hints) + flagValueType: T.flagValueType, hookCtx: hookCtx, details: evalDetails, hooks: mergedHooks, hints: hints + ) } catch { logger.error("Unable to correctly evaluate flag with key \(key) due to exception \(error)") diff --git a/Sources/OpenFeature/ProviderMetadata.swift b/Sources/OpenFeature/ProviderMetadata.swift new file mode 100644 index 0000000..8886edf --- /dev/null +++ b/Sources/OpenFeature/ProviderMetadata.swift @@ -0,0 +1,5 @@ +import Foundation + +public protocol ProviderMetadata { + var name: String? { get } +} diff --git a/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift b/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift index 7c0b43f..433fa55 100644 --- a/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift +++ b/Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift @@ -3,7 +3,7 @@ import Foundation @testable import OpenFeature class AlwaysBrokenProvider: FeatureProvider { - var metadata: Metadata = AlwaysBrokenMetadata() + var metadata: ProviderMetadata = AlwaysBrokenMetadata() var hooks: [any Hook] = [] func onContextSet(oldContext: OpenFeature.EvaluationContext?, newContext: OpenFeature.EvaluationContext) { @@ -46,7 +46,7 @@ class AlwaysBrokenProvider: FeatureProvider { } extension AlwaysBrokenProvider { - struct AlwaysBrokenMetadata: Metadata { + struct AlwaysBrokenMetadata: ProviderMetadata { var name: String? = "test" } } diff --git a/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift b/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift index ee691c0..f8cac77 100644 --- a/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift +++ b/Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift @@ -13,7 +13,7 @@ class DoSomethingProvider: FeatureProvider { } var hooks: [any OpenFeature.Hook] = [] - var metadata: OpenFeature.Metadata = DoMetadata() + var metadata: OpenFeature.ProviderMetadata = DoMetadata() func getBooleanEvaluation(key: String, defaultValue: Bool, context: EvaluationContext?) throws -> ProviderEvaluation< @@ -55,7 +55,7 @@ class DoSomethingProvider: FeatureProvider { return ProviderEvaluation(value: .null) } - public struct DoMetadata: Metadata { + public struct DoMetadata: ProviderMetadata { public var name: String? = DoSomethingProvider.name } } diff --git a/Tests/OpenFeatureTests/ValueTests.swift b/Tests/OpenFeatureTests/ValueTests.swift index 452aae7..4d30465 100644 --- a/Tests/OpenFeatureTests/ValueTests.swift +++ b/Tests/OpenFeatureTests/ValueTests.swift @@ -69,7 +69,7 @@ final class ValueTests: XCTestCase { formatter.dateFormat = "yyyy-MM-dd HH:mm:ss" let date = try XCTUnwrap(formatter.date(from: "2022-01-01 12:00:00")) - + let value: Value = .structure([ "null": .null, "bool": .boolean(true),