Skip to content

Commit a9cd2d8

Browse files
ncooke3themiswang
authored andcommitted
[Config] Add encoder and decoder param to public codable APIs (#14525)
1 parent 81ab6f9 commit a9cd2d8

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

Crashlytics/Crashlytics/Components/FIRCLSApplication.h

-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ UIApplication* FIRCLSApplicationSharedInstance(void);
8383
id FIRCLSApplicationSharedInstance(void);
8484
#endif
8585

86-
void FIRCLSApplicationOpenURL(NSURL* url,
87-
NSExtensionContext* extensionContext,
88-
void (^completionBlock)(BOOL success));
89-
9086
id<NSObject> FIRCLSApplicationBeginActivity(NSActivityOptions options, NSString* reason);
9187
void FIRCLSApplicationEndActivity(id<NSObject> activity);
9288

Crashlytics/Crashlytics/Components/FIRCLSApplication.m

-26
Original file line numberDiff line numberDiff line change
@@ -155,32 +155,6 @@ id FIRCLSApplicationSharedInstance(void) {
155155
}
156156
#endif
157157

158-
void FIRCLSApplicationOpenURL(NSURL* url,
159-
NSExtensionContext* extensionContext,
160-
void (^completionBlock)(BOOL success)) {
161-
if (extensionContext) {
162-
[extensionContext openURL:url completionHandler:completionBlock];
163-
return;
164-
}
165-
166-
BOOL result = NO;
167-
168-
#if TARGET_OS_IOS
169-
// What's going on here is the value returned is a scalar, but we really need an object to
170-
// call this dynamically. Hoops must be jumped.
171-
NSInvocationOperation* op =
172-
[[NSInvocationOperation alloc] initWithTarget:FIRCLSApplicationSharedInstance()
173-
selector:@selector(openURL:)
174-
object:url];
175-
[op start];
176-
[op.result getValue:&result];
177-
#elif CLS_TARGET_OS_OSX
178-
result = [[NSClassFromString(@"NSWorkspace") sharedWorkspace] openURL:url];
179-
#endif
180-
181-
completionBlock(result);
182-
}
183-
184158
id<NSObject> FIRCLSApplicationBeginActivity(NSActivityOptions options, NSString* reason) {
185159
if ([[NSProcessInfo processInfo] respondsToSelector:@selector(beginActivityWithOptions:
186160
reason:)]) {

FirebaseRemoteConfig/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Unreleased
2+
- [fixed] Codable APIs now accept optional `FirebaseDataEncoder` and
3+
`FirebaseDataDecoder` parameters, allowing for customization of
4+
encoding/decoding behavior. (#14368)
5+
16
# 11.9.0
27
- [fixed] Mark internal `fetchSession` property as `atomic` to prevent a concurrency
38
related crash. (#14449)

FirebaseRemoteConfig/Swift/Codable.swift

+6-4
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,21 @@ public extension RemoteConfig {
4747
/// Decodes a struct from the respective Remote Config values.
4848
///
4949
/// - Parameter asType: The type to decode to.
50-
func decoded<Value: Decodable>(asType: Value.Type = Value.self) throws -> Value {
50+
func decoded<Value: Decodable>(asType: Value.Type = Value.self,
51+
decoder: FirebaseDataDecoder = .init()) throws -> Value {
5152
let keys = allKeys(from: RemoteConfigSource.default) + allKeys(from: RemoteConfigSource.remote)
5253
let config = keys.reduce(into: [String: FirebaseRemoteConfigValueDecoderHelper]()) {
5354
$0[$1] = FirebaseRemoteConfigValueDecoderHelper(value: configValue(forKey: $1))
5455
}
55-
return try FirebaseDataDecoder().decode(Value.self, from: config)
56+
return try decoder.decode(Value.self, from: config)
5657
}
5758

5859
/// Sets config defaults from an encodable struct.
5960
///
6061
/// - Parameter value: The object to use to set the defaults.
61-
func setDefaults<Value: Encodable>(from value: Value) throws {
62-
guard let encoded = try FirebaseDataEncoder().encode(value) as? [String: NSObject] else {
62+
func setDefaults<Value: Encodable>(from value: Value,
63+
encoder: FirebaseDataEncoder = .init()) throws {
64+
guard let encoded = try encoder.encode(value) as? [String: NSObject] else {
6365
throw RemoteConfigCodableError.invalidSetDefaultsInput(
6466
"The setDefaults input: \(value), must be a Struct that encodes to a Dictionary"
6567
)

FirebaseRemoteConfig/Tests/Swift/SwiftAPI/Codable.swift

+32
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
// limitations under the License.
1414

1515
import FirebaseRemoteConfig
16+
import FirebaseSharedSwift
1617

1718
import XCTest
1819

@@ -206,4 +207,35 @@ class CodableTests: APITestBase {
206207
XCTAssertEqual(readDefaults.arrayValue, ["foo", "bar", "baz"])
207208
XCTAssertEqual(readDefaults.arrayIntValue, [1, 2, 0, 3])
208209
}
210+
211+
// MARK: - Test using injected encoder/decoder.
212+
213+
func testDateEncodingAndDecodingWithNonDefaultCoders() throws {
214+
// Given
215+
struct DateDefaults: Codable {
216+
let date: Date
217+
}
218+
219+
let defaults = DateDefaults(date: Date())
220+
221+
// When
222+
let encoder = FirebaseDataEncoder()
223+
encoder.dateEncodingStrategy = .iso8601
224+
try config.setDefaults(from: defaults, encoder: encoder)
225+
226+
// - Uses default decoder that won't decode ISO8601 format.
227+
let improperlyDecodedDefaults: DateDefaults = try config.decoded()
228+
XCTAssertNotEqual(improperlyDecodedDefaults.date, defaults.date)
229+
230+
// Then
231+
let decoder = FirebaseDataDecoder()
232+
decoder.dateDecodingStrategy = .iso8601
233+
234+
let decodedDefaults: DateDefaults = try config.decoded(decoder: decoder)
235+
XCTAssert(Calendar.current.isDate(
236+
decodedDefaults.date,
237+
equalTo: defaults.date,
238+
toGranularity: .second
239+
))
240+
}
209241
}

0 commit comments

Comments
 (0)