2015-07-06 12:24:35 -04:00
|
|
|
|
package jsonapi
|
|
|
|
|
|
2017-01-20 20:55:59 -05:00
|
|
|
|
import "fmt"
|
|
|
|
|
|
2017-06-28 20:30:09 -04:00
|
|
|
|
// Payloader is used to encapsulate the One and Many payload types
|
|
|
|
|
type Payloader interface {
|
|
|
|
|
clearIncluded()
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 01:12:42 -04:00
|
|
|
|
// OnePayload is used to represent a generic JSON API payload where a single
|
|
|
|
|
// resource (Node) was included as an {} in the "data" key
|
2015-07-10 12:07:12 -04:00
|
|
|
|
type OnePayload struct {
|
2017-01-20 20:55:59 -05:00
|
|
|
|
Data *Node `json:"data"`
|
|
|
|
|
Included []*Node `json:"included,omitempty"`
|
|
|
|
|
Links *Links `json:"links,omitempty"`
|
2017-02-16 20:40:50 -05:00
|
|
|
|
Meta *Meta `json:"meta,omitempty"`
|
2015-07-06 12:24:35 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 20:30:09 -04:00
|
|
|
|
func (p *OnePayload) clearIncluded() {
|
|
|
|
|
p.Included = []*Node{}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 01:12:42 -04:00
|
|
|
|
// ManyPayload is used to represent a generic JSON API payload where many
|
|
|
|
|
// resources (Nodes) were included in an [] in the "data" key
|
2015-07-10 12:07:12 -04:00
|
|
|
|
type ManyPayload struct {
|
2017-01-20 20:55:59 -05:00
|
|
|
|
Data []*Node `json:"data"`
|
|
|
|
|
Included []*Node `json:"included,omitempty"`
|
|
|
|
|
Links *Links `json:"links,omitempty"`
|
2017-02-16 20:40:50 -05:00
|
|
|
|
Meta *Meta `json:"meta,omitempty"`
|
2015-07-07 12:52:38 -04:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-28 20:30:09 -04:00
|
|
|
|
func (p *ManyPayload) clearIncluded() {
|
|
|
|
|
p.Included = []*Node{}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 01:12:42 -04:00
|
|
|
|
// Node is used to represent a generic JSON API Resource
|
2015-07-10 12:07:12 -04:00
|
|
|
|
type Node struct {
|
2015-07-06 12:24:35 -04:00
|
|
|
|
Type string `json:"type"`
|
2017-05-23 23:17:04 -04:00
|
|
|
|
ID string `json:"id,omitempty"`
|
2016-07-05 21:32:15 -04:00
|
|
|
|
ClientID string `json:"client-id,omitempty"`
|
2015-07-06 12:24:35 -04:00
|
|
|
|
Attributes map[string]interface{} `json:"attributes,omitempty"`
|
2015-07-08 12:34:37 -04:00
|
|
|
|
Relationships map[string]interface{} `json:"relationships,omitempty"`
|
2017-01-20 20:55:59 -05:00
|
|
|
|
Links *Links `json:"links,omitempty"`
|
2017-02-16 20:40:50 -05:00
|
|
|
|
Meta *Meta `json:"meta,omitempty"`
|
2015-07-06 12:24:35 -04:00
|
|
|
|
}
|
2015-07-06 20:04:26 -04:00
|
|
|
|
|
2017-07-27 14:16:00 -04:00
|
|
|
|
func (n *Node) merge(node *Node) {
|
|
|
|
|
if node.Type != "" {
|
|
|
|
|
n.Type = node.Type
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node.ID != "" {
|
|
|
|
|
n.ID = node.ID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node.ClientID != "" {
|
|
|
|
|
n.ClientID = node.ClientID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Attributes == nil && node.Attributes != nil {
|
|
|
|
|
n.Attributes = make(map[string]interface{})
|
|
|
|
|
}
|
|
|
|
|
for k, v := range node.Attributes {
|
|
|
|
|
n.Attributes[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if n.Relationships == nil && node.Relationships != nil {
|
|
|
|
|
n.Relationships = make(map[string]interface{})
|
|
|
|
|
}
|
|
|
|
|
for k, v := range node.Relationships {
|
|
|
|
|
n.Relationships[k] = v
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if node.Links != nil {
|
|
|
|
|
n.Links = node.Links
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 01:12:42 -04:00
|
|
|
|
// RelationshipOneNode is used to represent a generic has one JSON API relation
|
2015-07-10 12:07:12 -04:00
|
|
|
|
type RelationshipOneNode struct {
|
2017-01-20 20:55:59 -05:00
|
|
|
|
Data *Node `json:"data"`
|
|
|
|
|
Links *Links `json:"links,omitempty"`
|
2017-02-16 20:40:50 -05:00
|
|
|
|
Meta *Meta `json:"meta,omitempty"`
|
2015-07-06 20:04:26 -04:00
|
|
|
|
}
|
|
|
|
|
|
2016-09-13 01:12:42 -04:00
|
|
|
|
// RelationshipManyNode is used to represent a generic has many JSON API
|
|
|
|
|
// relation
|
2015-07-10 12:07:12 -04:00
|
|
|
|
type RelationshipManyNode struct {
|
2017-01-20 20:55:59 -05:00
|
|
|
|
Data []*Node `json:"data"`
|
|
|
|
|
Links *Links `json:"links,omitempty"`
|
2017-02-16 20:40:50 -05:00
|
|
|
|
Meta *Meta `json:"meta,omitempty"`
|
2017-01-20 20:55:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Links is used to represent a `links` object.
|
|
|
|
|
// http://jsonapi.org/format/#document-links
|
|
|
|
|
type Links map[string]interface{}
|
|
|
|
|
|
|
|
|
|
func (l *Links) validate() (err error) {
|
|
|
|
|
// Each member of a links object is a “link”. A link MUST be represented as
|
|
|
|
|
// either:
|
|
|
|
|
// - a string containing the link’s URL.
|
|
|
|
|
// - an object (“link object”) which can contain the following members:
|
|
|
|
|
// - href: a string containing the link’s URL.
|
|
|
|
|
// - meta: a meta object containing non-standard meta-information about the
|
|
|
|
|
// link.
|
|
|
|
|
for k, v := range *l {
|
|
|
|
|
_, isString := v.(string)
|
|
|
|
|
_, isLink := v.(Link)
|
|
|
|
|
|
|
|
|
|
if !(isString || isLink) {
|
|
|
|
|
return fmt.Errorf(
|
|
|
|
|
"The %s member of the links object was not a string or link object",
|
|
|
|
|
k,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Link is used to represent a member of the `links` object.
|
|
|
|
|
type Link struct {
|
2017-02-16 23:25:19 -05:00
|
|
|
|
Href string `json:"href"`
|
|
|
|
|
Meta Meta `json:"meta,omitempty"`
|
2017-01-20 20:55:59 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Linkable is used to include document links in response data
|
|
|
|
|
// e.g. {"self": "http://example.com/posts/1"}
|
|
|
|
|
type Linkable interface {
|
|
|
|
|
JSONAPILinks() *Links
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RelationshipLinkable is used to include relationship links in response data
|
|
|
|
|
// e.g. {"related": "http://example.com/posts/1/comments"}
|
|
|
|
|
type RelationshipLinkable interface {
|
|
|
|
|
// JSONAPIRelationshipLinks will be invoked for each relationship with the corresponding relation name (e.g. `comments`)
|
|
|
|
|
JSONAPIRelationshipLinks(relation string) *Links
|
2015-07-06 20:04:26 -04:00
|
|
|
|
}
|
2017-02-16 20:40:50 -05:00
|
|
|
|
|
2017-02-16 23:25:19 -05:00
|
|
|
|
// Meta is used to represent a `meta` object.
|
|
|
|
|
// http://jsonapi.org/format/#document-meta
|
|
|
|
|
type Meta map[string]interface{}
|
|
|
|
|
|
|
|
|
|
// Metable is used to include document meta in response data
|
|
|
|
|
// e.g. {"foo": "bar"}
|
2017-02-16 20:40:50 -05:00
|
|
|
|
type Metable interface {
|
|
|
|
|
JSONAPIMeta() *Meta
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RelationshipMetable is used to include relationship meta in response data
|
|
|
|
|
type RelationshipMetable interface {
|
|
|
|
|
// JSONRelationshipMeta will be invoked for each relationship with the corresponding relation name (e.g. `comments`)
|
|
|
|
|
JSONAPIRelationshipMeta(relation string) *Meta
|
|
|
|
|
}
|
2017-07-27 14:16:00 -04:00
|
|
|
|
|
|
|
|
|
// derefs the arg, and clones the map-type attributes
|
|
|
|
|
// note: maps are reference types, so they need an explicit copy.
|
|
|
|
|
func deepCopyNode(n *Node) *Node {
|
|
|
|
|
if n == nil {
|
|
|
|
|
return n
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copyMap := func(m map[string]interface{}) map[string]interface{} {
|
|
|
|
|
if m == nil {
|
|
|
|
|
return m
|
|
|
|
|
}
|
|
|
|
|
cp := make(map[string]interface{})
|
|
|
|
|
for k, v := range m {
|
|
|
|
|
cp[k] = v
|
|
|
|
|
}
|
|
|
|
|
return cp
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copy := *n
|
|
|
|
|
copy.Attributes = copyMap(copy.Attributes)
|
|
|
|
|
copy.Relationships = copyMap(copy.Relationships)
|
|
|
|
|
if copy.Links != nil {
|
|
|
|
|
tmp := Links(copyMap(map[string]interface{}(*copy.Links)))
|
|
|
|
|
copy.Links = &tmp
|
|
|
|
|
}
|
|
|
|
|
if copy.Meta != nil {
|
|
|
|
|
tmp := Meta(copyMap(map[string]interface{}(*copy.Meta)))
|
|
|
|
|
copy.Meta = &tmp
|
|
|
|
|
}
|
|
|
|
|
return ©
|
|
|
|
|
}
|