Created
December 3, 2020 13:04
-
-
Save HugoSay/93b2c869ab862f14fbcfd44fca525867 to your computer and use it in GitHub Desktop.
Property wrapper for firebase remote config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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