Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#145] Code Cleanup for v4 #146

Merged
merged 7 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/YMFF/FeatureFlag/FeatureFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ final public class FeatureFlag<RawValue, Value> {

return value
} set {
try? resolver.setValueSync(transformer.rawValueFromValue(newValue), toMutableStoreUsing: key)
try? resolver.setValueSync(transformer.rawValueFromValue(newValue), for: key)
}
}

Expand All @@ -87,8 +87,8 @@ final public class FeatureFlag<RawValue, Value> {
/// Removes the value from the first mutable feature flag store which contains one for `key`.
///
/// + Errors thrown by `resolver` are ignored.
public func removeValueFromMutableStore() {
try? resolver.removeValueFromMutableStoreSync(using: key)
public func removeValueFromMutableStores() {
try? resolver.removeValueSync(for: key)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// Copyright © 2020 Yakov Manshin. See the LICENSE file for license info.
//

#if !COCOAPODS
import YMFFProtocols
#endif

extension FeatureFlagResolver {

/// Errors returned by `FeatureFlagResolver`.
Expand All @@ -18,7 +22,7 @@ extension FeatureFlagResolver {
case noStoreAvailable

/// No feature-flag store contains a value for the given key.
case valueNotFoundInStores(key: String)
case valueNotFoundInStores(key: FeatureFlagKey)

/// The feature-flag store has thrown an error.
case storeError(any Swift.Error)
Expand Down
26 changes: 13 additions & 13 deletions Sources/YMFF/FeatureFlagResolver/FeatureFlagResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ final public class FeatureFlagResolver {
///
/// - Parameter stores: *Required.* The array of feature flag stores.
public convenience init(stores: [any FeatureFlagStore]) {
let configuration: any FeatureFlagResolverConfiguration = Configuration(stores: stores)
let configuration = Configuration(stores: stores)
self.init(configuration: configuration)
}

Expand All @@ -57,15 +57,15 @@ extension FeatureFlagResolver: FeatureFlagResolverProtocol {
try await retrieveFirstValue(forKey: key)
}

public func setValue<Value>(_ newValue: Value, toMutableStoreUsing key: FeatureFlagKey) async throws {
public func setValue<Value>(_ value: Value, for key: FeatureFlagKey) async throws {
let mutableStores = getMutableStores()
guard !mutableStores.isEmpty else {
throw Error.noStoreAvailable
}

for store in getStores() {
if
case .failure(let error) = await store.value(forKey: key) as Result<Value, _>,
case .failure(let error) = await store.value(for: key) as Result<Value, _>,
case .typeMismatch = error
{
throw Error.storeError(error)
Expand All @@ -75,7 +75,7 @@ extension FeatureFlagResolver: FeatureFlagResolverProtocol {
var lastErrorFromStore: (any Swift.Error)?
for store in mutableStores {
do {
try await store.setValue(newValue, forKey: key)
try await store.setValue(value, for: key)
} catch {
lastErrorFromStore = error
}
Expand All @@ -86,7 +86,7 @@ extension FeatureFlagResolver: FeatureFlagResolverProtocol {
}
}

public func removeValueFromMutableStore(using key: FeatureFlagKey) async throws {
public func removeValue(for key: FeatureFlagKey) async throws {
let mutableStores = getMutableStores()
guard !mutableStores.isEmpty else {
throw Error.noStoreAvailable
Expand All @@ -95,7 +95,7 @@ extension FeatureFlagResolver: FeatureFlagResolverProtocol {
var lastErrorFromStore: (any Swift.Error)?
for store in mutableStores {
do {
try await store.removeValue(forKey: key)
try await store.removeValue(for: key)
} catch {
lastErrorFromStore = error
}
Expand All @@ -116,15 +116,15 @@ extension FeatureFlagResolver: SynchronousFeatureFlagResolverProtocol {
try retrieveFirstValueSync(forKey: key)
}

public func setValueSync<Value>(_ newValue: Value, toMutableStoreUsing key: FeatureFlagKey) throws {
public func setValueSync<Value>(_ value: Value, for key: FeatureFlagKey) throws {
let syncMutableStores = getSyncMutableStores()
guard !syncMutableStores.isEmpty else {
throw Error.noStoreAvailable
}

for store in getSyncStores() {
if
case .failure(let error) = store.valueSync(forKey: key) as Result<Value, _>,
case .failure(let error) = store.valueSync(for: key) as Result<Value, _>,
case .typeMismatch = error
{
throw Error.storeError(error)
Expand All @@ -134,7 +134,7 @@ extension FeatureFlagResolver: SynchronousFeatureFlagResolverProtocol {
var lastErrorFromStore: (any Swift.Error)?
for store in syncMutableStores {
do {
try store.setValueSync(newValue, forKey: key)
try store.setValueSync(value, for: key)
} catch {
lastErrorFromStore = error
}
Expand All @@ -145,7 +145,7 @@ extension FeatureFlagResolver: SynchronousFeatureFlagResolverProtocol {
}
}

public func removeValueFromMutableStoreSync(using key: FeatureFlagKey) throws {
public func removeValueSync(for key: FeatureFlagKey) throws {
let syncMutableStores = getSyncMutableStores()
guard !syncMutableStores.isEmpty else {
throw Error.noStoreAvailable
Expand All @@ -154,7 +154,7 @@ extension FeatureFlagResolver: SynchronousFeatureFlagResolverProtocol {
var lastErrorFromStore: (any Swift.Error)?
for store in syncMutableStores {
do {
try store.removeValueSync(forKey: key)
try store.removeValueSync(for: key)
} catch {
lastErrorFromStore = error
}
Expand Down Expand Up @@ -200,7 +200,7 @@ extension FeatureFlagResolver {
}

for store in matchingStores {
switch await store.value(forKey: key) as Result<Value, _> {
switch await store.value(for: key) as Result<Value, _> {
case .success(let value):
return value
case .failure(let error):
Expand All @@ -225,7 +225,7 @@ extension FeatureFlagResolver {
}

for store in matchingStores {
switch store.valueSync(forKey: key) as Result<Value, _> {
switch store.valueSync(for: key) as Result<Value, _> {
case .success(let value):
return value
case .failure(let error):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ final public class RuntimeOverridesStore {

extension RuntimeOverridesStore: SynchronousMutableFeatureFlagStore {

public func valueSync<Value>(forKey key: String) -> Result<Value, FeatureFlagStoreError> {
public func valueSync<Value>(for key: FeatureFlagKey) -> Result<Value, FeatureFlagStoreError> {
guard let anyValue = store[key] else { return .failure(.valueNotFound) }
guard let value = anyValue as? Value else { return .failure(.typeMismatch) }
return .success(value)
}

public func setValueSync<Value>(_ value: Value, forKey key: String) {
public func setValueSync<Value>(_ value: Value, for key: FeatureFlagKey) {
store[key] = value
}

public func removeValueSync(forKey key: String) {
public func removeValueSync(for key: FeatureFlagKey) {
store.removeValue(forKey: key)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public typealias TransparentFeatureFlagStore = [String: Any]

extension TransparentFeatureFlagStore: SynchronousFeatureFlagStore, FeatureFlagStore {

public func valueSync<V>(forKey key: String) -> Result<V, FeatureFlagStoreError> {
public func valueSync<V>(for key: FeatureFlagKey) -> Result<V, FeatureFlagStoreError> {
guard let anyValue = self[key] else { return .failure(.valueNotFound) }
guard let value = anyValue as? V else { return .failure(.typeMismatch) }
return .success(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ final public class UserDefaultsStore {

extension UserDefaultsStore: SynchronousMutableFeatureFlagStore {

public func valueSync<Value>(forKey key: String) -> Result<Value, FeatureFlagStoreError> {
public func valueSync<Value>(for key: FeatureFlagKey) -> Result<Value, FeatureFlagStoreError> {
guard let anyValue = userDefaults.object(forKey: key) else { return .failure(.valueNotFound) }
guard let value = anyValue as? Value else { return .failure(.typeMismatch) }
return .success(value)
}

public func setValueSync<Value>(_ value: Value, forKey key: String) {
public func setValueSync<Value>(_ value: Value, for key: FeatureFlagKey) {
userDefaults.set(value, forKey: key)
}

public func removeValueSync(forKey key: String) {
public func removeValueSync(for key: FeatureFlagKey) {
userDefaults.removeObject(forKey: key)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
/// A service that resolves feature flag values with their keys.
public protocol FeatureFlagResolverProtocol {

/// The object used to configure the resolver.
var configuration: FeatureFlagResolverConfiguration { get }

/// Returns a value for the specified key.
///
/// - Parameter key: *Required.* The feature flag key.
Expand All @@ -22,11 +19,11 @@ public protocol FeatureFlagResolverProtocol {
/// - Parameters:
/// - newValue: *Required.* The override value.
/// - key: *Required.* The feature flag key.
func setValue<Value>(_ newValue: Value, toMutableStoreUsing key: FeatureFlagKey) async throws
func setValue<Value>(_ value: Value, for key: FeatureFlagKey) async throws

/// Removes the value from the first mutable feature flag store which has one for the specified key.
///
/// - Parameter key: *Required.* The feature flag key.
func removeValueFromMutableStore(using key: FeatureFlagKey) async throws
func removeValue(for key: FeatureFlagKey) async throws

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public protocol SynchronousFeatureFlagResolverProtocol: FeatureFlagResolverProto
/// - Parameters:
/// - newValue: *Required.* The override value.
/// - key: *Required.* The feature flag key.
func setValueSync<Value>(_ newValue: Value, toMutableStoreUsing key: FeatureFlagKey) throws
func setValueSync<Value>(_ value: Value, for key: FeatureFlagKey) throws

/// Removes the value from the first mutable feature flag store which has one for the specified key.
///
/// - Parameter key: *Required.* The feature flag key.
func removeValueFromMutableStoreSync(using key: FeatureFlagKey) throws
func removeValueSync(for key: FeatureFlagKey) throws

}

Expand All @@ -35,12 +35,12 @@ extension SynchronousFeatureFlagResolverProtocol {
try valueSync(for: key)
}

public func setValue<Value>(_ newValue: Value, toMutableStoreUsing key: FeatureFlagKey) async throws {
try setValueSync(newValue, toMutableStoreUsing: key)
public func setValue<Value>(_ newValue: Value, for key: FeatureFlagKey) async throws {
try setValueSync(newValue, for: key)
}

public func removeValueFromMutableStore(using key: FeatureFlagKey) async throws {
try removeValueFromMutableStoreSync(using: key)
public func removeValue(for key: FeatureFlagKey) async throws {
try removeValueSync(for: key)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ public protocol FeatureFlagStore {
/// Retrieves a feature flag value by its key.
///
/// - Parameter key: *Required.* The key that points to a feature flag value in the store.
func value<Value>(forKey key: String) async -> Result<Value, FeatureFlagStoreError>
func value<Value>(for key: FeatureFlagKey) async -> Result<Value, FeatureFlagStoreError>

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ public protocol MutableFeatureFlagStore: AnyObject, FeatureFlagStore {
/// - Parameters:
/// - value: *Required.* The value to record.
/// - key: *Required.* The key used to address the value.
func setValue<Value>(_ value: Value, forKey key: String) async throws
func setValue<Value>(_ value: Value, for key: FeatureFlagKey) async throws

/// Removes the value from the store.
///
/// - Parameter key: *Required.* The key used to address the value.
func removeValue(forKey key: String) async throws
func removeValue(for key: FeatureFlagKey) async throws

/// Immediately saves changed values so they’re not lost.
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ public protocol SynchronousFeatureFlagStore: FeatureFlagStore {
/// Retrieves a feature flag value by its key.
///
/// - Parameter key: *Required.* The key that points to a feature flag value in the store.
func valueSync<Value>(forKey key: String) -> Result<Value, FeatureFlagStoreError>
func valueSync<Value>(for key: FeatureFlagKey) -> Result<Value, FeatureFlagStoreError>

}

// MARK: - Async Requirements

extension SynchronousFeatureFlagStore {

public func value<Value>(forKey key: String) async -> Result<Value, FeatureFlagStoreError> {
valueSync(forKey: key)
public func value<Value>(for key: FeatureFlagKey) async -> Result<Value, FeatureFlagStoreError> {
valueSync(for: key)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public protocol SynchronousMutableFeatureFlagStore: SynchronousFeatureFlagStore,
/// - Parameters:
/// - value: *Required.* The value to record.
/// - key: *Required.* The key used to address the value.
func setValueSync<Value>(_ value: Value, forKey key: String) throws
func setValueSync<Value>(_ value: Value, for key: FeatureFlagKey) throws

/// Removes the value from the store.
///
/// - Parameter key: *Required.* The key used to address the value.
func removeValueSync(forKey key: String) throws
func removeValueSync(for key: FeatureFlagKey) throws

/// Immediately saves changed values so they’re not lost.
///
Expand All @@ -31,12 +31,12 @@ public protocol SynchronousMutableFeatureFlagStore: SynchronousFeatureFlagStore,

extension SynchronousMutableFeatureFlagStore {

public func setValue<Value>(_ value: Value, forKey key: String) async throws {
try setValueSync(value, forKey: key)
public func setValue<Value>(_ value: Value, for key: FeatureFlagKey) async throws {
try setValueSync(value, for: key)
}

public func removeValue(forKey key: String) async throws {
try removeValueSync(forKey: key)
public func removeValue(for key: FeatureFlagKey) async throws {
try removeValueSync(for: key)
}

public func saveChanges() async throws {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// FeatureFlagResolverConfigurationTests.swift
// YMFF
// YMFFTests
//
// Created by Yakov Manshin on 5/17/21.
// Copyright © 2021 Yakov Manshin. See the LICENSE file for license info.
Expand Down
Loading
Loading