Skip to content

Commit

Permalink
[#110] v4 Documentation (#152)
Browse files Browse the repository at this point in the history
* Updated `README` with instructions and examples for YMFF v4
* Updated code documentation
* Updated version number in the podspec file
  • Loading branch information
yakovmanshin authored May 13, 2024
1 parent 6121eff commit a0a1205
Show file tree
Hide file tree
Showing 16 changed files with 135 additions and 121 deletions.
140 changes: 73 additions & 67 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# YMFF: Feature management made easy

YMFF is a nice little library that makes managing features with feature flags—and managing feature flags themselves—a bliss, thanks to Swift’s [macros](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros) and [property wrappers](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/#Property-Wrappers).
YMFF is a nice little library that makes managing features with feature flags—and managing feature flags themselves—a bliss, thanks to Swift’s [property wrappers](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/properties/#Property-Wrappers) and (in the future) [macros](https://docs.swift.org/swift-book/documentation/the-swift-programming-language/macros).

<details>
<summary>Why & How</summary>

Every company I worked for needed a way to manage availability of features in the apps already shipped to users. Surprisingly enough, [*feature flags*](https://en.wikipedia.org/wiki/Feature_toggle) (a.k.a. *feature toggles* a.k.a. *feature switches*) tend to cause a lot of struggle.
Every company I worked for needed a way to manage availability of features in the apps already shipped to the users. Surprisingly, [*feature flags*](https://en.wikipedia.org/wiki/Feature_toggle) (a.k.a. *feature toggles* a.k.a. *feature switches*) tend to cause a lot of struggle.

**I aspire to change that.**

YMFF ships completely ready-to-use, right out of the box: you get everything you need to get started in just a few minutes. But you can also replace nearly any component of the system with your own, customized implementation. The supplied implementation and the protocols are kept in two separate targets (YMFF and YMFFProtocols, respectively).
YMFF ships completely ready-to-use, right out of the box: you get everything you need to get started in just a few lines of code.

</details>

Expand All @@ -21,35 +21,39 @@ I’m sure you know how to install dependencies. YMFF supports both SPM and Coco
<summary>Need Help?</summary>

### Swift Package Manager (SPM)
To add YMFF to your project, use Xcode’s built-in support for Swift packages. Click File → Swift Packages → Add Package Dependency, and paste the following URL into the search field:

To add YMFF to your project, use Xcode’s built-in support for Swift packages. Click File → Add Package Dependencies, and paste the following URL into the search field:

```
https://github.com/yakovmanshin/YMFF
```

You’re then prompted to select the version to install and indicate the desired update policy. I recommend starting with the latest version (it’s selected automatically), and choosing “up to next major” as the preferred update rule. Once you click Next, the package is fetched. Then select the target you’re going to use YMFF in. Click Finish, and you’re ready to go.
You’re then prompted to select the version to install and indicate the desired update policy. I recommend starting with the latest version (it’s selected automatically), and choosing “up to next major” as the update rule. Then select the target you want to link YMFF to. Click Finishand you’re ready to go!

If you need to use YMFF in another Swift package, add it to the `Package.swift` file as a dependency:

```swift
.package(url: "https://github.com/yakovmanshin/YMFF", .upToNextMajor(from: "3.1.0"))
.package(url: "https://github.com/yakovmanshin/YMFF", from: "4.0.0")
```

### CocoaPods
YMFF alternatively supports installation via [CocoaPods](https://youtu.be/iEAjvNRdZa0).

YMFF supports installation via [CocoaPods](https://youtu.be/iEAjvNRdZa0), but please keep in mind this support is provided on the best-effort basis.

Add the following to your Podfile:

```ruby
pod 'YMFF', '~> 3.1'
pod 'YMFF', '~> 4.0'
```

</details>

## Setup

YMFF relies on the concept of *feature-flag stores*—“sources of truth” for feature-flag values.

### Firebase Remote Config

Firebase Remote Config is one of the most popular tools to control feature flags remotely. YMFF integrates with Remote Config seamlessly, although with some manual action.

<details>
Expand All @@ -59,47 +63,55 @@ Firebase Remote Config is one of the most popular tools to control feature flags
import FirebaseRemoteConfig
import YMFFProtocols

extension RemoteConfig: FeatureFlagStoreProtocol {
extension RemoteConfig: SynchronousFeatureFlagStore {

public func containsValue(forKey key: String) -> Bool {
self.allKeys(from: .remote).contains(key)
}

public func value<Value>(forKey key: String) -> Value? {
public func valueSync<Value>(for key: FeatureFlagKey) -> Result<Value, FeatureFlagStoreError> {
// Remote Config returns a default value if the requested key doesn’t exist,
// so you need to check the key for existence explicitly.
guard containsValue(forKey: key) else { return nil }
guard allKeys(from: .remote).contains(key) else {
return .failure(.valueNotFound)
}

let remoteConfigValue = self[key]

let value: Value?
// You need to use different `RemoteConfigValue` methods, depending on the return type.
// I know, it doesn’t look fancy.
switch Value.self {
case is Bool.Type:
return remoteConfigValue.boolValue as? Value
value = remoteConfigValue.boolValue as? Value
case is Data.Type:
return remoteConfigValue.dataValue as? Value
value = remoteConfigValue.dataValue as? Value
case is Double.Type:
return remoteConfigValue.numberValue.doubleValue as? Value
value = remoteConfigValue.numberValue.doubleValue as? Value
case is Int.Type:
return remoteConfigValue.numberValue.intValue as? Value
value = remoteConfigValue.numberValue.intValue as? Value
case is String.Type:
return remoteConfigValue.stringValue as? Value
value = remoteConfigValue.stringValue as? Value
default:
return nil
value = nil
}

if let value {
return .success(value)
} else {
return .failure(.typeMismatch)
}
}

}
```

Now, `RemoteConfig` is a valid *feature-flag store*.
`RemoteConfig` is now a valid *feature-flag store*.

Alternatively, you can create a custom wrapper object. That’s what I tend to do in my projects to achieve greater flexibility and avoid tight coupling.
Alternatively, you can create a custom wrapper object. That’s what I do in my projects to avoid tight coupling.

</details>

## Usage

### Declaring Feature Flags

Here’s how you declare feature flags with YMFF:

```swift
Expand All @@ -108,109 +120,103 @@ import YMFF
// For convenience, organize feature flags in a separate namespace using an enum.
enum FeatureFlags {

// `resolver` references one or more feature flag stores.
private static var resolver = FeatureFlagResolver(configuration: .init(stores: [
// If you want to change feature flag values from within your app, you’ll need at least one mutable store.
// `RuntimeOverridesStore` is a YMFF-supplied object. It stores modified values until the app restarts.
.mutable(RuntimeOverridesStore()),
// `MyFeatureFlagStore.shared` is your object, conforming to `FeatureFlagStoreProtocol`.
.immutable(MyFeatureFlagStore.shared),
]))
// `resolver` references one or more feature-flag stores.
private static let resolver = FeatureFlagResolver(stores: [MyFeatureFlagStore.shared])

// Feature flags are initialized with three pieces of data:
// a key string, the default (fallback) value, and the resolver.
@FeatureFlag("promo_enabled", default: false, resolver: resolver)
static var promoEnabled
@FeatureFlag("ads_enabled", default: false, resolver: Self.resolver)
static var adsEnabled

// Feature flags aren’t limited to booleans. You can use any type of value!
@FeatureFlag("number_of_banners", default: 3, resolver: resolver)
@FeatureFlag("number_of_banners", default: 3, resolver: Self.resolver)
static var numberOfBanners

// Advanced: Sometimes you want to map raw values from the store
// to native values used in your app. `MyFeatureFlagStore` below
// stores values as strings, while the app uses an enum.
// To switch between them, you use a `FeatureFlagValueTransformer`.
@FeatureFlag(
"promo_unit_kind",
FeatureFlagValueTransformer { string in
PromoUnitKind(rawValue: string)
} rawValueFromValue: { kind in
kind.rawValue
"ad_unit_kind",
transformer: FeatureFlagValueTransformer { rawValue in
AdUnitKind(rawValue: rawValue)
} rawValueFromValue: { value in
value.rawValue
},
default: .image,
resolver: resolver
resolver: Self.resolver
)
static var promoUnitKind
static var adUnitKind

}

// You can use custom types for feature-flag values.
enum PromoUnitKind: String {
enum AdUnitKind: String {
case text
case image
case video
}
```

### Reading Values

To the code that makes use of a feature flag, the flag acts just like the type of its value:

```swift
if FeatureFlags.promoEnabled {
switch FeatureFlags.promoUnitKind {
if FeatureFlags.adsEnabled {
switch FeatureFlags.adUnitKind {
case .text:
displayPromoText()
displayAdText()
case .image:
displayPromoBanners(count: FeatureFlags.numberOfBanners)
displayAdBanners(count: FeatureFlags.numberOfBanners)
case .video:
playPromoVideo()
playAdVideo()
}
}
```

### Overriding Values

YMFF lets you override feature flag values in mutable stores from within your app. When you do, the new value is set to the first mutable store found in resolver configuration.
### Writing Values

Overriding a feature flag value is as simple as assigning a new value to the flag.
YMFF lets you write feature-flag values to mutable stores. It’s as simple as assigning a new value to the flag:

```swift
FeatureFlags.promoEnabled = true
FeatureFlags.adsEnabled = true
```

If you can set a value, you should also be able to remove it. And you can, indeed. Calling `removeValueFromMutableStore()` on `FeatureFlag`’s *projected value* (i.e. the `FeatureFlag` instance itself, as opposed to its *wrapped value*) removes the value from the first mutable feature flag store which contains one.
To remove the value, you call `removeValueFromMutableStores()` on `FeatureFlag`’s *projected value* (i.e. the `FeatureFlag` instance itself, as opposed to its *wrapped value*):

```swift
// Here `FeatureFlags.$promoEnabled` has the type `FeatureFlag<Bool>`,
// while `FeatureFlags.promoEnabled` is of type `Bool`.
FeatureFlags.$promoEnabled.removeValueFromMutableStore()
// Here `FeatureFlags.$adsEnabled` has the type `FeatureFlag<Bool>`,
// while `FeatureFlags.adsEnabled` is of type `Bool`.
FeatureFlags.$adsEnabled.removeValueFromMutableStore()
```

### `UserDefaults`

You can use `UserDefaults` to read and write feature flag values. Your changes will persist when the app is restarted.
You can use `UserDefaults` to read and write feature-flag values. Your changes will persist when the app restarts.

```swift
import YMFF

private static var resolver = FeatureFlagResolver(configuration: .init(stores: [.mutable(UserDefaultsStore())]))
private static let resolver = FeatureFlagResolver(stores: [UserDefaultsStore()])
```

That’s it!

### More

Feel free to browse the source files to learn more about the available options!
You can browse the source files to learn more about the available options.

## What’s in Store

## v4 Roadmap
* [[#96](https://github.com/yakovmanshin/YMFF/issues/96)] Support for asynchronous feature-flag stores
### [Next-Version Roadmap](https://github.com/yakovmanshin/YMFF/milestone/11)
* [[#124](https://github.com/yakovmanshin/YMFF/issues/124)] Swift macros for easier setup
* [[#113](https://github.com/yakovmanshin/YMFF/issues/113)] Thread-safety improvements
* ~~[[#104](https://github.com/yakovmanshin/YMFF/issues/104)] Minimum compiler version: Swift 5.5 (Xcode 13)~~
* ~~[[#106](https://github.com/yakovmanshin/YMFF/issues/106)] Minimum deployment target: iOS 13, macOS 10.15~~
* [[#150](https://github.com/yakovmanshin/YMFF/issues/150)] Support for optional values in `UserDefaultsStore`
* [[#144](https://github.com/yakovmanshin/YMFF/issues/144)] Minimum compiler version: Swift 5.9 (Xcode 15)

YMFF v4 is expected to be released in 2024.
This version is expected in late 2024, after Swift 6 is released.

## License and Copyright
YMFF is licensed under the Apache License. See the [LICENSE file](https://github.com/yakovmanshin/YMFF/blob/main/LICENSE) for details.
### Ideas & Bug Reports

© 2020–2024 Yakov Manshin
Feel free to open a new issue if something’s not working—or if you have a suggestion.
24 changes: 12 additions & 12 deletions Sources/YMFF/FeatureFlag/FeatureFlag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@
import YMFFProtocols
#endif

/// An object that facilitates access to feature flag values.
/// The object which provides easy access to feature-flag values.
@propertyWrapper
final public class FeatureFlag<RawValue, Value> {

// MARK: Properties

/// The key used to retrieve feature flag values.
/// The key used to retrieve feature-flag values.
public let key: FeatureFlagKey

private let transformer: FeatureFlagValueTransformer<RawValue, Value>

/// The fallback value returned when no store is able to provide the real one.
/// The fallback value returned when the actual one cannot be retrieved.
public let defaultValue: Value

private let resolver: any SynchronousFeatureFlagResolverProtocol

// MARK: Initializers

/// Creates a new `FeatureFlag`.
///
///
/// - Parameters:
/// - key: *Required.* The key used to address feature flag values in stores.
/// - transformer: *Required.* The object that transforms raw values into values, and vice versa.
/// - defaultValue: *Required.* The value returned in case all stores fail to provide a value.
/// - key: *Required.* The key used to address feature-flag values in stores.
/// - transformer: *Required.* The object that transforms raw values into client-type values, and vice versa.
/// - defaultValue: *Required.* The value returned in case no feature-flag store is able provide a value.
/// - resolver: *Required.* The resolver object used to retrieve values from stores.
public init(
_ key: FeatureFlagKey,
Expand All @@ -47,11 +47,11 @@ final public class FeatureFlag<RawValue, Value> {
self.resolver = resolver
}

/// Creates a new `FeatureFlag` with value and raw value of the same type.
/// Creates a new `FeatureFlag` whose value and raw value are of the same type.
///
/// - Parameters:
/// - key: *Required.* The key used to address feature flag values in stores.
/// - defaultValue: *Required.* The value returned in case all stores fail to provide a value.
/// - key: *Required.* The key used to address feature-flag values in stores.
/// - defaultValue: *Required.* The value returned in case no feature-flag store is able provide a value.
/// - resolver: *Required.* The resolver object used to retrieve values from stores.
public convenience init(
_ key: FeatureFlagKey,
Expand Down Expand Up @@ -79,12 +79,12 @@ final public class FeatureFlag<RawValue, Value> {

// MARK: Projected Value

/// The object returned when referencing the feature flag with a dollar sign (`$`).
/// The feature-flag object itself, returned when the feature flag is referenced with a dollar sign (`$`).
public var projectedValue: FeatureFlag<RawValue, Value> { self }

// MARK: Mutable Value Removal

/// Removes the value from the first mutable feature flag store which contains one for `key`.
/// Removes the value from all *synchronous* mutable feature-flag stores.
///
/// + Errors thrown by `resolver` are ignored.
public func removeValueFromMutableStores() {
Expand Down
2 changes: 1 addition & 1 deletion Sources/YMFF/FeatureFlag/FeatureFlagValueTransformer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

// MARK: - Transformer

/// An object used by `FeatureFlag` to transform raw values into native values, and vice versa.
/// The object used to transform raw values into client-type values, and vice versa.
public struct FeatureFlagValueTransformer<RawValue, Value> {

let valueFromRawValue: (RawValue) -> Value?
Expand Down
12 changes: 6 additions & 6 deletions Sources/YMFF/FeatureFlagResolver/FeatureFlagResolver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import YMFFProtocols

// MARK: - FeatureFlagResolver

/// A concrete, YMFF-supplied implementation of the feature flag resolver.
/// The concrete, YMFF-supplied implementation of the *synchronous* feature-flag resolver.
final public class FeatureFlagResolver {

// MARK: Properties
Expand All @@ -21,18 +21,18 @@ final public class FeatureFlagResolver {

// MARK: Initializers

/// Initializes the resolver with an object that conforms to `FeatureFlagResolverConfiguration`.
/// Initializes the resolver with an object which conforms to `FeatureFlagResolverConfiguration`.
///
/// - Parameter configuration: *Required.* The configuration used to read and write feature flag values.
/// - Parameter configuration: *Required.* The configuration used to read and write feature-flag values.
public init(configuration: any FeatureFlagResolverConfiguration) {
self.configuration = configuration
}

/// Initializes the resolver with the list of feature flag stores.
/// Initializes the resolver with an array of feature-flag stores.
///
/// + Passing in an empty array will produce the `noStoreAvailable` error on next read attempt.
/// + Passing an empty array will result in a `noStoreAvailable` error on the next read attempt.
///
/// - Parameter stores: *Required.* The array of feature flag stores.
/// - Parameter stores: *Required.* The array of feature-flag stores.
public convenience init(stores: [any FeatureFlagStore]) {
let configuration = Configuration(stores: stores)
self.init(configuration: configuration)
Expand Down
Loading

0 comments on commit a0a1205

Please sign in to comment.