Skip to content

Commit 7824bd8

Browse files
authored
Merge pull request #30 from mattpolzin/release/1.0
Release/1.0
2 parents 9a9403b + 89217f7 commit 7824bd8

8 files changed

Lines changed: 46 additions & 41 deletions

File tree

JSONAPI.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Pod::Spec.new do |spec|
1616
#
1717

1818
spec.name = "JSONAPI"
19-
spec.version = "0.31.1"
19+
spec.version = "1.0.0"
2020
spec.summary = "Swift Codable JSON API framework."
2121

2222
# This description is used to generate tags and improve search results.

README.md

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ A Swift package for encoding to- and decoding from **JSON API** compliant reques
55

66
See the JSON API Spec here: https://jsonapi.org/format/
77

8-
:warning: Although I find the type-safety of this framework appealing, the Swift compiler currently has enough trouble with it that it can become difficult to reason about errors produced by small typos. Similarly, auto-complete fails to provide reasonable suggestions much of the time. If you get the code right, everything compiles, otherwise it can suck to figure out what is wrong. This is mostly a concern when creating resource objects in-code (servers and test suites must do this). Writing a client that uses this framework to ingest JSON API Compliant API responses is much less painful. :warning:
8+
:warning: This library provides well-tested type safety when working with JSON:API 1.0, however the Swift compiler can sometimes have difficulty tracking down small typos when initializing `ResourceObjects`. Once the code is written correctly, it will compile, but tracking down the source of programmer errors can be an annoyance. This is mostly a concern when creating resource objects in-code (servers and test cases must do this). Writing a client that uses this framework to ingest JSON API Compliant API responses is much less painful. :warning:
99

1010
## Table of Contents
1111
<!-- TOC depthFrom:1 depthTo:6 withLinks:1 updateOnSave:1 orderedList:0 -->
@@ -109,52 +109,34 @@ Note that Playground support for importing non-system Frameworks is still a bit
109109

110110
### JSON:API
111111
#### Document
112-
- `data`
113-
- [x] Encoding/Decoding
114-
- `included`
115-
- [x] Encoding/Decoding
116-
- `errors`
117-
- [x] Encoding/Decoding
118-
- `meta`
119-
- [x] Encoding/Decoding
120-
- `jsonapi` (i.e. API Information)
121-
- [x] Encoding/Decoding
122-
- `links`
123-
- [x] Encoding/Decoding
112+
- [x] `data`
113+
- [x] `included`
114+
- [x] `errors`
115+
- [x] `meta`
116+
- [x] `jsonapi` (i.e. API Information)
117+
- [x] `links`
124118

125119
#### Resource Object
126-
- `id`
127-
- [x] Encoding/Decoding
128-
- `type`
129-
- [x] Encoding/Decoding
130-
- `attributes`
131-
- [x] Encoding/Decoding
132-
- `relationships`
133-
- [x] Encoding/Decoding
134-
- `links`
135-
- [x] Encoding/Decoding
136-
- `meta`
137-
- [x] Encoding/Decoding
120+
- [x] `id`
121+
- [x] `type`
122+
- [x] `attributes`
123+
- [x] `relationships`
124+
- [x] `links`
125+
- [x] `meta`
138126

139127
#### Relationship Object
140-
- `data`
141-
- [x] Encoding/Decoding
142-
- `links`
143-
- [x] Encoding/Decoding
144-
- `meta`
145-
- [x] Encoding/Decoding
128+
- [x] `data`
129+
- [x] `links`
130+
- [x] `meta`
146131

147132
#### Links Object
148-
- `href`
149-
- [x] Encoding/Decoding
150-
- `meta`
151-
- [x] Encoding/Decoding
133+
- [x] `href`
134+
- [x] `meta`
152135

153136
### Misc
154137
- [x] Support transforms on `Attributes` values (e.g. to support different representations of `Date`)
155138
- [x] Support validation on `Attributes`.
156-
- [x] Support sparse fieldsets (encoding only). A client can likely just define a new model to represent a sparse population of another model in a very specific use case. On the server side, sparse fieldsets of Resource Objects can be encoded without creating one model for every possible sparse fieldset.
157-
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
139+
- [x] Support sparse fieldsets (encoding only). A client can likely just define a new model to represent a sparse population of another model in a very specific use case for decoding purposes. On the server side, sparse fieldsets of Resource Objects can be encoded without creating one model for every possible sparse fieldset.
158140

159141
### Testing
160142
#### Resource Object Validator
@@ -163,6 +145,8 @@ Note that Playground support for importing non-system Frameworks is still a bit
163145
- [x] Only allow `ToManyRelationship` and `ToOneRelationship` within `Relationships` struct.
164146

