|
1 | 1 | # DynamicCodable
|
2 | 2 |
|
3 |
| -A description of this package. |
| 3 | +`DynamicCodable` aims to make it easy to preserve arbitrary `Codable` data structures when unarchiving, allowing you to inspect those structures in a type safe way after the decoding is finished. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +Add `DynamicCodable` as a dependency in your `Package.swift` file to start using it. Then, add `import DynamicCodable` to any file you wish to use the library in. |
| 8 | + |
| 9 | +Please check the [releases](https://github.com/mochidev/DynamicCodable/releases) for recommended versions. |
| 10 | + |
| 11 | +```swift |
| 12 | +dependencies: [ |
| 13 | + .package(url: "https://github.com/mochidev/DynamicCodable.git", .upToNextMinor(from: "0.1.0")), |
| 14 | +], |
| 15 | +... |
| 16 | +targets: [ |
| 17 | + .target( |
| 18 | + name: "MyPackage", |
| 19 | + dependencies: [ |
| 20 | + "DynamicCodable", |
| 21 | + ] |
| 22 | + ) |
| 23 | +] |
| 24 | +``` |
| 25 | + |
| 26 | +## What is `DynamicCodable`? |
| 27 | + |
| 28 | +`DynamicCodable` is an enumeration that represents the various primitive types that are themselves codable. Using it is easy — simply mark the portion of your graph as being of type `DynamicCodable` to enable that subtree to be preserved during decoding: |
| 29 | + |
| 30 | +```swift |
| 31 | +struct ServerResponse: Codable { |
| 32 | + let status: String |
| 33 | + let metadata: DynamicCodable |
| 34 | +} |
| 35 | + |
| 36 | +let response = try JSONDecoder().decode(ServerResponse.self, from: data) |
| 37 | + |
| 38 | +// Consume the metadata whole |
| 39 | +print(response.metadata) |
| 40 | + |
| 41 | +// Dig into the metadata — it's just an enum! |
| 42 | +switch response.metadata { |
| 43 | +case .keyed(let dictionary) |
| 44 | + print(dictionary["debugInfo"], default: .empty) // Convenience for dictionary[.string("debugInfo"), default: .empty] |
| 45 | +case .bool(false): |
| 46 | + print("Metadata disabled") |
| 47 | +case .nil: |
| 48 | + print("Metadata unavailable") |
| 49 | +default: break |
| 50 | +} |
| 51 | +``` |
| 52 | + |
| 53 | +Note that `DynamicCodable` is not limited to be used with JSON coders - it can be used with any `Codable`-compatible coder! |
| 54 | + |
| 55 | +## Contributing |
| 56 | + |
| 57 | +Contribution is welcome! Please take a look at the issues already available, or start a new issue to discuss a new feature. Although guarantees can't be made regarding feature requests, PRs that fit with the goals of the project and that have been discussed before hand are more than welcome! |
| 58 | + |
| 59 | +Please make sure that all submissions have clean commit histories, are well documented, and thoroughly tested. **Please rebase your PR** before submission rather than merge in `main`. Linear histories are required. |
0 commit comments