Merge pull request #1 from MPLew-is/flattened-intermediate-keys
Add flattened shortcuts for long paths without branching
This eliminates a lot of the boilerplate for specifying intermediate keys.
Thanks Sam Deane for the suggestion!
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802032778号
DeepCodable: Encode and decode deeply-nested data into flat Swift objectsHave you ever gotten a response from an API that looked like this and wanted to pull out and flatten the values you care about? (This is a real response from the GitHub GraphQL API, with only the actual values changed)
DeepCodablelets you easily do so in Swift while maintaining type-safety, with the magic of result builders, key paths, and property wrappers:Quick start
Add to your
Package.Swift:Conform a type you want to decode to
DeepDecodableby defining a coding tree representing which nodes are bound to which values:Nodes in your
codingTreeare made ofKeys initialized one of the following ways:Key("name") { /* More Keys */ }: node that don’t capture values directly, but contain other nodes{"name": { ... } }Key("name", containing: \._value): node that should be decoded into thevaluepropertyAll values to decode must be wrapped with the
@Valueproperty wrapper, and the\._{name}syntax refers directly to the wrapping instance (\.{name}without the underscore refers to the actual underlying value).Decode a value into an instance of your type:
DeepCodableis built on top of normalCodable, so any decoder (like the property list decoder inFoundationor the excellent third-party YAML decoder, Yams) can be used to decode values.Encoding
While decoding is probably the most common use-case for this type of nested decoding, this package also supports encoding a flat Swift struct into a deeply nested one with the same pattern:
With encoding, you don’t have to use the
@Valuewrappers, though you can if you’d like to support decoding and encoding on the same type (in which case you can conform toDeepCodableas an alias for the two).Key features
Encoding and decoding a Swift object to/from an arbitrarily complex deeply nested serialized representation without manually writing
CodableimplementationsPreservation of existing
Codablebehavior on the values being encoded/decoded, including custom typesDeepCodableis just a custom implementation of theCodablerequirements, this also means you can nestDeepCodableobjects like in theGithubGraphqlResponseexampleWhen conforming to
DeepEncodableorDeepDecodable, don’t interfere with the opposite normalCodableimplementation (Decodable/Encodable, respectively)struct Response: DeepDecodable, Encodable { ... }and decode from a deeply nested tree, and then re-encode back to a flat structure like normalEncodableobjectsNo requirement for
@Valueproperty wrapper for types only conforming toDeepEncodableOmission of the corresponding tree sections when all values at the leaves are
nilnilvalue doesn’t result in something like{"top": {"second": {"third": null} } }Flattened shortcuts using variadic parameters for long paths with no branching:
Key("topLevel", "secondLevel", containing: \._property)instead ofKey("topLevel") { Key("secondLevel", containing: \._property) }