Skip to content

Commit 186a8a2

Browse files
L-fourwwwdata
authored andcommitted
Allow resources to specify meta using the new MarshalMeta interface (#320)
- Changed the Meta field to use json.RawMessage type to match which Attributes - Added Basic test for New GetMeta Interface
1 parent dc67829 commit 186a8a2

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

jsonapi/data_structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ type Data struct {
120120
Attributes json.RawMessage `json:"attributes"`
121121
Relationships map[string]Relationship `json:"relationships,omitempty"`
122122
Links Links `json:"links,omitempty"`
123+
Meta json.RawMessage `json:"meta,omitempty"`
123124
}
124125

125126
// Relationship contains reference IDs to the related structs

jsonapi/fixtures_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,24 @@ func (n CustomLinksPost) GetCustomLinks(base string) Links {
500500
}
501501
}
502502

503+
type CustomResourceMetaPost struct{}
504+
505+
func (n CustomResourceMetaPost) GetID() string {
506+
return "someID"
507+
}
508+
509+
func (n *CustomResourceMetaPost) SetID(ID string) error {
510+
return nil
511+
}
512+
513+
func (n CustomResourceMetaPost) GetName() string {
514+
return "posts"
515+
}
516+
517+
func (n CustomResourceMetaPost) Meta() Meta {
518+
return Meta{"access_count": 15}
519+
}
520+
503521
type CustomMetaPost struct{}
504522

505523
func (n CustomMetaPost) GetID() string {

jsonapi/marshal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ type MarshalCustomLinks interface {
8080
GetCustomLinks(string) Links
8181
}
8282

83+
// The MarshalMeta interface can be implemented if the struct should
84+
// want any meta.
85+
type MarshalMeta interface {
86+
MarshalIdentifier
87+
Meta() Meta
88+
}
89+
8390
// The MarshalCustomRelationshipMeta interface can be implemented if the struct should
8491
// want a custom meta in a relationship.
8592
type MarshalCustomRelationshipMeta interface {
@@ -254,6 +261,14 @@ func marshalData(element MarshalIdentifier, data *Data, information ServerInform
254261
}
255262
}
256263

264+
if casteMetaTarget, ok := element.(MarshalMeta); ok {
265+
meta := casteMetaTarget.Meta()
266+
data.Meta, err = json.Marshal(meta)
267+
if err != nil {
268+
return err
269+
}
270+
}
271+
257272
if references, ok := element.(MarshalLinkedRelations); ok {
258273
data.Relationships = getStructRelationships(references, information)
259274
}

jsonapi/marshal_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,22 @@ var _ = Describe("Marshalling", func() {
271271
})
272272
})
273273

274+
Context("When marshaling objects with data meta", func() {
275+
It("contains the meta in the marshaled data object", func() {
276+
post := CustomResourceMetaPost{}
277+
i, err := Marshal(post)
278+
Expect(err).To(BeNil())
279+
Expect(i).To(MatchJSON(`{
280+
"data": {
281+
"type": "posts",
282+
"id": "someID",
283+
"attributes": {},
284+
"meta": { "access_count": 15 }
285+
}
286+
}`))
287+
})
288+
})
289+
274290
Context("When marshaling compound objects", func() {
275291
It("marshals nested objects", func() {
276292
comment1 := Comment{ID: 1, Text: "First!", SubCommentsEmpty: true}

0 commit comments

Comments
 (0)