Skip to content

Commit 9d550f3

Browse files
wwwdatasharpner
authored andcommitted
Recursively marshal included structs
The included structs of already included structs were not checked and marshaled as well
1 parent e492c58 commit 9d550f3

File tree

3 files changed

+381
-7
lines changed

3 files changed

+381
-7
lines changed

jsonapi/fixtures_test.go

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,10 @@ func (m MagicID) String() string {
2525
}
2626

2727
type Comment struct {
28-
ID int `json:"-"`
29-
Text string `json:"text"`
28+
ID int `json:"-"`
29+
Text string `json:"text"`
30+
SubComments []Comment `json:"-"`
31+
SubCommentsEmpty bool `json:"-"`
3032
}
3133

3234
func (c Comment) GetID() string {
@@ -44,6 +46,37 @@ func (c *Comment) SetID(stringID string) error {
4446
return nil
4547
}
4648

49+
func (c Comment) GetReferences() []Reference {
50+
return []Reference{
51+
{
52+
Type: "comments",
53+
Name: "comments",
54+
IsNotLoaded: c.SubCommentsEmpty,
55+
},
56+
}
57+
}
58+
59+
func (c Comment) GetReferencedIDs() []ReferenceID {
60+
result := []ReferenceID{}
61+
62+
for _, comment := range c.SubComments {
63+
commentID := ReferenceID{Type: "comments", Name: "comments", ID: comment.GetID()}
64+
result = append(result, commentID)
65+
}
66+
67+
return result
68+
}
69+
70+
func (c Comment) GetReferencedStructs() []MarshalIdentifier {
71+
result := []MarshalIdentifier{}
72+
73+
for _, comment := range c.SubComments {
74+
result = append(result, comment)
75+
}
76+
77+
return result
78+
}
79+
4780
type User struct {
4881
ID int `json:"-"`
4982
Name string `json:"name"`

jsonapi/marshal.go

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ func MarshalToStruct(data interface{}, information ServerInformation) (*Document
136136
}
137137
}
138138

139+
func recursivelyEmbedIncludes(input []MarshalIdentifier) []MarshalIdentifier {
140+
var referencedStructs []MarshalIdentifier
141+
142+
for _, referencedStruct := range input {
143+
included, ok := referencedStruct.(MarshalIncludedRelations)
144+
if ok {
145+
referencedStructs = append(referencedStructs, included.GetReferencedStructs()...)
146+
}
147+
}
148+
149+
if len(referencedStructs) == 0 {
150+
return input
151+
}
152+
153+
childStructs := recursivelyEmbedIncludes(referencedStructs)
154+
referencedStructs = append(referencedStructs, childStructs...)
155+
referencedStructs = append(input, referencedStructs...)
156+
157+
return referencedStructs
158+
}
159+
139160
func marshalSlice(data interface{}, information ServerInformation) (*Document, error) {
140161
result := &Document{}
141162

@@ -161,7 +182,8 @@ func marshalSlice(data interface{}, information ServerInformation) (*Document, e
161182
}
162183
}
163184

164-
includedElements, err := filterDuplicates(referencedStructs, information)
185+
allReferencedStructs := recursivelyEmbedIncludes(referencedStructs)
186+
includedElements, err := filterDuplicates(allReferencedStructs, information)
165187
if err != nil {
166188
return nil, err
167189
}
@@ -395,7 +417,7 @@ func marshalStruct(data MarshalIdentifier, information ServerInformation) (*Docu
395417

396418
included, ok := data.(MarshalIncludedRelations)
397419
if ok {
398-
included, err := filterDuplicates(included.GetReferencedStructs(), information)
420+
included, err := filterDuplicates(recursivelyEmbedIncludes(included.GetReferencedStructs()), information)
399421
if err != nil {
400422
return nil, err
401423
}

0 commit comments

Comments
 (0)