Skip to content
This repository has been archived by the owner on Sep 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #20 from spotify/metadata
Browse files Browse the repository at this point in the history
Different Metadata protocols client/provider
  • Loading branch information
Calibretto authored Jul 24, 2023
2 parents d2e5b4e + dae6a9a commit 3175764
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Sources/OpenFeature/Client.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation

public protocol Metadata {
public protocol ClientMetadata {
var name: String? { get }
}
2 changes: 1 addition & 1 deletion Sources/OpenFeature/FeatureProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenFeature/HookContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ public struct HookContext<T> {
var type: FlagValueType
var defaultValue: T
var ctx: EvaluationContext?
var clientMetadata: Metadata?
var providerMetadata: Metadata?
var clientMetadata: ClientMetadata?
var providerMetadata: ProviderMetadata?
}
4 changes: 2 additions & 2 deletions Sources/OpenFeature/NoOpProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -62,7 +62,7 @@ class NoOpProvider: FeatureProvider {
}

extension NoOpProvider {
struct NoOpMetadata: Metadata {
struct NoOpMetadata: ProviderMetadata {
var name: String?
}
}
2 changes: 1 addition & 1 deletion Sources/OpenFeature/OpenFeatureAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class OpenFeatureAPI {
return self._context
}

public func getProviderMetadata() -> Metadata? {
public func getProviderMetadata() -> ProviderMetadata? {
return self.getProvider()?.metadata
}

Expand Down
9 changes: 5 additions & 4 deletions Sources/OpenFeature/OpenFeatureClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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...) {
Expand Down Expand Up @@ -57,7 +57,7 @@ extension OpenFeatureClient {
}

extension OpenFeatureClient {
public struct ClientMetadata: Metadata {
public struct Metadata: ClientMetadata {
public var name: String?
}
}
Expand Down Expand Up @@ -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)")

Expand Down
5 changes: 5 additions & 0 deletions Sources/OpenFeature/ProviderMetadata.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

public protocol ProviderMetadata {
var name: String? { get }
}
4 changes: 2 additions & 2 deletions Tests/OpenFeatureTests/Helpers/AlwaysBrokenProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -46,7 +46,7 @@ class AlwaysBrokenProvider: FeatureProvider {
}

extension AlwaysBrokenProvider {
struct AlwaysBrokenMetadata: Metadata {
struct AlwaysBrokenMetadata: ProviderMetadata {
var name: String? = "test"
}
}
4 changes: 2 additions & 2 deletions Tests/OpenFeatureTests/Helpers/DoSomethingProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down Expand Up @@ -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
}
}
2 changes: 1 addition & 1 deletion Tests/OpenFeatureTests/ValueTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 3175764

Please sign in to comment.