Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions jsonapi/data_structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type Data struct {
Attributes json.RawMessage `json:"attributes"`
Relationships map[string]Relationship `json:"relationships,omitempty"`
Links Links `json:"links,omitempty"`
Meta json.RawMessage `json:"meta,omitempty"`
}

// Relationship contains reference IDs to the related structs
Expand Down
18 changes: 18 additions & 0 deletions jsonapi/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,24 @@ func (n CustomLinksPost) GetCustomLinks(base string) Links {
}
}

type CustomResourceMetaPost struct{}

func (n CustomResourceMetaPost) GetID() string {
return "someID"
}

func (n *CustomResourceMetaPost) SetID(ID string) error {
return nil
}

func (n CustomResourceMetaPost) GetName() string {
return "posts"
}

func (n CustomResourceMetaPost) Meta() Meta {
return Meta{"access_count": 15}
}

type CustomMetaPost struct{}

func (n CustomMetaPost) GetID() string {
Expand Down
15 changes: 15 additions & 0 deletions jsonapi/marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ type MarshalCustomLinks interface {
GetCustomLinks(string) Links
}

// The MarshalMeta interface can be implemented if the struct should
// want any meta.
type MarshalMeta interface {
MarshalIdentifier
Meta() Meta
}

// The MarshalCustomRelationshipMeta interface can be implemented if the struct should
// want a custom meta in a relationship.
type MarshalCustomRelationshipMeta interface {
Expand Down Expand Up @@ -254,6 +261,14 @@ func marshalData(element MarshalIdentifier, data *Data, information ServerInform
}
}

if casteMetaTarget, ok := element.(MarshalMeta); ok {
meta := casteMetaTarget.Meta()
data.Meta, err = json.Marshal(meta)
if err != nil {
return err
}
}

if references, ok := element.(MarshalLinkedRelations); ok {
data.Relationships = getStructRelationships(references, information)
}
Expand Down
16 changes: 16 additions & 0 deletions jsonapi/marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,22 @@ var _ = Describe("Marshalling", func() {
})
})

Context("When marshaling objects with data meta", func() {
It("contains the meta in the marshaled data object", func() {
post := CustomResourceMetaPost{}
i, err := Marshal(post)
Expect(err).To(BeNil())
Expect(i).To(MatchJSON(`{
"data": {
"type": "posts",
"id": "someID",
"attributes": {},
"meta": { "access_count": 15 }
}
}`))
})
})

Context("When marshaling compound objects", func() {
It("marshals nested objects", func() {
comment1 := Comment{ID: 1, Text: "First!", SubCommentsEmpty: true}
Expand Down