mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-07-10 12:18:40 -07:00
Compare commits
19 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8defe82536 | |||
| 1f83216b03 | |||
| 6e6973c170 | |||
| 4f52cc2fd1 | |||
| 35d6cbb440 | |||
| d667e91a6a | |||
| 75c8ce7405 | |||
| 3a8237ec0f | |||
| 49e33baa96 | |||
| f2a9b5a7b9 | |||
| 24baadd7a8 | |||
| 8d18be6154 | |||
| 8aa887b527 | |||
| 7d0a686dd9 | |||
| e4eb7816d7 | |||
| 482174f5b4 | |||
| 339480264e | |||
| 5d666ec998 | |||
| 9cacbe9b8b |
@@ -11,7 +11,7 @@ Please enjoy these examples, but allow me the forced casting and the lack of err
|
||||
// MARK: - Create a request or response body with one Dog in it
|
||||
let dogFromCode = try! Dog(name: "Buddy", owner: nil)
|
||||
|
||||
typealias SingleDogDocument = JSONAPI.Document<SingleResourceBody<Dog>, NoMetadata, NoLinks, NoIncludes, UnknownJSONAPIError>
|
||||
typealias SingleDogDocument = JSONAPI.Document<SingleResourceBody<Dog>, NoMetadata, NoLinks, NoIncludes, NoAPIDescription, UnknownJSONAPIError>
|
||||
|
||||
let singleDogDocument = SingleDogDocument(body: SingleResourceBody(entity: dogFromCode))
|
||||
|
||||
@@ -27,7 +27,7 @@ let dogs = try! [Dog(name: "Buddy", owner: personIds[0]), Dog(name: "Joy", owner
|
||||
let houses = [House(), House()]
|
||||
let people = try! [Person(id: personIds[0], name: ["Gary", "Doe"], favoriteColor: "Orange-Red", friends: [], dogs: [dogs[0], dogs[1]], home: houses[0]), Person(id: personIds[1], name: ["Elise", "Joy"], favoriteColor: "Red", friends: [], dogs: [dogs[2]], home: houses[1])]
|
||||
|
||||
typealias BatchPeopleDocument = JSONAPI.Document<ManyResourceBody<Person>, NoMetadata, NoLinks, Include2<Dog, House>, UnknownJSONAPIError>
|
||||
typealias BatchPeopleDocument = JSONAPI.Document<ManyResourceBody<Person>, NoMetadata, NoLinks, Include2<Dog, House>, NoAPIDescription, UnknownJSONAPIError>
|
||||
|
||||
let includes = dogs.map { BatchPeopleDocument.Include($0) } + houses.map { BatchPeopleDocument.Include($0) }
|
||||
let batchPeopleDocument = BatchPeopleDocument(body: .init(entities: people), includes: .init(values: includes))
|
||||
|
||||
@@ -23,8 +23,10 @@ extension String: CreatableRawIdType {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Entity typealias for convenience
|
||||
public typealias ExampleEntity<Description: EntityDescription> = Entity<Description, String>
|
||||
// MARK: - typealiases for convenience
|
||||
public typealias ExampleEntity<Description: EntityDescription> = Entity<Description, NoMetadata, NoLinks, String>
|
||||
public typealias ToOne<E: OptionalRelatable> = ToOneRelationship<E, NoMetadata, NoLinks>
|
||||
public typealias ToMany<E: Relatable> = ToManyRelationship<E, NoMetadata, NoLinks>
|
||||
|
||||
// MARK: - A few resource objects (entities)
|
||||
public enum PersonDescription: EntityDescription {
|
||||
@@ -46,11 +48,11 @@ public enum PersonDescription: EntityDescription {
|
||||
}
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
public let friends: ToManyRelationship<Person>
|
||||
public let dogs: ToManyRelationship<Dog>
|
||||
public let home: ToOneRelationship<House>
|
||||
public let friends: ToMany<Person>
|
||||
public let dogs: ToMany<Dog>
|
||||
public let home: ToOne<House>
|
||||
|
||||
public init(friends: ToManyRelationship<Person>, dogs: ToManyRelationship<Dog>, home: ToOneRelationship<House>) {
|
||||
public init(friends: ToMany<Person>, dogs: ToMany<Dog>, home: ToOne<House>) {
|
||||
self.friends = friends
|
||||
self.dogs = dogs
|
||||
self.home = home
|
||||
@@ -60,7 +62,7 @@ public enum PersonDescription: EntityDescription {
|
||||
|
||||
public typealias Person = ExampleEntity<PersonDescription>
|
||||
|
||||
public extension Entity where Description == PersonDescription, EntityRawIdType == String {
|
||||
public extension Entity where Description == PersonDescription, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == String {
|
||||
public init(id: Person.Id? = nil,name: [String], favoriteColor: String, friends: [Person], dogs: [Dog], home: House) throws {
|
||||
self = try Person(id: id ?? Person.Id(), attributes: .init(name: .init(rawValue: name), favoriteColor: .init(rawValue: favoriteColor)), relationships: .init(friends: .init(entities: friends), dogs: .init(entities: dogs), home: .init(entity: home)))
|
||||
}
|
||||
@@ -79,9 +81,9 @@ public enum DogDescription: EntityDescription {
|
||||
}
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
public let owner: ToOneRelationship<Person?>
|
||||
public let owner: ToOne<Person?>
|
||||
|
||||
public init(owner: ToOneRelationship<Person?>) {
|
||||
public init(owner: ToOne<Person?>) {
|
||||
self.owner = owner
|
||||
}
|
||||
}
|
||||
@@ -89,7 +91,7 @@ public enum DogDescription: EntityDescription {
|
||||
|
||||
public typealias Dog = ExampleEntity<DogDescription>
|
||||
|
||||
public extension Entity where Description == DogDescription, EntityRawIdType == String {
|
||||
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)))
|
||||
}
|
||||
|
||||
@@ -29,17 +29,17 @@ To create an Xcode project for JSONAPI, run
|
||||
### Running the Playground
|
||||
To run the included Playground files, create an Xcode project using Swift Package Manager, then create an Xcode Workspace in the root of the repository and add both the generated Xcode project and the playground to the Workspace.
|
||||
|
||||
Note that Playground support for importing non-system Frameworks is still a bit touchy as of Swift 4.2. Sometimes building, cleaning and building, or commenting out and then uncommenting import statements (especially in the Entities.swift Playground Source file) can get things working for me when I am getting an error about JSONAPI not being found.
|
||||
Note that Playground support for importing non-system Frameworks is still a bit touchy as of Swift 4.2. Sometimes building, cleaning and building, or commenting out and then uncommenting import statements (especially in the Entities.swift Playground Source file) can get things working for me when I am getting an error about `JSONAPI` not being found.
|
||||
|
||||
## Project Status
|
||||
|
||||
### Decoding
|
||||
### Encoding/Decoding
|
||||
#### Document
|
||||
- [x] `data`
|
||||
- [x] `included`
|
||||
- [x] `errors`
|
||||
- [x] `meta`
|
||||
- [ ] `jsonapi`
|
||||
- [x] `jsonapi`
|
||||
- [x] `links`
|
||||
|
||||
#### Resource Object
|
||||
@@ -47,71 +47,40 @@ Note that Playground support for importing non-system Frameworks is still a bit
|
||||
- [x] `type`
|
||||
- [x] `attributes`
|
||||
- [x] `relationships`
|
||||
- [ ] `links`
|
||||
- [ ] `meta`
|
||||
|
||||
#### Relationship Object
|
||||
- [x] `data`
|
||||
- [ ] `links`
|
||||
- [ ] `meta`
|
||||
|
||||
#### Links Object
|
||||
- [x] `href`
|
||||
- [x] `meta`
|
||||
|
||||
### Encoding
|
||||
#### Document
|
||||
- [x] `data`
|
||||
- [x] `included`
|
||||
- [x] `errors`
|
||||
- [x] `meta`
|
||||
- [ ] `jsonapi`
|
||||
- [x] `links`
|
||||
|
||||
#### Resource Object
|
||||
- [x] `id`
|
||||
- [x] `type`
|
||||
- [x] `attributes`
|
||||
- [x] `relationships`
|
||||
- [ ] `links`
|
||||
- [ ] `meta`
|
||||
- [x] `meta`
|
||||
|
||||
#### Relationship Object
|
||||
- [x] `data`
|
||||
- [ ] `links`
|
||||
- [ ] `meta`
|
||||
- [x] `links`
|
||||
- [x] `meta`
|
||||
|
||||
#### Links Object
|
||||
- [x] `href`
|
||||
- [x] `meta`
|
||||
|
||||
### Entity Validator (using reflection)
|
||||
### Misc
|
||||
- [x] Support transforms on `Attributes` values (e.g. to support different representations of `Date`)
|
||||
- [x] Support validation on `Attributes`.
|
||||
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
|
||||
|
||||
### JSONAPITestLib
|
||||
#### Entity Validator
|
||||
- [x] Disallow optional array in `Attribute` (should be empty array, not `null`).
|
||||
- [x] Only allow `TransformedAttribute` and its derivatives within `Attributes` struct.
|
||||
- [x] Only allow `ToManyRelationship` and `ToOneRelationship` within `Relationships` struct.
|
||||
|
||||
### Strict Decoding/Encoding Settings
|
||||
- [ ] Error (potentially while still encoding/decoding successfully) if an included entity is not related to a primary entity (Turned off by default).
|
||||
|
||||
### Misc
|
||||
- [x] Support transforms on `Attributes` values (e.g. to support different representations of `Date`)
|
||||
- [x] Support ability to distinguish between `Attributes` fields that are optional (i.e. the key might not be there) and `Attributes` values that are optional (i.e. the key is guaranteed to be there but it might be `null`).
|
||||
- [x] Fix `ToOneRelationship` so that it is possible to specify an optional relationship where the value is `null` rather than the key being omitted.
|
||||
- [x] Conform to `CustomStringConvertible`
|
||||
- [x] More tests around failing to decode improperly structured JSON (not bad JSON, but JSON that is not to spec)
|
||||
- [ ] Use `KeyPath` to specify `Includes` thus creating type safety around the relationship between a primary resource type and the types of included resources????
|
||||
- [x] For `NoIncludes`, do not even loop over the "included" JSON API section if it exists.
|
||||
- [ ] Property-based testing (using `SwiftCheck`)
|
||||
- [x] Roll my own `Result` or find an alternative that doesn't use `Foundation`.
|
||||
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
|
||||
- [x] Make it easier to construct `Attributes` and `Relationships` values in test cases (literal expressibility).
|
||||
- [x] Make `TransformedAttribute` a Functor.
|
||||
### Potential Improvements
|
||||
- [ ] (Maybe) Use `KeyPath` to specify `Includes` thus creating type safety around the relationship between a primary resource type and the types of included resources.
|
||||
- [ ] (Maybe) Replace `SingleResourceBody` and `ManyResourceBody` with support at the `Document` level to just interpret `PrimaryResource`, `PrimaryResource?`, or `[PrimaryResource]` as the same decoding/encoding strategies.
|
||||
- [ ] Property-based testing (using `SwiftCheck`).
|
||||
- [ ] Error or warning if an included entity is not related to a primary entity or another included entity (Turned off or at least not throwing by default).
|
||||
|
||||
## Usage
|
||||
|
||||
In this documentation, in order to draw attention to the difference between the `JSONAPI` framework (this Swift library) and the **JSON API Spec** (the specification this library helps you follow), the specification will consistently be referred to below as simply the **SPEC**.
|
||||
|
||||
### `EntityDescription`
|
||||
### `JSONAPI.EntityDescription`
|
||||
|
||||
An `EntityDescription` is the `JSONAPI` framework's representation of what the **SPEC** calls a *Resource Object*. You might create the following `EntityDescription` to represent a person in a network of friends:
|
||||
|
||||
@@ -167,45 +136,61 @@ This readme doesn't go into detail on the **SPEC**, but the following *Resource
|
||||
}
|
||||
```
|
||||
|
||||
### `Entity`
|
||||
### `JSONAPI.Entity`
|
||||
|
||||
Once you have an `EntityDescription`, you _create_, _encode_, and _decode_ `Entities` that "fit the description". If you have a `CreatableRawIdType` (see the section on `RawIdType`s below) then you can create new `Entities` that will automatically be given unique Ids, but even without a `CreatableRawIdType` you can encode, decode and work with entities.
|
||||
|
||||
The `Entity` and `EntityDescription` together embody the rules and properties of a JSON API *Resource Object*.
|
||||
The `Entity` and `EntityDescription` together with a `JSONAPI.Meta` type and a `JSONAPI.Links` type embody the rules and properties of a JSON API *Resource Object*.
|
||||
|
||||
An `Entity` needs to be specialized on four generic types. The first is the `EntityDescription` described above. The others are a `Meta`, `Links`, and `MaybeRawId`.
|
||||
|
||||
#### `Meta`
|
||||
|
||||
The second generic specialization on `Entity` is `Meta`. This is described in its own section [below](#jsonapimeta). All `Meta` at any level of a JSON API Document follow the same rules.
|
||||
|
||||
#### `Links`
|
||||
|
||||
The third generic specialization on `Entity` is `Links`. This is described in its own section [below](#jsonnapilinks). All `Links` at any level of a JSON API Document follow the same rules, although the **SPEC** makes different suggestions as to what types of links might live on which parts of the Document.
|
||||
|
||||
#### `IdType`
|
||||
|
||||
An `Entity` needs to be specialized on two generic types. The first is the `EntityDescription` described above. The second is the raw type of `Id` to use for the `Entity`. The actual `Id` of the `Entity` will not be a `RawIdType`, though. The `Id` will package a value of `RawIdType` with a specialized reference back to the `Entity` type it identifies. This just looks like `Id<RawIdType, Entity<EntityDescription, RawIdType>>`.
|
||||
The second is the raw type of `Id` to use for the `Entity`. The actual `Id` of the `Entity` will not be a `RawIdType`, though. The `Id` will package a value of `RawIdType` with a specialized reference back to the `Entity` type it identifies. This just looks like `Id<RawIdType, Entity<EntityDescription, RawIdType>>`.
|
||||
|
||||
Having the `Entity` type associated with the `Id` makes it easy to store all of your entities in a hash broken out by `Entity` type; You can pass `Ids` around and always know where to look for the `Entity` to which the `Id` refers.
|
||||
|
||||
A `RawIdType` is the underlying type that uniquely identifies an `Entity`. This is often a `String` or a `UUID`.
|
||||
|
||||
#### `MaybeRawId`
|
||||
|
||||
`MaybeRawId` is either a `RawIdType` that can be used to uniquely identify `Entities` or it is `Unidentified` which is used to indicate an `Entity` does not have an `Id` (which is useful when a client is requesting that the server create an `Entity` and assign it a new `Id`).
|
||||
|
||||
#### Convenient `typealiases`
|
||||
|
||||
Often you can use one `RawIdType` for many if not all of your `Entities`. That means you can save yourself some boilerplate by using `typealias`es like the following:
|
||||
```
|
||||
public typealias Entity<Description: JSONAPI.EntityDescription> = JSONAPI.Entity<Description, String>
|
||||
public typealias Entity<Description: JSONAPI.EntityDescription, Meta: JSONAPI.Meta, Links: JSONAPI.Links> = JSONAPI.Entity<Description, Meta, Links, String>
|
||||
|
||||
public typealias NewEntity<Description: JSONAPI.EntityDescription> = JSONAPI.Entity<Description, Unidentified>
|
||||
public typealias NewEntity<Description: JSONAPI.EntityDescription, Meta: JSONAPI.Meta, Links: JSONAPI.Links> = JSONAPI.Entity<Description, Meta, Links, Unidentified>
|
||||
```
|
||||
|
||||
It can also be nice to create a `typealias` for each type of entity you want to work with:
|
||||
```
|
||||
typealias Person = Entity<PersonDescription>
|
||||
typealias Person = Entity<PersonDescription, NoMetadata, NoLinks>
|
||||
|
||||
typealias NewPerson = NewEntity<PersonDescription>
|
||||
typealias NewPerson = NewEntity<PersonDescription, NoMetadata, NoLinks>
|
||||
```
|
||||
|
||||
Note that I am assuming an unidentified person is a "new" person. I suspect that is generally an acceptable conflation because the only time the **SPEC** allows a *Resource Object* to be encoded without an `Id` is when a client is requesting the given *Resource Object* be created by the server and the client wants the server to create the `Id` for that object.
|
||||
|
||||
### `Relationships`
|
||||
### `JSONAPI.Relationships`
|
||||
|
||||
There are two types of `Relationships`: `ToOneRelationship` and `ToManyRelationship`. An `EntityDescription`'s `Relationships` type can contain any number of `Relationship` properties of either of these types. Do not store anything other than `Relationship` properties in the `Relationships` struct of an `EntityDescription`.
|
||||
|
||||
To describe a relationship that may be omitted (i.e. the key is not even present in the JSON object), you make the entire `ToOneRelationship` or `ToManyRelationship` optional. However, this is not recommended because you can also represent optional relationships as nullable which means the key is always present. A `ToManyRelationship` can naturally represent the absence of related values with an empty array, so `ToManyRelationship` does not support nullability at all. A `ToOneRelationship` can be marked as nullable (i.e. the value might be `null` or it might be a resource identifier) like this:
|
||||
In addition to identifying entities by Id and type, `Relationships` can contain `Meta` or `Links` that follow the same rules as [`Meta`](#jsonapimeta) and [`Links`](#jsonapilinks) elsewhere in the JSON API Document.
|
||||
|
||||
To describe a relationship that may be omitted (i.e. the key is not even present in the JSON object), you make the entire `ToOneRelationship` or `ToManyRelationship` optional. However, this is not recommended because you can also represent optional relationships as nullable which means the key is always present. A `ToManyRelationship` can naturally represent the absence of related values with an empty array, so `ToManyRelationship` does not support nullability at all. A `ToOneRelationship` can be marked as nullable (i.e. the value could be either `null` or a resource identifier) like this:
|
||||
```
|
||||
let nullableRelative: ToOneRelationship<Person?>
|
||||
let nullableRelative: ToOneRelationship<Person?, NoMetadata, NoLinks>
|
||||
```
|
||||
|
||||
An entity that does not have relationships can be described by adding the following to an `EntityDescription`:
|
||||
@@ -218,7 +203,7 @@ typealias Relationships = NoRelationships
|
||||
let friendIds: [Person.Identifier] = person ~> \.friends
|
||||
```
|
||||
|
||||
### `Attributes`
|
||||
### `JSONAPI.Attributes`
|
||||
|
||||
The `Attributes` of an `EntityDescription` can contain any JSON encodable/decodable types as long as they are wrapped in an `Attribute`, `ValidatedAttribute`, or `TransformedAttribute` `struct`.
|
||||
|
||||
@@ -288,13 +273,13 @@ public var fullName: Attribute<String> {
|
||||
}
|
||||
```
|
||||
|
||||
### `JSONAPIDocument`
|
||||
### `JSONAPI.Document`
|
||||
|
||||
The entirety of a JSON API request or response is encoded or decoded from- or to a `JSONAPIDocument`. As an example, a JSON API response containing one `Person` and no included entities could be decoded as follows:
|
||||
The entirety of a JSON API request or response is encoded or decoded from- or to a `Document`. As an example, a JSON API response containing one `Person` and no included entities could be decoded as follows:
|
||||
```
|
||||
let decoder = JSONDecoder()
|
||||
|
||||
let responseStructure = JSONAPIDocument<SingleResourceBody<Person>, NoMetadata, NoLinks, NoIncludes, UnknownJSONAPIError>.self
|
||||
let responseStructure = JSONAPI.Document<SingleResourceBody<Person>, NoMetadata, NoLinks, NoIncludes, UnknownJSONAPIError>.self
|
||||
|
||||
let document = try decoder.decode(responseStructure, from: data)
|
||||
```
|
||||
@@ -315,7 +300,7 @@ You cannot, however, use an optional `PrimaryResource` with a `ManyResourceBody`
|
||||
|
||||
#### `MetaType`
|
||||
|
||||
The second generic type of a `JSONAPIDocument` is a `Meta`. This structure is entirely open-ended. As an example, the JSON API document could, as an example, contain the following pagination info in its meta entry:
|
||||
The second generic type of a `JSONAPIDocument` is a `Meta`. This `Meta` follows the same rules as `Meta` at any other part of a JSON API Document. It is described below in its own section, but as an example, the JSON API document could contain the following pagination info in its meta entry:
|
||||
```
|
||||
{
|
||||
"meta": {
|
||||
@@ -339,9 +324,7 @@ You can always use `NoMetadata` if this JSON API feature is not needed.
|
||||
|
||||
#### `LinksType`
|
||||
|
||||
The third generic type of a `JSONAPIDocument` is a `Links` struct. A `Links` struct must contain only `Link` properties. Each `Link` property can either be a `URL` or a `URL` and some `Meta`.
|
||||
|
||||
You can specify `NoLinks` if the document should not contain any links.
|
||||
The third generic type of a `JSONAPIDocument` is a `Links` struct. `Links` are described in their own section [below](#jsonapilinks).
|
||||
|
||||
#### `IncludeType`
|
||||
|
||||
@@ -351,11 +334,33 @@ The fourth generic type of a `JSONAPIDocument` is an `Include`. This type contro
|
||||
|
||||
To specify that we expect friends of a person to be included in the above example `JSONAPIDocument`, we would use `Include1<Person>` instead of `NoIncludes`.
|
||||
|
||||
#### `APIDescriptionType`
|
||||
|
||||
The fifth generic type of a `JSONAPIDocument` is an `APIDescription`. The type represents the "JSON:API Object" described by the **SPEC**. This type describes the highest version of the **SPEC** supported and can carry additional metadata to describe the API.
|
||||
|
||||
You can specify this is not part of the document by using the `NoAPIDescription` type.
|
||||
|
||||
You can describe the API by a version with no metadata by using `APIDescription<NoMetadata>`.
|
||||
|
||||
You can supply any `JSONAPI.Meta` type as the metadata type of the API description.
|
||||
|
||||
#### `Error`
|
||||
|
||||
The final generic type of a `JSONAPIDocument` is the `Error`. You should create an error type that can decode all the errors you expect your `JSONAPIDocument` to be able to decode. As prescribed by the **SPEC**, these errors will be found in the root document member `errors`.
|
||||
|
||||
### `RawIdType`
|
||||
### `JSONAPI.Meta`
|
||||
|
||||
A `Meta` struct is totally open-ended. It is described by the **SPEC** as a place to put any information that does not fit into the standard JSON API Document structure anywhere else.
|
||||
|
||||
You can specify `NoMetadata` if the part of the document being described should not contain any `Meta`.
|
||||
|
||||
### `JSONAPI.Links`
|
||||
|
||||
A `Links` struct must contain only `Link` properties. Each `Link` property can either be a `URL` or a `URL` and some `Meta`. Each part of the document has some suggested common `Links` to include but generally any link can be included.
|
||||
|
||||
You can specify `NoLinks` if the part of the document being described should not contain any `Links`.
|
||||
|
||||
### `JSONAPI.RawIdType`
|
||||
|
||||
If you want to create new `JSONAPI.Entity` values and assign them Ids then you will need to conform at least one type to `CreatableRawIdType`. Doing so is easy; here are two example conformances for `UUID` and `String` (via `UUID`):
|
||||
```
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//
|
||||
// APIDescription.swift
|
||||
// JSONAPI
|
||||
//
|
||||
// Created by Mathew Polzin on 12/1/18.
|
||||
//
|
||||
|
||||
/// This is what the JSON API Spec calls the "JSON:API Object"
|
||||
public protocol APIDescriptionType: Codable, Equatable {
|
||||
associatedtype Meta
|
||||
}
|
||||
|
||||
/// This is what the JSON API Spec calls the "JSON:API Object"
|
||||
public struct APIDescription<Meta: JSONAPI.Meta>: APIDescriptionType {
|
||||
public let version: String
|
||||
public let meta: Meta
|
||||
}
|
||||
|
||||
public struct NoAPIDescription: APIDescriptionType, CustomStringConvertible {
|
||||
public typealias Meta = NoMetadata
|
||||
|
||||
public init() {}
|
||||
|
||||
public static var none: NoAPIDescription { return .init() }
|
||||
|
||||
public var description: String { return "No JSON:API Object" }
|
||||
}
|
||||
|
||||
extension APIDescription {
|
||||
private enum CodingKeys: String, CodingKey {
|
||||
case version
|
||||
case meta
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
// The spec says that if a version is not specified, it should be assumed to be at least 1.0
|
||||
version = (try? container.decode(String.self, forKey: .version)) ?? "1.0"
|
||||
|
||||
if let metaVal = NoMetadata() as? Meta {
|
||||
meta = metaVal
|
||||
} else {
|
||||
meta = try container.decode(Meta.self, forKey: .meta)
|
||||
}
|
||||
}
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: CodingKeys.self)
|
||||
|
||||
try container.encode(version, forKey: .version)
|
||||
|
||||
if Meta.self != NoMetadata.self {
|
||||
try container.encode(meta, forKey: .meta)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,9 +10,10 @@ public protocol JSONAPIDocument: Codable, Equatable {
|
||||
associatedtype MetaType: JSONAPI.Meta
|
||||
associatedtype LinksType: JSONAPI.Links
|
||||
associatedtype IncludeType: JSONAPI.Include
|
||||
associatedtype APIDescription: APIDescriptionType
|
||||
associatedtype Error: JSONAPIError
|
||||
|
||||
typealias Body = Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, Error>.Body
|
||||
typealias Body = Document<PrimaryResourceBody, MetaType, LinksType, IncludeType, APIDescription, Error>.Body
|
||||
|
||||
var body: Body { get }
|
||||
}
|
||||
@@ -24,11 +25,19 @@ public protocol JSONAPIDocument: Codable, Equatable {
|
||||
/// API uses snake case, you will want to use
|
||||
/// a conversion such as the one offerred by the
|
||||
/// Foundation JSONEncoder/Decoder: `KeyDecodingStrategy`
|
||||
public struct Document<PrimaryResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, Error: JSONAPIError>: JSONAPIDocument {
|
||||
public struct Document<PrimaryResourceBody: JSONAPI.ResourceBody, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, IncludeType: JSONAPI.Include, APIDescription: APIDescriptionType, Error: JSONAPIError>: JSONAPIDocument {
|
||||
public typealias Include = IncludeType
|
||||
|
||||
/// The JSON API Spec calls this the JSON:API Object. It contains version
|
||||
/// and metadata information about the API itself.
|
||||
public let apiDescription: APIDescription
|
||||
|
||||
/// The Body of the Document. This body is either one or more errors
|
||||
/// with links and metadata attempted to parse but not guaranteed or
|
||||
/// it is a successful data struct containing all the primary and
|
||||
/// included resources, the metadata, and the links that this
|
||||
/// document type specifies.
|
||||
public let body: Body
|
||||
// public let jsonApi: APIDescription?
|
||||
|
||||
public enum Body: Equatable {
|
||||
case errors([Error], meta: MetaType?, links: LinksType?)
|
||||
@@ -91,54 +100,86 @@ public struct Document<PrimaryResourceBody: JSONAPI.ResourceBody, MetaType: JSON
|
||||
}
|
||||
}
|
||||
|
||||
public init(errors: [Error], meta: MetaType? = nil, links: LinksType? = nil) {
|
||||
public init(apiDescription: APIDescription, errors: [Error], meta: MetaType? = nil, links: LinksType? = nil) {
|
||||
body = .errors(errors, meta: meta, links: links)
|
||||
self.apiDescription = apiDescription
|
||||
}
|
||||
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
|
||||
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: links))
|
||||
self.apiDescription = apiDescription
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes {
|
||||
public init(body: PrimaryResourceBody, meta: MetaType, links: LinksType) {
|
||||
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: links))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, meta: MetaType, links: LinksType) {
|
||||
self.init(apiDescription: apiDescription, body: body, includes: .none, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where MetaType == NoMetadata {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>, links: LinksType) {
|
||||
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: links))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, includes: Includes<Include>, links: LinksType) {
|
||||
self.init(apiDescription: apiDescription, body: body, includes: includes, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where LinksType == NoLinks {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType) {
|
||||
self.body = .data(.init(primary: body, includes: includes, meta: meta, links: .none))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType) {
|
||||
self.init(apiDescription: apiDescription, body: body, includes: includes, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where APIDescription == NoAPIDescription {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>, meta: MetaType, links: LinksType) {
|
||||
self.init(apiDescription: .none, body: body, includes: includes, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes, LinksType == NoLinks {
|
||||
public init(body: PrimaryResourceBody, meta: MetaType) {
|
||||
self.body = .data(.init(primary: body, includes: .none, meta: meta, links: .none))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, meta: MetaType) {
|
||||
self.init(apiDescription: apiDescription, body: body, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata {
|
||||
public init(body: PrimaryResourceBody, links: LinksType) {
|
||||
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: links))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, links: LinksType) {
|
||||
self.init(apiDescription: apiDescription, body: body, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes, APIDescription == NoAPIDescription {
|
||||
public init(body: PrimaryResourceBody, meta: MetaType, links: LinksType) {
|
||||
self.init(apiDescription: .none, body: body, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>) {
|
||||
self.body = .data(.init(primary: body, includes: includes, meta: .none, links: .none))
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody, includes: Includes<Include>) {
|
||||
self.init(apiDescription: apiDescription, body: body, includes: includes, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where MetaType == NoMetadata, APIDescription == NoAPIDescription {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>, links: LinksType) {
|
||||
self.init(apiDescription: .none, body: body, includes: includes, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(apiDescription: APIDescription, body: PrimaryResourceBody) {
|
||||
self.init(apiDescription: apiDescription, body: body, includes: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where MetaType == NoMetadata, LinksType == NoLinks, APIDescription == NoAPIDescription {
|
||||
public init(body: PrimaryResourceBody, includes: Includes<Include>) {
|
||||
self.init(apiDescription: .none, body: body, includes: includes)
|
||||
}
|
||||
}
|
||||
|
||||
extension Document where IncludeType == NoIncludes, MetaType == NoMetadata, LinksType == NoLinks, APIDescription == NoAPIDescription {
|
||||
public init(body: PrimaryResourceBody) {
|
||||
self.body = .data(.init(primary: body, includes: .none, meta: .none, links: .none))
|
||||
self.init(apiDescription: .none, body: body)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -155,6 +196,12 @@ extension Document {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: RootCodingKeys.self)
|
||||
|
||||
if let noData = NoAPIDescription() as? APIDescription {
|
||||
apiDescription = noData
|
||||
} else {
|
||||
apiDescription = try container.decode(APIDescription.self, forKey: .jsonapi)
|
||||
}
|
||||
|
||||
let errors = try container.decodeIfPresent([Error].self, forKey: .errors)
|
||||
|
||||
let meta: MetaType?
|
||||
@@ -242,6 +289,10 @@ extension Document {
|
||||
try container.encode(data.links, forKey: .links)
|
||||
}
|
||||
}
|
||||
|
||||
if APIDescription.self != NoAPIDescription.self {
|
||||
try container.encode(apiDescription, forKey: .jsonapi)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,17 @@ public protocol JSONAPIURL: Codable, Equatable {}
|
||||
public struct Link<URL: JSONAPI.JSONAPIURL, Meta: JSONAPI.Meta>: Equatable, Codable {
|
||||
public let url: URL
|
||||
public let meta: Meta
|
||||
|
||||
public init(url: URL, meta: Meta) {
|
||||
self.url = url
|
||||
self.meta = meta
|
||||
}
|
||||
}
|
||||
|
||||
extension Link where Meta == NoMetadata {
|
||||
public init(url: URL) {
|
||||
self.init(url: url, meta: .none)
|
||||
}
|
||||
}
|
||||
|
||||
public extension Link {
|
||||
|
||||
@@ -80,7 +80,7 @@ public protocol IdentifiableEntityType: EntityType, Relatable where EntityRawIdT
|
||||
/// encoded to or decoded from a JSON API
|
||||
/// "Resource Object."
|
||||
/// See https://jsonapi.org/format/#document-resource-objects
|
||||
public struct Entity<Description: JSONAPI.EntityDescription, EntityRawIdType: JSONAPI.MaybeRawId>: EntityType {
|
||||
public struct Entity<Description: JSONAPI.EntityDescription, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links, EntityRawIdType: JSONAPI.MaybeRawId>: EntityType {
|
||||
/// The `Entity`'s Id. This can be of type `Unidentified` if
|
||||
/// the entity is being created clientside and the
|
||||
/// server is being asked to create a unique Id. Otherwise,
|
||||
@@ -92,11 +92,19 @@ public struct Entity<Description: JSONAPI.EntityDescription, EntityRawIdType: JS
|
||||
|
||||
/// The JSON API compliant relationships of this `Entity`.
|
||||
public let relationships: Description.Relationships
|
||||
|
||||
/// Any additional metadata packaged with the entity.
|
||||
public let meta: MetaType
|
||||
|
||||
/// Links related to the entity.
|
||||
public let links: LinksType
|
||||
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships) {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.id = id
|
||||
self.attributes = attributes
|
||||
self.relationships = relationships
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,60 +121,232 @@ extension Entity: CustomStringConvertible {
|
||||
|
||||
// MARK: Convenience initializers
|
||||
extension Entity where EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships) {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.id = Entity.Id()
|
||||
self.attributes = attributes
|
||||
self.relationships = relationships
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where EntityRawIdType == Unidentified {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships) {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.id = .unidentified
|
||||
self.attributes = attributes
|
||||
self.relationships = relationships
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes {
|
||||
public init(id: Entity.Id, relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: relationships, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, MetaType == NoMetadata {
|
||||
public init(id: Entity.Id, relationships: Description.Relationships, links: LinksType) {
|
||||
self.init(id: id, relationships: relationships, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, relationships: Description.Relationships, meta: MetaType) {
|
||||
self.init(id: id, relationships: relationships, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, relationships: Description.Relationships) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: relationships)
|
||||
self.init(id: id, relationships: relationships, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, EntityRawIdType: CreatableRawIdType {
|
||||
public init(relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.init(attributes: NoAttributes(), relationships: relationships, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, MetaType == NoMetadata, EntityRawIdType: CreatableRawIdType {
|
||||
public init(relationships: Description.Relationships, links: LinksType) {
|
||||
self.init(attributes: NoAttributes(), relationships: relationships, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(relationships: Description.Relationships, meta: MetaType) {
|
||||
self.init(attributes: NoAttributes(), relationships: relationships, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(relationships: Description.Relationships) {
|
||||
self.init(attributes: NoAttributes(), relationships: relationships)
|
||||
self.init(attributes: NoAttributes(), relationships: relationships, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, EntityRawIdType == Unidentified {
|
||||
public init(relationships: Description.Relationships, meta: MetaType, links: LinksType) {
|
||||
self.init(attributes: NoAttributes(), relationships: relationships, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, meta: MetaType, links: LinksType) {
|
||||
self.init(id: id, attributes: attributes, relationships: NoRelationships(), meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, links: LinksType) {
|
||||
self.init(id: id, attributes: attributes, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, meta: MetaType) {
|
||||
self.init(id: id, attributes: attributes, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes) {
|
||||
self.init(id: id, attributes: attributes, relationships: NoRelationships())
|
||||
self.init(id: id, attributes: attributes, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, meta: MetaType, links: LinksType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, links: LinksType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, meta: MetaType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships())
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, EntityRawIdType == Unidentified {
|
||||
public init(attributes: Description.Attributes, meta: MetaType, links: LinksType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata, EntityRawIdType == Unidentified {
|
||||
public init(attributes: Description.Attributes, links: LinksType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, LinksType == NoLinks, EntityRawIdType == Unidentified {
|
||||
public init(attributes: Description.Attributes, meta: MetaType) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Relationships == NoRelationships, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType == Unidentified {
|
||||
public init(attributes: Description.Attributes) {
|
||||
self.init(attributes: attributes, relationships: NoRelationships())
|
||||
self.init(attributes: attributes, relationships: NoRelationships(), meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships {
|
||||
public init(id: Entity.Id, meta: MetaType, links: LinksType) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships(), meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, MetaType == NoMetadata {
|
||||
public init(id: Entity.Id, links: LinksType) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships(), meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, meta: MetaType) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships(), meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(id: Entity.Id) {
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships())
|
||||
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships(), meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, EntityRawIdType: CreatableRawIdType {
|
||||
public init(meta: MetaType, links: LinksType) {
|
||||
self.init(attributes: NoAttributes(), relationships: NoRelationships(), meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, MetaType == NoMetadata, EntityRawIdType: CreatableRawIdType {
|
||||
public init(links: LinksType) {
|
||||
self.init(attributes: NoAttributes(), relationships: NoRelationships(), meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(meta: MetaType) {
|
||||
self.init(attributes: NoAttributes(), relationships: NoRelationships(), meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships, MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init() {
|
||||
self.init(attributes: NoAttributes(), relationships: NoRelationships())
|
||||
self.init(attributes: NoAttributes(), relationships: NoRelationships(), meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where MetaType == NoMetadata {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships, links: LinksType) {
|
||||
self.init(id: id, attributes: attributes, relationships: relationships, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where MetaType == NoMetadata, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships, links: LinksType) {
|
||||
self.init(attributes: attributes, relationships: relationships, meta: .none, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where LinksType == NoLinks {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType) {
|
||||
self.init(id: id, attributes: attributes, relationships: relationships, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships, meta: MetaType) {
|
||||
self.init(attributes: attributes, relationships: relationships, meta: meta, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(id: Entity.Id, attributes: Description.Attributes, relationships: Description.Relationships) {
|
||||
self.init(id: id, attributes: attributes, relationships: relationships, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where MetaType == NoMetadata, LinksType == NoLinks, EntityRawIdType: CreatableRawIdType {
|
||||
public init(attributes: Description.Attributes, relationships: Description.Relationships) {
|
||||
self.init(attributes: attributes, relationships: relationships, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,8 +354,12 @@ extension Entity where Description.Attributes == NoAttributes, Description.Relat
|
||||
public extension Entity where EntityRawIdType: JSONAPI.RawIdType {
|
||||
/// Get a pointer to this entity that can be used as a
|
||||
/// relationship to another entity.
|
||||
public var pointer: ToOneRelationship<Entity> {
|
||||
return ToOneRelationship(entity: self)
|
||||
public var pointer: ToOneRelationship<Entity, NoMetadata, NoLinks> {
|
||||
return ToOneRelationship(entity: self, meta: .none, links: .none)
|
||||
}
|
||||
|
||||
public func pointer<MType: JSONAPI.Meta, LType: JSONAPI.Links>(withMeta meta: MType, links: LType) -> ToOneRelationship<Entity, MType, LType> {
|
||||
return ToOneRelationship(entity: self, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,14 +392,14 @@ public extension EntityProxy {
|
||||
/// Access to an Id of a `ToOneRelationship`.
|
||||
/// This allows you to write `entity ~> \.other` instead
|
||||
/// of `entity.relationships.other.id`.
|
||||
public static func ~><OtherEntity: OptionalRelatable>(entity: Self, path: KeyPath<Description.Relationships, ToOneRelationship<OtherEntity>>) -> OtherEntity.WrappedIdentifier {
|
||||
public static func ~><OtherEntity: OptionalRelatable, MType: JSONAPI.Meta, LType: JSONAPI.Links>(entity: Self, path: KeyPath<Description.Relationships, ToOneRelationship<OtherEntity, MType, LType>>) -> OtherEntity.WrappedIdentifier {
|
||||
return entity.relationships[keyPath: path].id
|
||||
}
|
||||
|
||||
/// Access to all Ids of a `ToManyRelationship`.
|
||||
/// This allows you to write `entity ~> \.others` instead
|
||||
/// of `entity.relationships.others.ids`.
|
||||
public static func ~><OtherEntity: Relatable>(entity: Self, path: KeyPath<Description.Relationships, ToManyRelationship<OtherEntity>>) -> [OtherEntity.Identifier] {
|
||||
public static func ~><OtherEntity: Relatable, MType: JSONAPI.Meta, LType: JSONAPI.Links>(entity: Self, path: KeyPath<Description.Relationships, ToManyRelationship<OtherEntity, MType, LType>>) -> [OtherEntity.Identifier] {
|
||||
return entity.relationships[keyPath: path].ids
|
||||
}
|
||||
}
|
||||
@@ -228,6 +412,8 @@ private enum ResourceObjectCodingKeys: String, CodingKey {
|
||||
case id = "id"
|
||||
case attributes = "attributes"
|
||||
case relationships = "relationships"
|
||||
case meta = "meta"
|
||||
case links = "links"
|
||||
}
|
||||
|
||||
public extension Entity {
|
||||
@@ -247,6 +433,14 @@ public extension Entity {
|
||||
if Description.Relationships.self != NoRelationships.self {
|
||||
try container.encode(relationships, forKey: .relationships)
|
||||
}
|
||||
|
||||
if MetaType.self != NoMetadata.self {
|
||||
try container.encode(meta, forKey: .meta)
|
||||
}
|
||||
|
||||
if LinksType.self != NoLinks.self {
|
||||
try container.encode(links, forKey: .links)
|
||||
}
|
||||
}
|
||||
|
||||
public init(from decoder: Decoder) throws {
|
||||
@@ -265,5 +459,9 @@ public extension Entity {
|
||||
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)
|
||||
|
||||
links = try (NoLinks() as? LinksType) ?? container.decode(LinksType.self, forKey: .links)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,30 +5,59 @@
|
||||
// Created by Mathew Polzin on 8/31/18.
|
||||
//
|
||||
|
||||
public protocol RelationshipType: Codable {}
|
||||
public protocol RelationshipType: Codable {
|
||||
associatedtype LinksType
|
||||
associatedtype MetaType
|
||||
|
||||
var links: LinksType { get }
|
||||
var meta: MetaType { get }
|
||||
}
|
||||
|
||||
/// An Entity relationship that can be encoded to or decoded from
|
||||
/// a JSON API "Resource Linkage."
|
||||
/// See https://jsonapi.org/format/#document-resource-object-linkage
|
||||
/// A convenient typealias might make your code much more legible: `One<EntityDescription>`
|
||||
public struct ToOneRelationship<Relatable: JSONAPI.OptionalRelatable>: RelationshipType, Equatable {
|
||||
public struct ToOneRelationship<Relatable: JSONAPI.OptionalRelatable, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links>: RelationshipType, Equatable {
|
||||
|
||||
public let id: Relatable.WrappedIdentifier
|
||||
|
||||
public init(id: Relatable.WrappedIdentifier) {
|
||||
public let meta: MetaType
|
||||
public let links: LinksType
|
||||
|
||||
public init(id: Relatable.WrappedIdentifier, meta: MetaType, links: LinksType) {
|
||||
self.id = id
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship where MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(id: Relatable.WrappedIdentifier) {
|
||||
self.init(id: id, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier {
|
||||
public init<E: EntityType>(entity: E, meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
self.init(id: entity.id, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init<E: EntityType>(entity: E) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
id = entity.id
|
||||
self.init(id: entity.id, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier? {
|
||||
public init<E: EntityType>(entity: E?, meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
self.init(id: entity?.id, meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Identifier?, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init<E: EntityType>(entity: E?) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
id = entity?.id
|
||||
self.init(id: entity?.id, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -36,30 +65,54 @@ extension ToOneRelationship where Relatable.WrappedIdentifier == Relatable.Ident
|
||||
/// a JSON API "Resource Linkage."
|
||||
/// See https://jsonapi.org/format/#document-resource-object-linkage
|
||||
/// A convenient typealias might make your code much more legible: `Many<EntityDescription>`
|
||||
public struct ToManyRelationship<Relatable: JSONAPI.Relatable>: RelationshipType, Equatable {
|
||||
public struct ToManyRelationship<Relatable: JSONAPI.Relatable, MetaType: JSONAPI.Meta, LinksType: JSONAPI.Links>: RelationshipType, Equatable {
|
||||
|
||||
public let ids: [Relatable.Identifier]
|
||||
|
||||
public init(ids: [Relatable.Identifier]) {
|
||||
public let meta: MetaType
|
||||
public let links: LinksType
|
||||
|
||||
public init(ids: [Relatable.Identifier], meta: MetaType, links: LinksType) {
|
||||
self.ids = ids
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
|
||||
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T>]) where T.WrappedIdentifier == Relatable.Identifier {
|
||||
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T, NoMetadata, NoLinks>], meta: MetaType, links: LinksType) where T.WrappedIdentifier == Relatable.Identifier {
|
||||
ids = relationships.map { $0.id }
|
||||
self.meta = meta
|
||||
self.links = links
|
||||
}
|
||||
|
||||
private init() {
|
||||
ids = []
|
||||
public init<E: EntityType>(entities: [E], meta: MetaType, links: LinksType) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
self.init(ids: entities.map { $0.id }, meta: meta, links: links)
|
||||
}
|
||||
|
||||
private init(meta: MetaType, links: LinksType) {
|
||||
self.init(ids: [], meta: meta, links: links)
|
||||
}
|
||||
|
||||
public static var none: ToManyRelationship {
|
||||
return .init()
|
||||
public static func none(withMeta meta: MetaType, links: LinksType) -> ToManyRelationship {
|
||||
return ToManyRelationship(meta: meta, links: links)
|
||||
}
|
||||
}
|
||||
|
||||
extension ToManyRelationship {
|
||||
extension ToManyRelationship where MetaType == NoMetadata, LinksType == NoLinks {
|
||||
|
||||
public init(ids: [Relatable.Identifier]) {
|
||||
self.init(ids: ids, meta: .none, links: .none)
|
||||
}
|
||||
|
||||
public init<T: JSONAPI.Relatable>(relationships: [ToOneRelationship<T, NoMetadata, NoLinks>]) where T.WrappedIdentifier == Relatable.Identifier {
|
||||
self.init(relationships: relationships, meta: .none, links: .none)
|
||||
}
|
||||
|
||||
public static var none: ToManyRelationship {
|
||||
return .none(withMeta: .none, links: .none)
|
||||
}
|
||||
|
||||
public init<E: EntityType>(entities: [E]) where E.Description == Relatable.Description, E.Id == Relatable.Identifier {
|
||||
ids = entities.map { $0.id }
|
||||
self.init(entities: entities, meta: .none, links: .none)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,6 +138,8 @@ extension Optional: OptionalRelatable where Wrapped: Relatable {
|
||||
// MARK: Codable
|
||||
private enum ResourceLinkageCodingKeys: String, CodingKey {
|
||||
case data = "data"
|
||||
case meta = "meta"
|
||||
case links = "links"
|
||||
}
|
||||
private enum ResourceIdentifierCodingKeys: String, CodingKey {
|
||||
case id = "id"
|
||||
@@ -103,6 +158,18 @@ extension ToOneRelationship {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: ResourceLinkageCodingKeys.self)
|
||||
|
||||
if let noMeta = NoMetadata() as? MetaType {
|
||||
meta = noMeta
|
||||
} else {
|
||||
meta = try container.decode(MetaType.self, forKey: .meta)
|
||||
}
|
||||
|
||||
if let noLinks = NoLinks() as? LinksType {
|
||||
links = noLinks
|
||||
} else {
|
||||
links = try container.decode(LinksType.self, forKey: .links)
|
||||
}
|
||||
|
||||
// A little trickery follows. If the id is nil, the
|
||||
// container.decode(Identifier.self) will fail even if Identifier
|
||||
// is Optional. However, we can check if decoding nil
|
||||
@@ -133,6 +200,14 @@ extension ToOneRelationship {
|
||||
try container.encodeNil(forKey: .data)
|
||||
}
|
||||
|
||||
if MetaType.self != NoMetadata.self {
|
||||
try container.encode(meta, forKey: .meta)
|
||||
}
|
||||
|
||||
if LinksType.self != NoLinks.self {
|
||||
try container.encode(links, forKey: .links)
|
||||
}
|
||||
|
||||
var identifier = container.nestedContainer(keyedBy: ResourceIdentifierCodingKeys.self, forKey: .data)
|
||||
|
||||
try identifier.encode(id, forKey: .id)
|
||||
@@ -143,7 +218,19 @@ extension ToOneRelationship {
|
||||
extension ToManyRelationship {
|
||||
public init(from decoder: Decoder) throws {
|
||||
let container = try decoder.container(keyedBy: ResourceLinkageCodingKeys.self)
|
||||
|
||||
|
||||
if let noMeta = NoMetadata() as? MetaType {
|
||||
meta = noMeta
|
||||
} else {
|
||||
meta = try container.decode(MetaType.self, forKey: .meta)
|
||||
}
|
||||
|
||||
if let noLinks = NoLinks() as? LinksType {
|
||||
links = noLinks
|
||||
} else {
|
||||
links = try container.decode(LinksType.self, forKey: .links)
|
||||
}
|
||||
|
||||
var identifiers = try container.nestedUnkeyedContainer(forKey: .data)
|
||||
|
||||
var newIds = [Relatable.Identifier]()
|
||||
@@ -163,6 +250,15 @@ extension ToManyRelationship {
|
||||
|
||||
public func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.container(keyedBy: ResourceLinkageCodingKeys.self)
|
||||
|
||||
if MetaType.self != NoMetadata.self {
|
||||
try container.encode(meta, forKey: .meta)
|
||||
}
|
||||
|
||||
if LinksType.self != NoLinks.self {
|
||||
try container.encode(links, forKey: .links)
|
||||
}
|
||||
|
||||
var identifiers = container.nestedUnkeyedContainer(forKey: .data)
|
||||
|
||||
for id in ids {
|
||||
|
||||
@@ -45,6 +45,10 @@ extension TransformedAttribute: AttributeTypeWithOptionalArray where RawValue: O
|
||||
private protocol OptionalRelationshipType {}
|
||||
extension Optional: OptionalRelationshipType where Wrapped: RelationshipType {}
|
||||
|
||||
private protocol _RelationshipType {}
|
||||
extension ToOneRelationship: _RelationshipType {}
|
||||
extension ToManyRelationship: _RelationshipType {}
|
||||
|
||||
public extension Entity {
|
||||
public static func check(_ entity: Entity) throws {
|
||||
var problems = [EntityCheckError]()
|
||||
@@ -72,7 +76,7 @@ public extension Entity {
|
||||
}
|
||||
|
||||
for relationship in relationshipsMirror.children {
|
||||
if relationship.value as? RelationshipType == nil {
|
||||
if relationship.value as? _RelationshipType == nil {
|
||||
problems.append(.nonRelationship(named: relationship.label ?? "unnamed"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,14 +7,14 @@
|
||||
|
||||
import JSONAPI
|
||||
|
||||
extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral {
|
||||
extension ToOneRelationship: ExpressibleByNilLiteral where Relatable.WrappedIdentifier: ExpressibleByNilLiteral, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public init(nilLiteral: ()) {
|
||||
|
||||
self.init(id: Relatable.WrappedIdentifier(nilLiteral: ()))
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.WrappedIdentifier: ExpressibleByUnicodeScalarLiteral {
|
||||
extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.WrappedIdentifier: ExpressibleByUnicodeScalarLiteral, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public typealias UnicodeScalarLiteralType = Relatable.WrappedIdentifier.UnicodeScalarLiteralType
|
||||
|
||||
public init(unicodeScalarLiteral value: UnicodeScalarLiteralType) {
|
||||
@@ -22,7 +22,7 @@ extension ToOneRelationship: ExpressibleByUnicodeScalarLiteral where Relatable.W
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where Relatable.WrappedIdentifier: ExpressibleByExtendedGraphemeClusterLiteral {
|
||||
extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where Relatable.WrappedIdentifier: ExpressibleByExtendedGraphemeClusterLiteral, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public typealias ExtendedGraphemeClusterLiteralType = Relatable.WrappedIdentifier.ExtendedGraphemeClusterLiteralType
|
||||
|
||||
public init(extendedGraphemeClusterLiteral value: ExtendedGraphemeClusterLiteralType) {
|
||||
@@ -30,7 +30,7 @@ extension ToOneRelationship: ExpressibleByExtendedGraphemeClusterLiteral where R
|
||||
}
|
||||
}
|
||||
|
||||
extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedIdentifier: ExpressibleByStringLiteral {
|
||||
extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedIdentifier: ExpressibleByStringLiteral, MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public typealias StringLiteralType = Relatable.WrappedIdentifier.StringLiteralType
|
||||
|
||||
public init(stringLiteral value: StringLiteralType) {
|
||||
@@ -38,7 +38,7 @@ extension ToOneRelationship: ExpressibleByStringLiteral where Relatable.WrappedI
|
||||
}
|
||||
}
|
||||
|
||||
extension ToManyRelationship: ExpressibleByArrayLiteral {
|
||||
extension ToManyRelationship: ExpressibleByArrayLiteral where MetaType == NoMetadata, LinksType == NoLinks {
|
||||
public typealias ArrayLiteralElement = Relatable.Identifier
|
||||
|
||||
public init(arrayLiteral elements: ArrayLiteralElement...) {
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
//
|
||||
// APIDescriptionTests.swift
|
||||
// JSONAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 12/2/18.
|
||||
//
|
||||
|
||||
import XCTest
|
||||
import JSONAPI
|
||||
|
||||
class APIDescriptionTests: XCTestCase {
|
||||
|
||||
func test_NoDescriptionString() {
|
||||
XCTAssertEqual(String(describing: NoAPIDescription()), "No JSON:API Object")
|
||||
}
|
||||
|
||||
func test_empty() {
|
||||
let description = decoded(type: APIDescription<NoMetadata>.self, data: api_description_empty)
|
||||
|
||||
XCTAssertEqual(description.version, "1.0")
|
||||
}
|
||||
|
||||
func test_WithVersion() {
|
||||
let description = decoded(type: APIDescription<NoMetadata>.self, data: api_description_with_version)
|
||||
|
||||
XCTAssertEqual(description.version, "1.5")
|
||||
}
|
||||
|
||||
func test_WithMeta() {
|
||||
let description = decoded(type: APIDescription<TestMetadata>.self, data: api_description_with_meta)
|
||||
|
||||
XCTAssertEqual(description.version, "1.0")
|
||||
XCTAssertEqual(description.meta.hello, "world")
|
||||
XCTAssertEqual(description.meta.number, 10)
|
||||
}
|
||||
|
||||
func test_WithVersionAndMeta() {
|
||||
let description = decoded(type: APIDescription<TestMetadata>.self, data: api_description_with_version_and_meta)
|
||||
|
||||
XCTAssertEqual(description.version, "2.0")
|
||||
XCTAssertEqual(description.meta.hello, "world")
|
||||
XCTAssertEqual(description.meta.number, 10)
|
||||
}
|
||||
|
||||
func test_failsMissingMeta() {
|
||||
XCTAssertThrowsError(try JSONDecoder().decode(APIDescription<TestMetadata>.self, from: api_description_with_version))
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test types
|
||||
extension APIDescriptionTests {
|
||||
struct TestMetadata: JSONAPI.Meta {
|
||||
let hello: String
|
||||
let number: Int
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
//
|
||||
// APIDescriptionsStubs.swift
|
||||
// JSONAPITests
|
||||
//
|
||||
// Created by Mathew Polzin on 12/2/18.
|
||||
//
|
||||
|
||||
let api_description_empty = """
|
||||
{}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let api_description_with_version = """
|
||||
{
|
||||
"version": "1.5"
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let api_description_with_meta = """
|
||||
{
|
||||
"meta": {
|
||||
"hello": "world",
|
||||
"number": 10
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let api_description_with_version_and_meta = """
|
||||
{
|
||||
"version": "2.0",
|
||||
"meta": {
|
||||
"hello": "world",
|
||||
"number": 10
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
@@ -60,7 +60,7 @@ extension Attribute_FunctorTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestType = Entity<TestTypeDescription>
|
||||
typealias TestType = BasicEntity<TestTypeDescription>
|
||||
|
||||
enum DoubleToString: Transformer {
|
||||
public static func transform(_ from: Double) -> String {
|
||||
|
||||
@@ -49,13 +49,13 @@ extension ComputedPropertiesTests {
|
||||
}
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
public let other: ToOneRelationship<TestType>
|
||||
public let other: ToOneRelationship<TestType, NoMetadata, NoLinks>
|
||||
|
||||
public var computed: ToOneRelationship<TestType> {
|
||||
public var computed: ToOneRelationship<TestType, NoMetadata, NoLinks> {
|
||||
return other
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public typealias TestType = Entity<TestTypeDescription>
|
||||
public typealias TestType = BasicEntity<TestTypeDescription>
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -13,14 +13,14 @@ class EntityTests: XCTestCase {
|
||||
|
||||
func test_relationship_access() {
|
||||
let entity1 = TestEntity1()
|
||||
let entity2 = TestEntity2(other: entity1.pointer)
|
||||
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
|
||||
|
||||
XCTAssertEqual(entity2.relationships.other, entity1.pointer)
|
||||
}
|
||||
|
||||
func test_relationship_operator_access() {
|
||||
let entity1 = TestEntity1()
|
||||
let entity2 = TestEntity2(other: entity1.pointer)
|
||||
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
|
||||
|
||||
XCTAssertEqual(entity2 ~> \.other, entity1.id)
|
||||
}
|
||||
@@ -29,34 +29,51 @@ class EntityTests: XCTestCase {
|
||||
let entity1 = TestEntity1()
|
||||
let entity2 = TestEntity1()
|
||||
let entity4 = TestEntity1()
|
||||
let entity3 = TestEntity3(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer]))
|
||||
let entity3 = TestEntity3(relationships: .init(others: .init(relationships: [entity1.pointer, entity2.pointer, entity4.pointer])))
|
||||
|
||||
XCTAssertEqual(entity3 ~> \.others, [entity1.id, entity2.id, entity4.id])
|
||||
}
|
||||
|
||||
func test_relationshipIds() {
|
||||
let entity1 = TestEntity1()
|
||||
let entity2 = TestEntity2(other: entity1.pointer)
|
||||
let entity2 = TestEntity2(relationships: .init(other: entity1.pointer))
|
||||
|
||||
XCTAssertEqual(entity2.relationships.other.id, entity1.id)
|
||||
}
|
||||
|
||||
func test_pointerWithMetaAndLinks() {
|
||||
let entity = TestEntity4WithMetaAndLinks(attributes: .init(word: "hello", number: 10, array: []), relationships: .init(other: .init(id: "2")), meta: .init(x: "world", y: nil), links: .init(link1: .init(url: "ok")))
|
||||
|
||||
let pointer = entity.pointer(withMeta: TestEntityMeta(x: "world", y: nil), links: TestEntityLinks(link1: .init(url: "ok")))
|
||||
|
||||
XCTAssertEqual(pointer.id, entity.id)
|
||||
XCTAssertEqual(pointer.meta.x, "world")
|
||||
XCTAssertEqual(pointer.links.link1.url, "ok")
|
||||
}
|
||||
|
||||
func test_initialization() {
|
||||
let entity1 = TestEntity1(id: .init(rawValue: "wow"))
|
||||
let entity2 = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)))
|
||||
let _ = TestEntity2(id: .init(rawValue: "cool"), attributes: .none, relationships: .init(other: .init(entity: entity1)))
|
||||
let _ = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)), meta: .none)
|
||||
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id])))
|
||||
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .none))
|
||||
let _ = TestEntity4(id: .init(rawValue: "4"), attributes: .init(word: .init(value: "hello"), number: .init(value: 10), array: .init(value: [10.2, 10.3])), relationships: .init(other: entity2.pointer))
|
||||
let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2)))
|
||||
let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil)))
|
||||
let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world")))
|
||||
XCTAssertNoThrow(try TestEntity8(id: .init(rawValue: "8"), attributes: .init(string: .init(value: "hello"), int: .init(value: 10), stringFromInt: .init(rawValue: 20), plus: .init(rawValue: 30), doubleFromInt: .init(rawValue: 32), omitted: nil, nullToString: .init(rawValue: nil))))
|
||||
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil))
|
||||
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: nil)))
|
||||
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: entity1, meta: .none, links: .none)))
|
||||
let e10id1 = TestEntity10.Identifier(rawValue: "hello")
|
||||
let e10id2 = TestEntity10.Id(rawValue: "world")
|
||||
let e10id3: TestEntity10.Id = "!"
|
||||
let _ = TestEntity10(id: .init(rawValue: "10"), relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])))
|
||||
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11))))
|
||||
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")))
|
||||
let _ = UnidentifiedTestEntityWithMeta(attributes: .init(me: .init(value: "hello")), meta: .init(x: "world", y: nil))
|
||||
let _ = UnidentifiedTestEntityWithLinks(attributes: .init(me: .init(value: "hello")), links: .init(link1: .init(url: "hmmm")))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -336,6 +353,109 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: With Meta and/or Links
|
||||
|
||||
extension EntityTests {
|
||||
func test_UnidentifiedEntityWithAttributesAndMeta() {
|
||||
let entity = decoded(type: UnidentifiedTestEntityWithMeta.self,
|
||||
data: entity_unidentified_with_attributes_and_meta)
|
||||
|
||||
XCTAssertEqual(entity[\.me], "unknown")
|
||||
XCTAssertEqual(entity.id, .unidentified)
|
||||
XCTAssertEqual(entity.meta.x, "world")
|
||||
XCTAssertEqual(entity.meta.y, 5)
|
||||
XCTAssertNoThrow(try UnidentifiedTestEntityWithMeta.check(entity))
|
||||
}
|
||||
|
||||
func test_UnidentifiedEntityWithAttributesAndMeta_encode() {
|
||||
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithMeta.self,
|
||||
data: entity_unidentified_with_attributes_and_meta)
|
||||
}
|
||||
|
||||
func test_UnidentifiedEntityWithAttributesAndLinks() {
|
||||
let entity = decoded(type: UnidentifiedTestEntityWithLinks.self,
|
||||
data: entity_unidentified_with_attributes_and_links)
|
||||
|
||||
XCTAssertEqual(entity[\.me], "unknown")
|
||||
XCTAssertEqual(entity.id, .unidentified)
|
||||
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
|
||||
XCTAssertNoThrow(try UnidentifiedTestEntityWithLinks.check(entity))
|
||||
}
|
||||
|
||||
func test_UnidentifiedEntityWithAttributesAndLinks_encode() {
|
||||
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithLinks.self,
|
||||
data: entity_unidentified_with_attributes_and_links)
|
||||
}
|
||||
|
||||
func test_UnidentifiedEntityWithAttributesAndMetaAndLinks() {
|
||||
let entity = decoded(type: UnidentifiedTestEntityWithMetaAndLinks.self,
|
||||
data: entity_unidentified_with_attributes_and_meta_and_links)
|
||||
|
||||
XCTAssertEqual(entity[\.me], "unknown")
|
||||
XCTAssertEqual(entity.id, .unidentified)
|
||||
XCTAssertEqual(entity.meta.x, "world")
|
||||
XCTAssertEqual(entity.meta.y, 5)
|
||||
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
|
||||
XCTAssertNoThrow(try UnidentifiedTestEntityWithMetaAndLinks.check(entity))
|
||||
}
|
||||
|
||||
func test_UnidentifiedEntityWithAttributesAndMetaAndLinks_encode() {
|
||||
test_DecodeEncodeEquality(type: UnidentifiedTestEntityWithMetaAndLinks.self,
|
||||
data: entity_unidentified_with_attributes_and_meta_and_links)
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithMeta() {
|
||||
let entity = decoded(type: TestEntity4WithMeta.self,
|
||||
data: entity_some_relationships_some_attributes_with_meta)
|
||||
|
||||
XCTAssertEqual(entity[\.word], "coolio")
|
||||
XCTAssertEqual(entity[\.number], 992299)
|
||||
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
|
||||
XCTAssertEqual(entity.meta.x, "world")
|
||||
XCTAssertEqual(entity.meta.y, 5)
|
||||
XCTAssertNoThrow(try TestEntity4WithMeta.check(entity))
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithMeta_encode() {
|
||||
test_DecodeEncodeEquality(type: TestEntity4WithMeta.self,
|
||||
data: entity_some_relationships_some_attributes_with_meta)
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithLinks() {
|
||||
let entity = decoded(type: TestEntity4WithLinks.self,
|
||||
data: entity_some_relationships_some_attributes_with_links)
|
||||
|
||||
XCTAssertEqual(entity[\.word], "coolio")
|
||||
XCTAssertEqual(entity[\.number], 992299)
|
||||
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
|
||||
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
|
||||
XCTAssertNoThrow(try TestEntity4WithLinks.check(entity))
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithLinks_encode() {
|
||||
test_DecodeEncodeEquality(type: TestEntity4WithLinks.self,
|
||||
data: entity_some_relationships_some_attributes_with_links)
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithMetaAndLinks() {
|
||||
let entity = decoded(type: TestEntity4WithMetaAndLinks.self,
|
||||
data: entity_some_relationships_some_attributes_with_meta_and_links)
|
||||
|
||||
XCTAssertEqual(entity[\.word], "coolio")
|
||||
XCTAssertEqual(entity[\.number], 992299)
|
||||
XCTAssertEqual((entity ~> \.other).rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF")
|
||||
XCTAssertEqual(entity.meta.x, "world")
|
||||
XCTAssertEqual(entity.meta.y, 5)
|
||||
XCTAssertEqual(entity.links.link1, .init(url: "https://image.com/image.png"))
|
||||
XCTAssertNoThrow(try TestEntity4WithMetaAndLinks.check(entity))
|
||||
}
|
||||
|
||||
func test_EntitySomeRelationshipsSomeAttributesWithMetaAndLinks_encode() {
|
||||
test_DecodeEncodeEquality(type: TestEntity4WithMetaAndLinks.self,
|
||||
data: entity_some_relationships_some_attributes_with_meta_and_links)
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Test Types
|
||||
extension EntityTests {
|
||||
|
||||
@@ -346,7 +466,7 @@ extension EntityTests {
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity1 = Entity<TestEntityType1>
|
||||
typealias TestEntity1 = BasicEntity<TestEntityType1>
|
||||
|
||||
enum TestEntityType2: EntityDescription {
|
||||
static var type: String { return "second_test_entities"}
|
||||
@@ -354,11 +474,11 @@ extension EntityTests {
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let other: ToOneRelationship<TestEntity1>
|
||||
let other: ToOneRelationship<TestEntity1, NoMetadata, NoLinks>
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity2 = Entity<TestEntityType2>
|
||||
typealias TestEntity2 = BasicEntity<TestEntityType2>
|
||||
|
||||
enum TestEntityType3: EntityDescription {
|
||||
static var type: String { return "third_test_entities"}
|
||||
@@ -366,17 +486,17 @@ extension EntityTests {
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let others: ToManyRelationship<TestEntity1>
|
||||
let others: ToManyRelationship<TestEntity1, NoMetadata, NoLinks>
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity3 = Entity<TestEntityType3>
|
||||
typealias TestEntity3 = BasicEntity<TestEntityType3>
|
||||
|
||||
enum TestEntityType4: EntityDescription {
|
||||
static var type: String { return "fourth_test_entities"}
|
||||
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let other: ToOneRelationship<TestEntity2>
|
||||
let other: ToOneRelationship<TestEntity2, NoMetadata, NoLinks>
|
||||
}
|
||||
|
||||
struct Attributes: JSONAPI.Attributes {
|
||||
@@ -386,7 +506,13 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity4 = Entity<TestEntityType4>
|
||||
typealias TestEntity4 = BasicEntity<TestEntityType4>
|
||||
|
||||
typealias TestEntity4WithMeta = Entity<TestEntityType4, TestEntityMeta, NoLinks>
|
||||
|
||||
typealias TestEntity4WithLinks = Entity<TestEntityType4, NoMetadata, TestEntityLinks>
|
||||
|
||||
typealias TestEntity4WithMetaAndLinks = Entity<TestEntityType4, TestEntityMeta, TestEntityLinks>
|
||||
|
||||
enum TestEntityType5: EntityDescription {
|
||||
static var type: String { return "fifth_test_entities"}
|
||||
@@ -398,7 +524,7 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity5 = Entity<TestEntityType5>
|
||||
typealias TestEntity5 = BasicEntity<TestEntityType5>
|
||||
|
||||
enum TestEntityType6: EntityDescription {
|
||||
static var type: String { return "sixth_test_entities" }
|
||||
@@ -412,7 +538,7 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity6 = Entity<TestEntityType6>
|
||||
typealias TestEntity6 = BasicEntity<TestEntityType6>
|
||||
|
||||
enum TestEntityType7: EntityDescription {
|
||||
static var type: String { return "seventh_test_entities" }
|
||||
@@ -425,7 +551,7 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity7 = Entity<TestEntityType7>
|
||||
typealias TestEntity7 = BasicEntity<TestEntityType7>
|
||||
|
||||
enum TestEntityType8: EntityDescription {
|
||||
static var type: String { return "eighth_test_entities" }
|
||||
@@ -443,7 +569,7 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity8 = Entity<TestEntityType8>
|
||||
typealias TestEntity8 = BasicEntity<TestEntityType8>
|
||||
|
||||
enum TestEntityType9: EntityDescription {
|
||||
public static var type: String { return "ninth_test_entities" }
|
||||
@@ -451,9 +577,9 @@ extension EntityTests {
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let one: ToOneRelationship<TestEntity1>
|
||||
let one: ToOneRelationship<TestEntity1, NoMetadata, NoLinks>
|
||||
|
||||
let nullableOne: ToOneRelationship<TestEntity1?>
|
||||
let nullableOne: ToOneRelationship<TestEntity1?, NoMetadata, NoLinks>
|
||||
|
||||
// a nullable many is not allowed. it should
|
||||
// just be an empty array.
|
||||
@@ -465,7 +591,7 @@ extension EntityTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity9 = Entity<TestEntityType9>
|
||||
typealias TestEntity9 = BasicEntity<TestEntityType9>
|
||||
|
||||
enum TestEntityType10: EntityDescription {
|
||||
public static var type: String { return "tenth_test_entities" }
|
||||
@@ -473,12 +599,12 @@ extension EntityTests {
|
||||
typealias Attributes = NoAttributes
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let selfRef: ToOneRelationship<TestEntity10>
|
||||
let selfRefs: ToManyRelationship<TestEntity10>
|
||||
let selfRef: ToOneRelationship<TestEntity10, NoMetadata, NoLinks>
|
||||
let selfRefs: ToManyRelationship<TestEntity10, NoMetadata, NoLinks>
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity10 = Entity<TestEntityType10>
|
||||
typealias TestEntity10 = BasicEntity<TestEntityType10>
|
||||
|
||||
enum TestEntityType11: EntityDescription {
|
||||
public static var type: String { return "eleventh_test_entities" }
|
||||
@@ -490,7 +616,7 @@ extension EntityTests {
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias TestEntity11 = Entity<TestEntityType11>
|
||||
typealias TestEntity11 = BasicEntity<TestEntityType11>
|
||||
|
||||
enum UnidentifiedTestEntityType: EntityDescription {
|
||||
public static var type: String { return "unidentified_test_entities" }
|
||||
@@ -502,7 +628,13 @@ extension EntityTests {
|
||||
typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
typealias UnidentifiedTestEntity = NewEntity<UnidentifiedTestEntityType>
|
||||
typealias UnidentifiedTestEntity = NewEntity<UnidentifiedTestEntityType, NoMetadata, NoLinks>
|
||||
|
||||
typealias UnidentifiedTestEntityWithMeta = NewEntity<UnidentifiedTestEntityType, TestEntityMeta, NoLinks>
|
||||
|
||||
typealias UnidentifiedTestEntityWithLinks = NewEntity<UnidentifiedTestEntityType, NoMetadata, TestEntityLinks>
|
||||
|
||||
typealias UnidentifiedTestEntityWithMetaAndLinks = NewEntity<UnidentifiedTestEntityType, TestEntityMeta, TestEntityLinks>
|
||||
|
||||
enum IntToString: Transformer {
|
||||
public static func transform(_ from: Int) -> String {
|
||||
@@ -540,16 +672,13 @@ extension EntityTests {
|
||||
return from
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description == EntityTests.TestEntityType2, EntityRawIdType: CreatableRawIdType {
|
||||
init(other: ToOneRelationship<EntityTests.TestEntity1>) {
|
||||
self.init(relationships: .init(other: other))
|
||||
struct TestEntityMeta: JSONAPI.Meta {
|
||||
let x: String
|
||||
let y: Int?
|
||||
}
|
||||
}
|
||||
|
||||
extension Entity where Description == EntityTests.TestEntityType3, EntityRawIdType: CreatableRawIdType {
|
||||
init(others: ToManyRelationship<EntityTests.TestEntity1>) {
|
||||
self.init(relationships: .init(others: others))
|
||||
struct TestEntityLinks: JSONAPI.Links {
|
||||
let link1: Link<String, NoMetadata>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +57,83 @@ let entity_some_relationships_some_attributes = """
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_some_relationships_some_attributes_with_meta = """
|
||||
{
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
|
||||
"type": "fourth_test_entities",
|
||||
"attributes": {
|
||||
"word": "coolio",
|
||||
"number": 992299,
|
||||
"array": [12.3, 4, 0.1]
|
||||
},
|
||||
"relationships": {
|
||||
"other": {
|
||||
"data": {
|
||||
"type": "second_test_entities",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
}
|
||||
},
|
||||
"meta": {
|
||||
"x": "world",
|
||||
"y": 5
|
||||
},
|
||||
"links": {
|
||||
"link1": "https://image.com/image.png"
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_some_relationships_some_attributes_with_links = """
|
||||
{
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
|
||||
"type": "fourth_test_entities",
|
||||
"attributes": {
|
||||
"word": "coolio",
|
||||
"number": 992299,
|
||||
"array": [12.3, 4, 0.1]
|
||||
},
|
||||
"relationships": {
|
||||
"other": {
|
||||
"data": {
|
||||
"type": "second_test_entities",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
}
|
||||
},
|
||||
"links": {
|
||||
"link1": "https://image.com/image.png"
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_some_relationships_some_attributes_with_meta_and_links = """
|
||||
{
|
||||
"id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333",
|
||||
"type": "fourth_test_entities",
|
||||
"attributes": {
|
||||
"word": "coolio",
|
||||
"number": 992299,
|
||||
"array": [12.3, 4, 0.1]
|
||||
},
|
||||
"relationships": {
|
||||
"other": {
|
||||
"data": {
|
||||
"type": "second_test_entities",
|
||||
"id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"
|
||||
}
|
||||
}
|
||||
},
|
||||
"meta": {
|
||||
"x": "world",
|
||||
"y": 5
|
||||
},
|
||||
"links": {
|
||||
"link1": "https://image.com/image.png"
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_one_omitted_attribute = """
|
||||
{
|
||||
"id": "1",
|
||||
@@ -241,3 +318,44 @@ let entity_unidentified_with_attributes = """
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_unidentified_with_attributes_and_meta = """
|
||||
{
|
||||
"type": "unidentified_test_entities",
|
||||
"attributes": {
|
||||
"me": "unknown"
|
||||
},
|
||||
"meta": {
|
||||
"x": "world",
|
||||
"y": 5
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_unidentified_with_attributes_and_links = """
|
||||
{
|
||||
"type": "unidentified_test_entities",
|
||||
"attributes": {
|
||||
"me": "unknown"
|
||||
},
|
||||
"links": {
|
||||
"link1": "https://image.com/image.png"
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
let entity_unidentified_with_attributes_and_meta_and_links = """
|
||||
{
|
||||
"type": "unidentified_test_entities",
|
||||
"attributes": {
|
||||
"me": "unknown"
|
||||
},
|
||||
"meta": {
|
||||
"x": "world",
|
||||
"y": 5
|
||||
},
|
||||
"links": {
|
||||
"link1": "https://image.com/image.png"
|
||||
}
|
||||
}
|
||||
""".data(using: .utf8)!
|
||||
|
||||
@@ -137,14 +137,14 @@ extension IncludedTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity = Entity<TestEntityType>
|
||||
typealias TestEntity = BasicEntity<TestEntityType>
|
||||
|
||||
enum TestEntityType2: EntityDescription {
|
||||
|
||||
public static var type: String { return "test_entity2" }
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let entity1: ToOneRelationship<TestEntity>
|
||||
let entity1: ToOneRelationship<TestEntity, NoMetadata, NoLinks>
|
||||
}
|
||||
|
||||
public struct Attributes: JSONAPI.Attributes {
|
||||
@@ -153,7 +153,7 @@ extension IncludedTests {
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity2 = Entity<TestEntityType2>
|
||||
typealias TestEntity2 = BasicEntity<TestEntityType2>
|
||||
|
||||
enum TestEntityType3: EntityDescription {
|
||||
|
||||
@@ -162,12 +162,12 @@ extension IncludedTests {
|
||||
public static var type: String { return "test_entity3" }
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let entity1: ToOneRelationship<TestEntity>
|
||||
let entity2: ToManyRelationship<TestEntity2>
|
||||
let entity1: ToOneRelationship<TestEntity, NoMetadata, NoLinks>
|
||||
let entity2: ToManyRelationship<TestEntity2, NoMetadata, NoLinks>
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity3 = Entity<TestEntityType3>
|
||||
typealias TestEntity3 = BasicEntity<TestEntityType3>
|
||||
|
||||
enum TestEntityType4: EntityDescription {
|
||||
|
||||
@@ -178,7 +178,7 @@ extension IncludedTests {
|
||||
public static var type: String { return "test_entity4" }
|
||||
}
|
||||
|
||||
typealias TestEntity4 = Entity<TestEntityType4>
|
||||
typealias TestEntity4 = BasicEntity<TestEntityType4>
|
||||
|
||||
enum TestEntityType5: EntityDescription {
|
||||
|
||||
@@ -189,7 +189,7 @@ extension IncludedTests {
|
||||
public static var type: String { return "test_entity5" }
|
||||
}
|
||||
|
||||
typealias TestEntity5 = Entity<TestEntityType5>
|
||||
typealias TestEntity5 = BasicEntity<TestEntityType5>
|
||||
|
||||
enum TestEntityType6: EntityDescription {
|
||||
|
||||
@@ -198,9 +198,9 @@ extension IncludedTests {
|
||||
public static var type: String { return "test_entity6" }
|
||||
|
||||
struct Relationships: JSONAPI.Relationships {
|
||||
let entity4: ToOneRelationship<TestEntity4>
|
||||
let entity4: ToOneRelationship<TestEntity4, NoMetadata, NoLinks>
|
||||
}
|
||||
}
|
||||
|
||||
typealias TestEntity6 = Entity<TestEntityType6>
|
||||
typealias TestEntity6 = BasicEntity<TestEntityType6>
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ extension EntityCheckTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
public typealias OkEntity = Entity<OkDescription>
|
||||
public typealias OkEntity = BasicEntity<OkDescription>
|
||||
|
||||
enum OtherOkDescription: EntityDescription {
|
||||
public static var type: String { return "hmm" }
|
||||
@@ -56,7 +56,7 @@ extension EntityCheckTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
public typealias OtherOkEntity = Entity<OtherOkDescription>
|
||||
public typealias OtherOkEntity = BasicEntity<OtherOkDescription>
|
||||
|
||||
enum EnumAttributesDescription: EntityDescription {
|
||||
public static var type: String { return "hello" }
|
||||
@@ -75,7 +75,7 @@ extension EntityCheckTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
public typealias EnumAttributesEntity = Entity<EnumAttributesDescription>
|
||||
public typealias EnumAttributesEntity = BasicEntity<EnumAttributesDescription>
|
||||
|
||||
enum EnumRelationshipsDescription: EntityDescription {
|
||||
public static var type: String { return "hello" }
|
||||
@@ -94,7 +94,7 @@ extension EntityCheckTests {
|
||||
}
|
||||
}
|
||||
|
||||
public typealias EnumRelationshipsEntity = Entity<EnumRelationshipsDescription>
|
||||
public typealias EnumRelationshipsEntity = BasicEntity<EnumRelationshipsDescription>
|
||||
|
||||
enum BadAttributeDescription: EntityDescription {
|
||||
public static var type: String { return "hello" }
|
||||
@@ -107,7 +107,7 @@ extension EntityCheckTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
public typealias BadAttributeEntity = Entity<BadAttributeDescription>
|
||||
public typealias BadAttributeEntity = BasicEntity<BadAttributeDescription>
|
||||
|
||||
enum BadRelationshipDescription: EntityDescription {
|
||||
public static var type: String { return "hello" }
|
||||
@@ -115,12 +115,12 @@ extension EntityCheckTests {
|
||||
public typealias Attributes = NoAttributes
|
||||
|
||||
public struct Relationships: JSONAPI.Relationships {
|
||||
let x: ToOneRelationship<OkEntity>
|
||||
let x: ToOneRelationship<OkEntity, NoMetadata, NoLinks>
|
||||
let y: Id<String, OkEntity>
|
||||
}
|
||||
}
|
||||
|
||||
public typealias BadRelationshipEntity = Entity<BadRelationshipDescription>
|
||||
public typealias BadRelationshipEntity = BasicEntity<BadRelationshipDescription>
|
||||
|
||||
enum OptionalArrayAttributeDescription: EntityDescription {
|
||||
public static var type: String { return "hello" }
|
||||
@@ -133,5 +133,5 @@ extension EntityCheckTests {
|
||||
public typealias Relationships = NoRelationships
|
||||
}
|
||||
|
||||
public typealias OptionalArrayAttributeEntity = Entity<OptionalArrayAttributeDescription>
|
||||
public typealias OptionalArrayAttributeEntity = BasicEntity<OptionalArrayAttributeDescription>
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user