Skip to content

Commit e74262b

Browse files
committed
decodedValue subscript
1 parent 78de216 commit e74262b

File tree

2 files changed

+11
-45
lines changed

2 files changed

+11
-45
lines changed

FirebaseRemoteConfigSwift/Sources/Value.swift

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -20,40 +20,8 @@ import FirebaseRemoteConfig
2020
/// Implements subscript overloads to enable Remote Config values to be accessed
2121
/// in a type-safe way directly from the current config.
2222
public extension RemoteConfig {
23-
subscript(stringValue key: String) -> String {
24-
guard let value = configValue(forKey: key).stringValue else {
25-
// An empty string is the historical behavior for an non-existent key.
26-
return ""
27-
}
28-
return value
29-
}
30-
31-
subscript(intValue key: String) -> Int {
32-
return configValue(forKey: key).numberValue.intValue
33-
}
34-
35-
subscript(uintValue key: String) -> UInt {
36-
return configValue(forKey: key).numberValue.uintValue
37-
}
38-
39-
subscript(numberValue key: String) -> NSNumber {
40-
return configValue(forKey: key).numberValue
41-
}
42-
43-
subscript(floatValue key: String) -> Float {
44-
return configValue(forKey: key).numberValue.floatValue
45-
}
46-
47-
subscript(doubleValue key: String) -> Double {
48-
return configValue(forKey: key).numberValue.doubleValue
49-
}
50-
51-
subscript(boolValue key: String) -> Bool {
52-
return configValue(forKey: key).boolValue
53-
}
54-
55-
subscript(dataValue key: String) -> Data {
56-
return configValue(forKey: key).dataValue
23+
subscript<T: Decodable>(decodedValue key: String) -> T? {
24+
return try? configValue(forKey: key).decoded()
5725
}
5826

5927
subscript(jsonValue key: String) -> [String: AnyHashable]? {

FirebaseRemoteConfigSwift/Tests/SwiftAPI/Value.swift

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,14 @@ import XCTest
5555
func testStrongTypingViaSubscriptApi() async throws {
5656
let status = try await config.fetchAndActivate()
5757
XCTAssertEqual(status, .successFetchedFromRemote)
58-
XCTAssertEqual(config[stringValue: Constants.stringKey], Constants.stringValue)
59-
XCTAssertEqual(config[intValue: Constants.intKey], Constants.intValue)
60-
XCTAssertEqual(config[numberValue: Constants.intKey],
61-
NSNumber(integerLiteral: Constants.intValue))
62-
XCTAssertEqual(config[floatValue: Constants.floatKey], Constants.floatValue)
63-
XCTAssertEqual(config[doubleValue: Constants.floatKey], Constants.doubleValue)
64-
XCTAssertEqual(config[boolValue: Constants.trueKey], true)
65-
XCTAssertEqual(config[boolValue: Constants.falseKey], false)
66-
XCTAssertEqual(
67-
config[dataValue: Constants.stringKey],
58+
XCTAssertEqual(config[decodedValue: Constants.stringKey], Constants.stringValue)
59+
XCTAssertEqual(config[decodedValue: Constants.intKey], Constants.intValue)
60+
XCTAssertEqual(config[decodedValue: Constants.floatKey], Constants.floatValue)
61+
XCTAssertEqual(config[decodedValue: Constants.floatKey], Constants.doubleValue)
62+
XCTAssertEqual(config[decodedValue: Constants.trueKey], true)
63+
XCTAssertEqual(config[decodedValue: Constants.falseKey], false)
64+
XCTAssertEqual(
65+
config[decodedValue: Constants.stringKey],
6866
Constants.stringValue.data(using: .utf8)
6967
)
7068
XCTAssertEqual(try XCTUnwrap(config[jsonValue: Constants.jsonKey]), Constants.jsonValue)
@@ -174,7 +172,7 @@ import XCTest
174172
}
175173

176174
func testStringFails() {
177-
XCTAssertEqual(config[stringValue: "UndefinedKey"], "")
175+
XCTAssertEqual(config[decodedValue: "UndefinedKey"], "")
178176
}
179177

180178
func testJSONFails() {

0 commit comments

Comments
 (0)