You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-12Lines changed: 36 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -47,8 +47,8 @@ Note that Playground support for importing non-system Frameworks is still a bit
47
47
-[x]`type`
48
48
-[x]`attributes`
49
49
-[x]`relationships`
50
-
-[]`links`
51
-
-[]`meta`
50
+
-[x]`links` (untested)
51
+
-[x]`meta` (untested)
52
52
53
53
#### Relationship Object
54
54
-[x]`data`
@@ -140,30 +140,44 @@ This readme doesn't go into detail on the **SPEC**, but the following *Resource
140
140
141
141
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.
142
142
143
-
The `Entity` and `EntityDescription` together embody the rules and properties of a JSON API *Resource Object*.
143
+
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*.
144
+
145
+
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`.
146
+
147
+
#### `Meta`
148
+
149
+
The second generic specialization on `Entity` is `Meta`. This is described in its own section [below](#meta). All `Meta` at any level of a JSON API Document follow the same rules.
150
+
151
+
#### `Links`
152
+
153
+
The third generic specialization on `Entity` is `Links`. This is described in its own section [below](#links). 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.
144
154
145
155
#### `IdType`
146
156
147
-
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>>`.
157
+
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>>`.
148
158
149
159
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.
150
160
151
161
A `RawIdType` is the underlying type that uniquely identifies an `Entity`. This is often a `String` or a `UUID`.
152
162
163
+
#### `MaybeRawId`
164
+
165
+
`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`).
166
+
153
167
#### Convenient `typealiases`
154
168
155
169
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:
156
170
```
157
-
public typealias Entity<Description: JSONAPI.EntityDescription> = JSONAPI.Entity<Description, String>
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.
@@ -284,7 +298,7 @@ You cannot, however, use an optional `PrimaryResource` with a `ManyResourceBody`
284
298
285
299
#### `MetaType`
286
300
287
-
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:
301
+
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:
288
302
```
289
303
{
290
304
"meta": {
@@ -308,9 +322,7 @@ You can always use `NoMetadata` if this JSON API feature is not needed.
308
322
309
323
#### `LinksType`
310
324
311
-
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`.
312
-
313
-
You can specify `NoLinks` if the document should not contain any links.
325
+
The third generic type of a `JSONAPIDocument` is a `Links` struct. `Links` are described in their own section [below](#links).
314
326
315
327
#### `IncludeType`
316
328
@@ -334,6 +346,18 @@ You can supply any `JSONAPI.Meta` type as the metadata type of the API descripti
334
346
335
347
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`.
336
348
349
+
### `Meta`
350
+
351
+
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.
352
+
353
+
You can specify `NoMetadata` if the part of the document being described should not contain any `Meta`.
354
+
355
+
### `Links`
356
+
357
+
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.
358
+
359
+
You can specify `NoLinks` if the part of the document being described should not contain any `Links`.
360
+
337
361
### `RawIdType`
338
362
339
363
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 commit comments