Compare commits

...

2 Commits

8 changed files with 246 additions and 10 deletions
@@ -19,7 +19,14 @@ let singleDogData = try! JSONEncoder().encode(singleDogDocument)
// MARK: - Parse a request or response body with one Dog in it
let dogResponse = try! JSONDecoder().decode(SingleDogDocument.self, from: singleDogData)
let dogFromData = dogResponse.body.primaryData?.value
let dogFromData = dogResponse.body.primaryResource?.value
let dogOwner: Person.Identifier? = dogFromData.flatMap { $0 ~> \.owner }
// MARKL - Parse a request or response body with one Dog in it using an alternative model
typealias AltSingleDogDocument = JSONAPI.Document<SingleResourceBody<AlternativeDog>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>
let altDogResponse = try! JSONDecoder().decode(AltSingleDogDocument.self, from: singleDogData)
let altDogFromData = altDogResponse.body.primaryResource?.value
let altDogHuman: Person.Identifier? = altDogFromData.flatMap { $0 ~> \.human }
// MARK: - Create a request or response with multiple people and dogs and houses included
let personIds = [Person.Identifier(), Person.Identifier()]
@@ -36,7 +43,7 @@ let batchPeopleData = try! JSONEncoder().encode(batchPeopleDocument)
// MARK: - Parse a request or response body with multiple people in it and dogs and houses included
let peopleResponse = try! JSONDecoder().decode(BatchPeopleDocument.self, from: batchPeopleData)
let peopleFromData = peopleResponse.body.primaryData?.values
let peopleFromData = peopleResponse.body.primaryResource?.values
let dogsFromData = peopleResponse.body.includes?[Dog.self]
let housesFromData = peopleResponse.body.includes?[House.self]
+28
View File
@@ -91,6 +91,34 @@ public enum DogDescription: EntityDescription {
public typealias Dog = ExampleEntity<DogDescription>
public enum AlternativeDogDescription: EntityDescription {
public static var type: String { return "dogs" }
public struct Attributes: JSONAPI.Attributes {
public let name: Attribute<String>
public init(name: Attribute<String>) {
self.name = name
}
}
public struct Relationships: JSONAPI.Relationships {
public let human: ToOne<Person?>
public init(human: ToOne<Person?>) {
self.human = human
}
// define custom key mapping:
enum CodingKeys: String, CodingKey {
case human = "owner"
}
}
}
public typealias AlternativeDog = ExampleEntity<AlternativeDogDescription>
public extension Entity where Description == DogDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String {
public init(name: String, owner: Person?) throws {
self = try Dog(attributes: .init(name: .init(rawValue: name)), relationships: DogDescription.Relationships(owner: .init(entity: owner)), meta: .none, links: .none)
+76 -1
View File
@@ -227,6 +227,11 @@ typealias Attributes = NoAttributes
let favoriteColor: String = person[\.favoriteColor]
```
NOTE: Because of support for computed properties that are not wrapped in `Attribute`, `TransformedAttribute`, or `ValidatedAttribute`, the compiler cannot always infer the type of thing you want back when using subscript attribute access. The following code is ambiguous about whether it should return a `String` or an `Attribute<String>`:
```
let favoriteColor = person[\.favoriteColor]
```
#### `Transformer`
Sometimes you need to use a type that does not encode or decode itself in the way you need to represent it as a serialized JSON object. For example, the Swift `Foundation` type `Date` can encode/decode itself to `Double` out of the box, but you might want to represent dates as ISO 8601 compliant `String`s instead. The Foundation library `JSONDecoder` has a setting to make this adjustment, but for the sake of an example, you could create a `Transformer`.
@@ -265,7 +270,7 @@ You can also creator `Validators` and `ValidatedAttribute`s. A `Validator` is ju
#### Computed `Attribute`
You can add computed properties to your `EntityDescription.Attributes` struct if you would like to expose attributes that are not explicitly represented by the JSON. These computed properties should still have the type `Attribute` because that way you can take advantage of the quick access provided by `Entity`'s subscript operator. Here's an example of how you might take the `Person[\.name]` attribute from the example above and create a `fullName` computed property.
You can add computed properties to your `EntityDescription.Attributes` struct if you would like to expose attributes that are not explicitly represented by the JSON. These computed properties do not have to be wrapped in `Attribute`, `ValidatedAttribute`, or `TransformedAttribute`. This allows computed attributes to be of types that are not `Codable`. Here's an example of how you might take the `Person[\.name]` attribute from the example above and create a `fullName` computed property.
```
public var fullName: Attribute<String> {
@@ -394,5 +399,75 @@ extension String: CreatableRawIdType {
}
```
### Custom Attribute or Relationship Key Mapping
There is not anything special going on at the `JSONAPI.Attributes` and `JSONAPI.Relationships` levels, so you can easily provide custom key mappings by taking advantage of `Codable`'s `CodingKeys` pattern. Here are two models that will encode/decode equivalently but offer different naming in your codebase:
```
public enum EntityDescription1: JSONAPI.EntityDescription {
public static var type: String { return "entity" }
public struct Attributes: JSONAPI.Attributes {
public let coolProperty: Attribute<String>
}
public typealias Relationships = NoRelationships
}
public enum EntityDescription2: JSONAPI.EntityDescription {
public static var type: String { return "entity" }
public struct Attributes: JSONAPI.Attributes {
public let wholeOtherThing: Attribute<String>
enum CodingKeys: String, CodingKey {
case wholeOtherThing = "coolProperty"
}
}
}
```
### Custom Attribute Encode/Decode
You can safely provide your own encoding or decoding functions for your Attributes struct if you need to as long as you are careful that your encode operation correctly reverses your decode operation. Although this is generally not necessary, `AttributeType` provides a convenience method to make your decoding a bit less boilerplate ridden. This is what it looks like:
```
public enum EntityDescription1: JSONAPI.EntityDescription {
public static var type: String { return "entity" }
public struct Attributes: JSONAPI.Attributes {
public let property1: Attribute<String>
public let property2: Attribute<Int>
public let property3: Attribute<String>
public let weirdThing: Attribute<String>
enum CodingKeys: String, CodingKey {
case property1
case property2
case property3
}
}
public typealias Relationships = NoRelationships
}
extension EntityDescription1.Attributes {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
property1 = try .defaultDecoding(from: container, forKey: .property1)
property2 = try .defaultDecoding(from: container, forKey: .property2)
property3 = try .defaultDecoding(from: container, forKey: .property3)
weirdThing = "hello world"
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(property1, forKey: .property1)
try container.encode(property2, forKey: .property2)
try container.encode(property3, forKey: .property3)
}
}
```
# JSONAPITestLib
The `JSONAPI` framework is packaged with a test library to help you test your `JSONAPI` integration. The test library is called `JSONAPITestLib`. It provides literal expressibility for `Attribute`, `ToOneRelationship`, and `Id` in many situations so that you can easily write test `Entity` values into your unit tests. It also provides a `check()` function for each `Entity` type that can be used to catch problems with your `JSONAPI` structures that are not caught by Swift's type system. You can see the `JSONAPITestLib` in action in the Playground included with the `JSONAPI` repository.
+8
View File
@@ -84,3 +84,11 @@ extension TransformedAttribute {
try container.encode(rawValue)
}
}
// MARK: Attribute decoding and encoding defaults
extension AttributeType {
public static func defaultDecoding<Container: KeyedDecodingContainerProtocol>(from container: Container, forKey key: Container.Key) throws -> Self {
return try container.decode(Self.self, forKey: key)
}
}
+6 -5
View File
@@ -8,11 +8,11 @@
/// A JSON API structure within an Entity that contains
/// named properties of types `ToOneRelationship` and
/// `ToManyRelationship`.
public typealias Relationships = Codable & Equatable
public protocol Relationships: Codable & Equatable {}
/// A JSON API structure within an Entity that contains
/// properties of any types that are JSON encodable.
public typealias Attributes = Codable & Equatable
public protocol Attributes: Codable & Equatable {}
/// Can be used as `Relationships` Type for Entities that do not
/// have any Relationships.
@@ -556,9 +556,10 @@ public extension Entity {
let maybeUnidentified = Unidentified() as? EntityRawIdType
id = try maybeUnidentified.map { Entity.Id(rawValue: $0) } ?? container.decode(Entity.Id.self, forKey: .id)
attributes = try (NoAttributes() as? Description.Attributes) ?? container.decode(Description.Attributes.self, forKey: .attributes)
attributes = try (NoAttributes() as? Description.Attributes) ??
container.decode(Description.Attributes.self, forKey: .attributes)
relationships = try (NoRelationships() as? Description.Relationships) ?? container.decode(Description.Relationships.self, forKey: .relationships)
meta = try (NoMetadata() as? MetaType) ?? container.decode(MetaType.self, forKey: .meta)
@@ -0,0 +1,107 @@
//
// CustomAttributesTests.swift
// JSONAPITests
//
// Created by Mathew Polzin on 12/27/18.
//
import XCTest
@testable import JSONAPI
import JSONAPITestLib
class CustomAttributesTests: XCTestCase {
func test_customDecode() {
let entity = decoded(type: CustomAttributeEntity.self, data: customAttributeEntityData)
XCTAssertEqual(entity[\.firstName], "Cool")
XCTAssertEqual(entity[\.name], "Cool Name")
XCTAssertNoThrow(try CustomAttributeEntity.check(entity))
}
func test_customEncode() {
test_DecodeEncodeEquality(type: CustomAttributeEntity.self,
data: customAttributeEntityData)
}
func test_customKeysDecode() {
let entity = decoded(type: CustomKeysEntity.self, data: customAttributeEntityData)
XCTAssertEqual(entity[\.firstNameSilly], "Cool")
XCTAssertEqual(entity[\.lastNameSilly], "Name")
XCTAssertNoThrow(try CustomKeysEntity.check(entity))
}
func test_customKeysEncode() {
test_DecodeEncodeEquality(type: CustomKeysEntity.self,
data: customAttributeEntityData)
}
}
// MARK: - Test Types
extension CustomAttributesTests {
enum CustomAttributeEntityDescription: EntityDescription {
public static var type: String { return "test1" }
public struct Attributes: JSONAPI.Attributes {
let firstName: Attribute<String>
public let name: Attribute<String>
private enum CodingKeys: String, CodingKey {
case firstName
case lastName
}
}
public typealias Relationships = NoRelationships
}
typealias CustomAttributeEntity = BasicEntity<CustomAttributeEntityDescription>
enum CustomKeysEntityDescription: EntityDescription {
public static var type: String { return "test1" }
public struct Attributes: JSONAPI.Attributes {
public let firstNameSilly: Attribute<String>
public let lastNameSilly: Attribute<String>
enum CodingKeys: String, CodingKey {
case firstNameSilly = "firstName"
case lastNameSilly = "lastName"
}
}
public typealias Relationships = NoRelationships
}
typealias CustomKeysEntity = BasicEntity<CustomKeysEntityDescription>
}
extension CustomAttributesTests.CustomAttributeEntityDescription.Attributes {
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
firstName = try .defaultDecoding(from: container, forKey: .firstName)
let lastName = try container.decode(String.self, forKey: .lastName)
name = firstName.map { "\($0) \(lastName)" }
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(firstName, forKey: .firstName)
let lastName = String(name.value.split(separator: " ")[1])
try container.encode(lastName, forKey: .lastName)
}
}
// MARK: - Test Data
private let customAttributeEntityData = """
{
"type": "test1",
"id": "1",
"attributes": {
"firstName": "Cool",
"lastName": "Name"
}
}
""".data(using: .utf8)!
@@ -61,7 +61,7 @@ extension EntityCheckTests {
enum EnumAttributesDescription: EntityDescription {
public static var type: String { return "hello" }
public enum Attributes: Codable, Equatable {
public enum Attributes: JSONAPI.Attributes {
case hello
public init(from decoder: Decoder) throws {
@@ -82,7 +82,7 @@ extension EntityCheckTests {
public typealias Attributes = NoAttributes
public enum Relationships: Codable, Equatable {
public enum Relationships: JSONAPI.Relationships {
case hello
public init(from decoder: Decoder) throws {
+10
View File
@@ -73,6 +73,15 @@ extension ComputedPropertiesTests {
]
}
extension CustomAttributesTests {
static let __allTests = [
("test_customDecode", test_customDecode),
("test_customEncode", test_customEncode),
("test_customKeysDecode", test_customKeysDecode),
("test_customKeysEncode", test_customKeysEncode),
]
}
extension DocumentTests {
static let __allTests = [
("test_errorDocumentFailsWithNoAPIDescription", test_errorDocumentFailsWithNoAPIDescription),
@@ -399,6 +408,7 @@ public func __allTests() -> [XCTestCaseEntry] {
testCase(Attribute_FunctorTests.__allTests),
testCase(Attribute_LiteralTests.__allTests),
testCase(ComputedPropertiesTests.__allTests),
testCase(CustomAttributesTests.__allTests),
testCase(DocumentTests.__allTests),
testCase(EntityCheckTests.__allTests),
testCase(EntityTests.__allTests),