165147
### Potential Improvements
148+
These ideas could be implemented in future versions.
149+
166150
- [ ] (Maybe) Use `KeyPath` to specify `Includes` thus creating type safety around the relationship between a primary resource type and the types of included resources.
167151
- [ ] (Maybe) Replace `SingleResourceBody` and `ManyResourceBody` with support at the `Document` level to just interpret `PrimaryResource`, `PrimaryResource?`, or `[PrimaryResource]` as the same decoding/encoding strategies.
168152
- [ ] Support sideposting. JSONAPI spec might become opinionated in the future (https://github.com/json-api/json-api/pull/1197, https://github.com/json-api/json-api/issues/1215, https://github.com/json-api/json-api/issues/1216) but there is also an existing implementation to consider (https://jsonapi-suite.github.io/jsonapi_suite/ruby/writes/nested-writes). At this time, any sidepost implementation would be an awesome tertiary library to be used alongside the primary JSONAPI library. Maybe `JSONAPISideloading`.

Sources/JSONAPI/Document/Document.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public struct Document<PrimaryResourceBody: JSONAPI.EncodableResourceBody, MetaT
5353
case data(Data)
5454

5555
public struct Data: Equatable {
56+
/// The document's Primary Resource object(s)
5657
public let primary: PrimaryResourceBody
58+
/// The document's included objects
5759
public let includes: Includes<Include>
5860
public let meta: MetaType
5961
public let links: LinksType
@@ -66,6 +68,8 @@ public struct Document<PrimaryResourceBody: JSONAPI.EncodableResourceBody, MetaT
6668
}
6769
}
6870

71+
/// `true` if the document represents one or more errors. `false` if the
72+
/// document represents JSON:API data and/or metadata.
6973
public var isError: Bool {
7074
guard case .errors = self else { return false }
7175
return true

Sources/JSONAPI/Document/Includes.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import Poly
99

1010
public typealias Include = EncodableJSONPoly
1111

12+
/// A structure holding zero or more included Resource Objects.
13+
/// The resources are accessed by their type using a subscript.
14+
///
15+
/// If you have
16+
///
17+
/// `let includes: Includes<Include2<Thing1, Thing2>> = ...`
18+
///
19+
/// then you can access all `Thing1` included resources with
20+
///
21+
/// `let includedThings = includes[Thing1.self]`
1222
public struct Includes<I: Include>: Encodable, Equatable {
1323
public static var none: Includes { return .init(values: []) }
1424

Sources/JSONAPI/Document/ResourceBody.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ public func +<R: Appendable>(_ left: R, right: R) -> R {
4848
return left.appending(right)
4949
}
5050

51+
/// A type allowing for a document body containing 1 primary resource.
52+
/// If the `Entity` specialization is an `Optional` type, the body can contain
53+
/// 0 or 1 primary resources.
5154
public struct SingleResourceBody<Entity: JSONAPI.OptionalEncodablePrimaryResource>: EncodableResourceBody {
5255
public let value: Entity
5356

@@ -56,6 +59,7 @@ public struct SingleResourceBody<Entity: JSONAPI.OptionalEncodablePrimaryResourc
5659
}
5760
}
5861

62+
/// A type allowing for a document body containing 0 or more primary resources.
5963
public struct ManyResourceBody<Entity: JSONAPI.EncodablePrimaryResource>: EncodableResourceBody, Appendable {
6064
public let values: [Entity]
6165

Sources/JSONAPI/Resource/Id.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ public protocol CreatableRawIdType: RawIdType {
2828

2929
extension String: RawIdType {}
3030

31+
/// A type that can be used as the `MaybeRawId` for a `ResourceObject` that does not
32+
/// have an Id (most likely because it was created by a client and the server will be responsible
33+
/// for assigning it an Id).
3134
public struct Unidentified: MaybeRawId, CustomStringConvertible {
3235
public init() {}
3336

Sources/JSONAPI/SparseFields/SparseFieldEncoder.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Created by Mathew Polzin on 8/4/19.
66
//
77

8-
public class SparseFieldEncoder<SparseKey: CodingKey & Equatable>: Encoder {
8+
class SparseFieldEncoder<SparseKey: CodingKey & Equatable>: Encoder {
99
private let wrappedEncoder: Encoder
1010
private let allowedKeys: [SparseKey]
1111

@@ -37,7 +37,7 @@ public class SparseFieldEncoder<SparseKey: CodingKey & Equatable>: Encoder {
3737
}
3838
}
3939

40-
public struct SparseFieldKeyedEncodingContainer<Key, SparseKey>: KeyedEncodingContainerProtocol where SparseKey: CodingKey, SparseKey: Equatable, Key: CodingKey {
40+
struct SparseFieldKeyedEncodingContainer<Key, SparseKey>: KeyedEncodingContainerProtocol where SparseKey: CodingKey, SparseKey: Equatable, Key: CodingKey {
4141
private var wrappedContainer: KeyedEncodingContainer<Key>
4242
private let allowedKeys: [SparseKey]
4343

Tests/JSONAPITests/SparseFields/SparseFieldEncoderTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import XCTest
9-
import JSONAPI
9+
@testable import JSONAPI
1010
import Foundation
1111

1212
class SparseFieldEncoderTests: XCTestCase {

0 commit comments

Comments
 (0)