@@ -77,7 +77,7 @@ all of your data easily.
77
77
78
78
## Example App
79
79
80
- [ examples/app.go] ( https://github.com/google /jsonapi/blob/master /examples/app.go )
80
+ [ examples/app.go] ( https://github.com/hashicorp /jsonapi/blob/main /examples/app.go )
81
81
82
82
This program demonstrates the implementation of a create, a show,
83
83
and a list [ http.Handler] ( http://golang.org/pkg/net/http#Handler ) . It
@@ -179,6 +179,60 @@ used as the key in the `relationships` hash for the record. The optional
179
179
third argument is ` omitempty ` - if present will prevent non existent to-one and
180
180
to-many from being serialized.
181
181
182
+
183
+ #### ` polyrelation `
184
+
185
+ ```
186
+ `jsonapi:"polyrelation,<key name in relationships hash>,<optional: omitempty>"`
187
+ ```
188
+
189
+ Polymorphic relations can be represented exactly as relations, except that
190
+ an intermediate type is needed within your model struct that provides a choice
191
+ for the actual value to be populated within.
192
+
193
+ Example:
194
+
195
+ ``` go
196
+ type Video struct {
197
+ ID int ` jsonapi:"primary,videos"`
198
+ SourceURL string ` jsonapi:"attr,source-url"`
199
+ CaptionsURL string ` jsonapi:"attr,captions-url"`
200
+ }
201
+
202
+ type Image struct {
203
+ ID int ` jsonapi:"primary,images"`
204
+ SourceURL string ` jsonapi:"attr,src"`
205
+ AltText string ` jsonapi:"attr,alt"`
206
+ }
207
+
208
+ type OneOfMedia struct {
209
+ Video *Video
210
+ Image *Image
211
+ }
212
+
213
+ type Post struct {
214
+ ID int ` jsonapi:"primary,posts"`
215
+ Title string ` jsonapi:"attr,title"`
216
+ Body string ` jsonapi:"attr,body"`
217
+ Gallery []*OneOfMedia ` jsonapi:"polyrelation,gallery"`
218
+ Hero *OneOfMedia ` jsonapi:"polyrelation,hero"`
219
+ }
220
+ ```
221
+
222
+ During decoding, the ` polyrelation ` annotation instructs jsonapi to assign each relationship
223
+ to either ` Video ` or ` Image ` within the value of the associated field, provided that the
224
+ payload contains either a "videos" or "images" type. This field value must be
225
+ a pointer to a special choice type struct (also known as a tagged union, or sum type) containing
226
+ other pointer fields to jsonapi models. The actual field assignment depends on that type having
227
+ a jsonapi "primary" annotation with a type matching the relationship type found in the response.
228
+ All other fields will be remain empty. If no matching types are represented by the choice type,
229
+ all fields will be empty.
230
+
231
+ During encoding, the very first non-nil field will be used to populate the payload. Others
232
+ will be ignored. Therefore, it's critical to set the value of only one field within the choice
233
+ struct. When accepting input values on this type of choice type, it would a good idea to enforce
234
+ and check that the value is set on only one field.
235
+
182
236
#### ` links `
183
237
184
238
* Note: This annotation is an added feature independent of the canonical google/jsonapi package*
@@ -471,13 +525,13 @@ I use git subtrees to manage dependencies rather than `go get` so that
471
525
the src is committed to my repo.
472
526
473
527
```
474
- git subtree add --squash --prefix=src/github.com/google /jsonapi https://github.com/google /jsonapi.git master
528
+ git subtree add --squash --prefix=src/github.com/hashicorp /jsonapi https://github.com/hashicorp /jsonapi.git main
475
529
```
476
530
477
531
To update,
478
532
479
533
```
480
- git subtree pull --squash --prefix=src/github.com/google /jsonapi https://github.com/google /jsonapi.git master
534
+ git subtree pull --squash --prefix=src/github.com/hashicorp /jsonapi https://github.com/hashicorp /jsonapi.git main
481
535
```
482
536
483
537
This assumes that I have my repo structured with a ` src ` dir containing
0 commit comments