-
Notifications
You must be signed in to change notification settings - Fork 218
Add support for 'links' via optional interfaces #57
Conversation
|
It's pretty cool that we have not only one, but two Link implementations. (IE: #58 ) Very cool! This is important for us (as is Meta). Specific use cases for links on our end at the moment are pagination related (we'd like to do previous and next links). For meta, we'd like to return some idea of total results found. After looking at both implementations, I think I'd like to vote for this one because it feels flexible, and doesn't add any additional fields to our structs. In general I don't think LINK and META objects really have much to do with the resource objects themselves, they are really more related to the context in which the object is used. This approach keeps structs cleaner. @shwoodard wondering if you guys wanted to make an "official" call on these, we'd like to stick with whatever is blessed by you guys. |
|
One thing to note; this pull doesn't specifically address "top-level" links. The interface approach (e.g. In my opinion, payload, _ := jsonapi.MarshalMany(blogPosts)
payload.Links = map[string]jsonapi.Link{
"self": {
Href: "https://example.com/api/blogs/1/posts",
},
}
// Then JSON encode the payload myself...
json.NewEncoder(w).Encode(payload) |
|
We're pretty sure this is going to be the design we go with on this one. Doing final review and local testing. Should have a conclusion within a day or two. |
|
I've reviewed this PR thoroughly; and as far as I can tell there is only one minor modification required. The JSON API spec allows links objects to be of the form (map[string]string): "links": {
"self": "http://example.com/articles?page[number]=3&page[size]=1",
"first": "http://example.com/articles?page[number]=1&page[size]=1",
"prev": "http://example.com/articles?page[number]=2&page[size]=1",
"next": "http://example.com/articles?page[number]=4&page[size]=1",
"last": "http://example.com/articles?page[number]=13&page[size]=1"
}or "links": {
"self": "/articles/1/relationships/author",
"related": "/articles/1/author"
}However this PR only enables the later ( |
aren55555
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to support string links:
"links": {
"self": "http://example.com/articles?page[number]=3&page[size]=1"
}See my comment for more info.
…ible for different allowed links spec implementations.
|
Hey @aren55555. Yeah that makes sense; both link styles should be possible. I think just a vanilla Question: is there a specific reason the My vote is change 'em to |
|
@keighl the reason for the ptr is that it enables the serialization of an empty links object; "links": {}Without a ptr |
…vide errors when marshaling an invalid links JSON API object.
|
@aren55555 Ah, I see. Thanks for the explanation |
|
So there's good news and bad news. 👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there. 😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that they're okay with their commits being contributed to this project. Please have them confirm that here in the pull request. Note to project maintainer: This is a terminal state, meaning the |
…y user implements the interface it is clear the functions are used for JSON API as opposed to JSON
shwoodard
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* stash-master: Update the go versions Use constants for status codes, HTTP methods and JSON API media types in comments and readme Add support for 'links' via optional interfaces (google#57) Skimata's null relationships + fixes (google#62) Simplify the MarshalMany implementation. For & range rather than for with int inc; etc. (google#59)
|
Cool! Thanks for your feedback and tweaks, @shwoodard @aren55555 |
Here's a an implementation for supporting document links.
To include relationships links, implement the
RelationshipLinkableinterface on a parent struct. It'll be invoked by the marshaler for each defined relationship:To include document links, implement the
Linkableinterface on a struct: