Skip to content

Commit e67b9fc

Browse files
committed
Update playground and README for computed attributes.
1 parent a628992 commit e67b9fc

3 files changed

Lines changed: 16 additions & 1 deletion

File tree

JSONAPI.playground/Pages/Usage.xcplaygroundpage/Contents.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ let housesFromData = peopleResponse.body.includes?[House.self]
4444

4545
if case let .data(bodyData) = peopleResponse.body {
4646
print(bodyData)
47+
print("first person's name: \(bodyData.primary.values[0][\.fullName])")
4748
} else {
4849
print("no body data")
4950
}

JSONAPI.playground/Sources/Entities.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ public enum PersonDescription: EntityDescription {
3535
public let name: Attribute<[String]>
3636
public let favoriteColor: Attribute<String>
3737

38+
public var fullName: Attribute<String> {
39+
return name.map { $0.joined(separator: " ") }
40+
}
41+
3842
public init(name: Attribute<[String]>, favoriteColor: Attribute<String>) {
3943
self.name = name
4044
self.favoriteColor = favoriteColor

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ To create an Xcode project for JSONAPI, run
100100
- [x] Roll my own `Result` or find an alternative that doesn't use `Foundation`.
101101
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
102102
- [x] Make it easier to construct `Attributes` and `Relationships` values in test cases (literal expressibility).
103-
- [ ] Make `TransformedAttribute` a Monad (or at least a Functor).
103+
- [x] Make `TransformedAttribute` a Functor.
104104

105105
## Usage
106106

@@ -257,6 +257,16 @@ Note that the first generic parameter of `TransformAttribute` is the type you ex
257257

258258
You can also creator `Validator`s and `ValidatedAttribute`s. A `Validator` is just a `Transformer` that by convention does not perform a transformation. It simply `throws` if an attribute value is invalid.
259259

260+
#### Computed `Attribute`
261+
262+
You can add computed properties to your `EntityDescription.Attributes` struct if you would like to expose attributes that are not explicitly represented by the JSON. These computed properties should still have the type `Attribute` because that way you can take advantage of the quick access provided by `Entity`'s subscript operator. Here's an example of how you might take the `Person[\.name]` attribute from the example above and create a `fullName` computed property.
263+
264+
```
265+
public var fullName: Attribute<String> {
266+
return name.map { $0.joined(separator: " ") }
267+
}
268+
```
269+
260270
### `JSONAPIDocument`
261271

262272
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:

0 commit comments

Comments
 (0)