Skip to content

Commit

Permalink
feat: Move flag evaluation confidence (#113)
Browse files Browse the repository at this point in the history
* move all the logic inside confidence

* add evaluation extension in flag resolution

* cleanup

* have the evaluation in the confidence module for the flags

* update demo app to support only confidence

* fix some tests to move to confidence

* use int instead of int64 for 32bits system to work with default value for int, fix some more tests

* fixup! use int instead of int64 for 32bits system to work with default value for int, fix some more tests

* fixup! Merge branch 'main' into move-flag-evaluation-confidence

* fixup! fixup! Merge branch 'main' into move-flag-evaluation-confidence

* fixup! fixup! fixup! Merge branch 'main' into move-flag-evaluation-confidence

* refactor: Remove Common

* ignore swiftpm

* format

* fix typo

* address SDK_ID

* visitor id key fix

* timeout is unused

* Bring back some docs

* Refactor type mappers

* Add storage call

* Empty cache returns noFlagFound error

* TypeMapper refactor and tests

* rename class to TypeMapperTest

debug tests

---------

Co-authored-by: Fabrizio Demaria <fabrizio.f.demaria@gmail.com>
  • Loading branch information
vahidlazio and fabriziodemaria authored May 6, 2024
1 parent 18ab190 commit 5f3c8aa
Show file tree
Hide file tree
Showing 84 changed files with 1,620 additions and 2,799 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
/**/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.swiftpm/
.netrc
.build
.mockingbird
Expand Down
59 changes: 30 additions & 29 deletions ConfidenceDemoApp/ConfidenceDemoApp/ConfidenceDemoApp.swift
Original file line number Diff line number Diff line change
@@ -1,45 +1,46 @@
import ConfidenceProvider
import Confidence
import OpenFeature
import SwiftUI

class Status: ObservableObject {
enum State {
case unknown
case ready
case error(Error?)
}

@Published var state: State = .unknown
}


@main
struct ConfidenceDemoApp: App {
var body: some Scene {
WindowGroup {
ContentView()
.onReceive(NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)) { _ in
self.setup()
let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] ?? ""
let confidence = Confidence.Builder(clientSecret: secret)
.withContext(initialContext: ["targeting_key": ConfidenceValue(string: UUID.init().uuidString)])
.withRegion(region: .europe)
.build()

let status = Status()

ContentView(confidence: confidence, status: status)
.task {
do {
try await self.setup(confidence: confidence)
status.state = .ready
} catch {
status.state = .error(error)
print(error.localizedDescription)
}
}
}
}
}

extension ConfidenceDemoApp {
func setup() {
guard let secret = ProcessInfo.processInfo.environment["CLIENT_SECRET"] else {
return
}

// If we have no cache, then do a fetch first.
var initializationStrategy: InitializationStrategy = .activateAndFetchAsync
if ConfidenceFeatureProvider.isStorageEmpty() {
initializationStrategy = .fetchAndActivate
}

let confidence = Confidence.Builder(clientSecret: secret)
.withRegion(region: .europe)
.withInitializationstrategy(initializationStrategy: initializationStrategy)
.build()
let provider = ConfidenceFeatureProvider(confidence: confidence)

// NOTE: Using a random UUID for each app start is not advised and can result in getting stale values.
let ctx = MutableContext(
targetingKey: UUID.init().uuidString,
structure: MutableStructure.init(attributes: ["country": .string("SE")]))
Task {
await OpenFeatureAPI.shared.setProviderAndWait(provider: provider, initialContext: ctx)
}
func setup(confidence: Confidence) async throws {
try await confidence.fetchAndActivate()
confidence.track(
eventName: "all-types",
message: [
Expand Down
43 changes: 10 additions & 33 deletions ConfidenceDemoApp/ConfidenceDemoApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import SwiftUI
import OpenFeature
import Confidence
import Combine

struct ContentView: View {
@StateObject var status = Status()
@ObservedObject var status: Status
@StateObject var text = DisplayText()
@StateObject var color = FlagColor()

private let confidence: Confidence

init(confidence: Confidence, status: Status) {
self.confidence = confidence
self.status = status
}

var body: some View {
if case .ready = status.state {
VStack {
Expand All @@ -16,10 +23,7 @@ struct ContentView: View {
.padding(10)
Text(text.text)
Button("Get remote flag value") {
text.text = OpenFeatureAPI
.shared
.getClient()
.getStringValue(key: "swift-demoapp.color", defaultValue: "ERROR")
text.text = confidence.getValue(flagName: "swift-demoapp.color", defaultValue: "ERROR")
if text.text == "Green" {
color.color = .green
} else if text.text == "Yellow" {
Expand All @@ -44,33 +48,6 @@ struct ContentView: View {
}
}

class Status: ObservableObject {
enum State {
case unknown
case ready
case error(Error?)
}

var cancellable: AnyCancellable?

@Published var state: State = .unknown

init() {
cancellable = OpenFeatureAPI.shared.observe().sink { [weak self] event in
if event == .ready {
DispatchQueue.main.async {
self?.state = .ready
}
}
if event == .error {
DispatchQueue.main.async {
self?.state = .error(nil)
}
}
}
}
}

class DisplayText: ObservableObject {
@Published var text = "Hello World!"
}
Expand Down
15 changes: 2 additions & 13 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,23 @@ let package = Package(
.package(url: "git@github.com:open-feature/swift-sdk.git", from: "0.1.0"),
],
targets: [
// Internal definitions shared between Confidence and ConfidenceProvider
// These are not exposed to the consumers of Confidence or ConfidenceProvider
.target(
name: "Common",
dependencies: [],
plugins: []
),
.target(
name: "Confidence",
dependencies: [
"Common"
],
dependencies: [],
plugins: []
),
.target(
name: "ConfidenceProvider",
dependencies: [
.product(name: "OpenFeature", package: "swift-sdk"),
"Confidence",
"Common"
"Confidence"
],
plugins: []
),
.testTarget(
name: "ConfidenceProviderTests",
dependencies: [
"ConfidenceProvider",
"Common",
]
),
.testTarget(
Expand Down
11 changes: 0 additions & 11 deletions Sources/Common/Sdk.swift

This file was deleted.

File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import Foundation
import Common
import Confidence
import OpenFeature
import os

typealias ApplyFlagHTTPResponse = HttpClientResponse<ApplyFlagsResponse>
Expand Down Expand Up @@ -145,7 +142,7 @@ final class FlagApplierWithRetries: FlagApplier {
}

private func handleError(error: Error) -> Error {
if error is ConfidenceError || error is OpenFeatureError {
if error is ConfidenceError {
return error
} else {
return ConfidenceError.grpcError(message: "\(error)")
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Foundation
import Confidence

public enum BaseUrlMapper {
static func from(region: ConfidenceRegion) -> String {
Expand Down
Loading

0 comments on commit 5f3c8aa

Please sign in to comment.