firebase / firebase/firebase-ios-sdk

[FR]: Support for CodableWithConfiguration

Open
#13,552 4 comments 2 reactions 1 assignee Claimed by @MarkDuckworth View on GitHub
api: firestore Swift API type: feature request
Dominant language
C++
Stars
6.7k
Forks
1.8k
Avg merge
2d 18h
Merged PRs (30d)
75

Description

### Description

1. Firestore currently supports passing `Codable` ( `Encodable` or `Decodable` respectively) types when retrieving or setting the data of a document. iOS 17 introduced a new set of protocols: [`CodableWithConfiguration`](https://developer.apple.com/documentation/foundation/codablewithconfiguration) (a type alias for [`EncodableWithConfiguration`](https://developer.apple.com/documentation/foundation/encodablewithconfiguration) & [`DecodableWithConfiguration`](https://developer.apple.com/documentation/foundation/decodablewithconfiguration)) that allow passing an additional, fully typed `Configuration` type alongside the encode and decode methods. New [`encode(_:configuration:)`](https://developer.apple.com/documentation/foundation/jsonencoder/4273653-encode) and [`decode(_:configuration:)`](https://developer.apple.com/documentation/foundation/jsondecoder/4273651-decode) methods on e.g. the JSON coders allow to pass a configuration alongside the type/value.
2. It is currently not possible to work with `CodableWithConfiguration`-conforming types when using Firestore without the need to create additional wrapper types.
3. Similar to `Codable` types, Firestore should accept `CodableWithConfiguration` types by allowing to pass an additional `configuration` argument.

### API Proposal

The following additive API changes are proposed. They are expressed as Swift Extensions with the implementation body missing.

```swift
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension Firestore.Encoder {
public func encode(
_ value: T,
configuration: T.EncodingConfiguration
) throws -> [String: Any]

public func encode(
_ value: T,
configuration: C.Type
) throws -> [String: Any] where T.EncodingConfiguration == C.EncodingConfiguration
}

@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
extension Firestore.Decoder {
public func decode(
_ type: T.Type,
from data: Any,
configuration: T.DecodingConfiguration
) throws -> T

public func decode(
_ type: T.Type,
from data: Any,
configuration: C.Type
) throws -> T where T.DecodingConfiguration == C.DecodingConfiguration
}
```

Additionally, one would need to add additional overloads for the factious `setData`, `data`, `getDocument`, `setDocument` methods in types like `DocumentReference`, `DocumentSnapshot`, `CollectionReference`, `Transaction` and `WriteBatch` (might be missing some). Each of them would ned similar adjustments of using the new protocols and accepting an additional `configuration` parameter that is forwarded to the Encoder/Decoder.

### Firebase Product(s)

Firestore

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.