Skip to content

Commit 8d18be6

Browse files
committed
Update README after adding Meta and Links to Entity.
1 parent 8aa887b commit 8d18be6

1 file changed

Lines changed: 36 additions & 12 deletions

File tree

README.md

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ Note that Playground support for importing non-system Frameworks is still a bit
4747
- [x] `type`
4848
- [x] `attributes`
4949
- [x] `relationships`
50-
- [ ] `links`
51-
- [ ] `meta`
50+
- [x] `links` (untested)
51+
- [x] `meta` (untested)
5252

5353
#### Relationship Object
5454
- [x] `data`
@@ -140,30 +140,44 @@ This readme doesn't go into detail on the **SPEC**, but the following *Resource
140140

141141
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.
142142

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.
144154

145155
#### `IdType`
146156

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>>`.
148158

149159
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.
150160

151161
A `RawIdType` is the underlying type that uniquely identifies an `Entity`. This is often a `String` or a `UUID`.
152162

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+
153167
#### Convenient `typealiases`
154168

155169
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:
156170
```
157-
public typealias Entity<Description: JSONAPI.EntityDescription> = JSONAPI.Entity<Description, String>
171+
public typealias Entity<Description: JSONAPI.EntityDescription, Meta: JSONAPI.Meta, Links: JSONAPI.Links> = JSONAPI.Entity<Description, Meta, Links, String>
158172
159-
public typealias NewEntity<Description: JSONAPI.EntityDescription> = JSONAPI.Entity<Description, Unidentified>
173+
public typealias NewEntity<Description: JSONAPI.EntityDescription, Meta: JSONAPI.Meta, Links: JSONAPI.Links> = JSONAPI.Entity<Description, Meta, Links, Unidentified>
160174
```
161175

162176
It can also be nice to create a `typealias` for each type of entity you want to work with:
163177
```
164-
typealias Person = Entity<PersonDescription>
178+
typealias Person = Entity<PersonDescription, NoMetadata, NoLinks>
165179
166-
typealias NewPerson = NewEntity<PersonDescription>
180+
typealias NewPerson = NewEntity<PersonDescription, NoMetadata, NoLinks>
167181
```
168182

169183
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`
284298

285299
#### `MetaType`
286300

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:
288302
```
289303
{
290304
"meta": {
@@ -308,9 +322,7 @@ You can always use `NoMetadata` if this JSON API feature is not needed.
308322

309323
#### `LinksType`
310324

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).
314326

315327
#### `IncludeType`
316328

@@ -334,6 +346,18 @@ You can supply any `JSONAPI.Meta` type as the metadata type of the API descripti
334346

335347
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`.
336348

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+
337361
### `RawIdType`
338362

339363
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

Comments
 (0)