Skip to content

Commit eba9c3c

Browse files
arwineapAlex Arwine
andauthored
Adding initial support for the meta field inside of data containers (#341)
Co-authored-by: Alex Arwine <alex_arwine@intuit.com>
1 parent 03231f2 commit eba9c3c

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

jsonapi/fixtures_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jsonapi
22

33
import (
44
"database/sql"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"strconv"
@@ -676,3 +677,12 @@ func (d DeepDedendencies) GetReferencedStructs() []MarshalIdentifier {
676677

677678
return structs
678679
}
680+
681+
type SimplePostWithMetadata struct {
682+
SimplePost
683+
ResourceMetadata map[string]interface{}
684+
}
685+
686+
func (p *SimplePostWithMetadata) SetResourceMeta(r json.RawMessage) error {
687+
return json.Unmarshal(r, &p.ResourceMetadata)
688+
}

jsonapi/unmarshal.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type UnmarshalToManyRelations interface {
2525
SetToManyReferenceIDs(name string, IDs []string) error
2626
}
2727

28+
// The UnmarshalResourceMeta interface must be implemented to unmarshal meta fields inside of data containers
29+
type UnmarshalResourceMeta interface {
30+
MarshalIdentifier
31+
SetResourceMeta(json.RawMessage) error
32+
}
33+
2834
// The EditToManyRelations interface can be optionally implemented to add and
2935
// delete to-many relationships on a already unmarshalled struct. These methods
3036
// are used by our API for the to-many relationship update routes.
@@ -173,6 +179,15 @@ func setDataIntoTarget(data *Data, target interface{}) error {
173179
return err
174180
}
175181

182+
if data.Meta != nil {
183+
if m, ok := target.(UnmarshalResourceMeta); ok {
184+
err = m.SetResourceMeta(data.Meta)
185+
if err != nil {
186+
return err
187+
}
188+
}
189+
}
190+
176191
return setRelationshipIDs(data.Relationships, castedTarget)
177192
}
178193

jsonapi/unmarshal_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,4 +769,40 @@ var _ = Describe("Unmarshal", func() {
769769
Expect(target.Today.Valid).To(Equal(false))
770770
})
771771
})
772+
773+
Context("when unmarshaling resources with meta", func() {
774+
It("unmarshal resource level meta", func() {
775+
expectedPost := SimplePostWithMetadata{
776+
SimplePost: SimplePost{
777+
ID: "1",
778+
Title: "First Post",
779+
Text: "Lipsum",
780+
},
781+
ResourceMetadata: map[string]interface{}{
782+
"user-agent": "Lorem ipsum 1.0",
783+
"post-count": 10.0,
784+
},
785+
}
786+
postJSON := []byte(`{
787+
"data": {
788+
"id": "1",
789+
"type": "simplePostWithMetadatas",
790+
"meta": {
791+
"user-agent": "Lorem ipsum 1.0",
792+
"post-count": 10
793+
},
794+
"attributes": {
795+
"title": "First Post",
796+
"text": "Lipsum"
797+
}
798+
}
799+
}`)
800+
var simplePostWithMetadata SimplePostWithMetadata
801+
err := Unmarshal(postJSON, &simplePostWithMetadata)
802+
Expect(err).To(BeNil())
803+
Expect(expectedPost.SimplePost).To(Equal(simplePostWithMetadata.SimplePost))
804+
Expect(expectedPost.ResourceMetadata["user-agent"]).To(Equal(simplePostWithMetadata.ResourceMetadata["user-agent"]))
805+
Expect(expectedPost.ResourceMetadata["post-count"]).To(Equal(simplePostWithMetadata.ResourceMetadata["post-count"]))
806+
})
807+
})
772808
})

0 commit comments

Comments
 (0)