Skip to content

Instantly share code, notes, and snippets.

@HugoSay
Created December 3, 2020 13:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HugoSay/93b2c869ab862f14fbcfd44fca525867 to your computer and use it in GitHub Desktop.
Save HugoSay/93b2c869ab862f14fbcfd44fca525867 to your computer and use it in GitHub Desktop.
Property wrapper for firebase remote config
//
// FCRemoteConfig.swift
// Brut
//
// Created by Hugo Saynac on 25/11/2020.
//
import FirebaseRemoteConfig
private let remoteConfigDecoder = JSONDecoder()
@propertyWrapper struct FCRemoteConfig<Value: Decodable> {
let key: String
let defaultValue: Value
var wrappedValue: Value {
get {
do {
let data = RemoteConfig.remoteConfig()
.configValue(forKey: key).dataValue
return try remoteConfigDecoder.decode(Value.self, from: data)
} catch {
Log(.firebase, .error, "Error fetching remote config\n\(error)")
return defaultValue
}
}
set {
fatalError("Cannot set a remote config value")
}
}
}
protocol FeatureFlag {
static var `default`: Self { get }
static var key: String { get }
}
extension FCRemoteConfig where Value: FeatureFlag {
init() {
self.key = Value.key
self.defaultValue = Value.default
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment