Skip to content

Commit

Permalink
Merge pull request #268 from ADone/single_struct_included_duplications
Browse files Browse the repository at this point in the history
FIX: replace getIncludedStructs(2) by filterDuplicates(2) method call
  • Loading branch information
wwwdata committed Nov 7, 2016
2 parents f3a667a + f9f798b commit d3dd640
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 18 deletions.
38 changes: 38 additions & 0 deletions jsonapi/fixtures_test.go
Expand Up @@ -519,3 +519,41 @@ func (a Article) GetReferencedIDs() []ReferenceID {

return referenceIDs
}

type DeepDedendencies struct {
ID string `json:"-"`
Relationships []DeepDedendencies `json:"-"`
}

func (d DeepDedendencies) GetID() string {
return d.ID
}

func (DeepDedendencies) GetName() string {
return "deep"
}

func (d DeepDedendencies) GetReferences() []Reference {
return []Reference{{Type: "deep", Name: "deps"}}
}

func (d DeepDedendencies) GetReferencedIDs() []ReferenceID {
references := make([]ReferenceID, 0, len(d.Relationships))

for _, r := range d.Relationships {
references = append(references, ReferenceID{ID: r.ID, Type: "deep", Name: "deps"})
}

return references
}

func (d DeepDedendencies) GetReferencedStructs() []MarshalIdentifier {
var structs []MarshalIdentifier

for _, r := range d.Relationships {
structs = append(structs, r)
structs = append(structs, r.GetReferencedStructs()...)
}

return structs
}
17 changes: 1 addition & 16 deletions jsonapi/marshal.go
Expand Up @@ -318,21 +318,6 @@ func getLinksForServerInformation(relationer MarshalLinkedRelations, name string
return links
}

func getIncludedStructs(included MarshalIncludedRelations, information ServerInformation) ([]Data, error) {
includedStructs := included.GetReferencedStructs()

result := make([]Data, len(includedStructs))

for i, includedStruct := range includedStructs {
err := marshalData(includedStruct, &result[i], information)
if err != nil {
return nil, err
}
}

return result, nil
}

func marshalStruct(data MarshalIdentifier, information ServerInformation) (*Document, error) {
var contentData Data

Expand All @@ -349,7 +334,7 @@ func marshalStruct(data MarshalIdentifier, information ServerInformation) (*Docu

included, ok := data.(MarshalIncludedRelations)
if ok {
included, err := getIncludedStructs(included, information)
included, err := filterDuplicates(included.GetReferencedStructs(), information)
if err != nil {
return nil, err
}
Expand Down
85 changes: 83 additions & 2 deletions jsonapi/marshal_test.go
Expand Up @@ -624,7 +624,7 @@ var _ = Describe("Marshalling", func() {
}`))
})

It("Does not marshall same dependencies multiple times", func() {
It("Does not marshall same dependencies multiple times for slice", func() {
marshalled, err := Marshal([]Question{question3, question2})
Expect(err).To(BeNil())
Expect(marshalled).To(MatchJSON(`{
Expand Down Expand Up @@ -676,6 +676,87 @@ var _ = Describe("Marshalling", func() {
]
}`))
})

It("Does not marshall same dependencies multiple times for single struct", func() {
sharedDependency := DeepDedendencies{ID: "4"}
marshalled, err := Marshal(DeepDedendencies{
ID: "1",
Relationships: []DeepDedendencies{
{
ID: "2",
Relationships: []DeepDedendencies{sharedDependency},
},
{
ID: "3",
Relationships: []DeepDedendencies{sharedDependency},
},
},
})
Expect(err).To(BeNil())
Expect(marshalled).To(MatchJSON(`{
"data": {
"type": "deep",
"id": "1",
"attributes": {},
"relationships": {
"deps": {
"data": [
{
"type": "deep",
"id": "2"
},
{
"type": "deep",
"id": "3"
}
]
}
}
},
"included": [
{
"type": "deep",
"id": "2",
"attributes": {},
"relationships": {
"deps": {
"data": [
{
"type": "deep",
"id": "4"
}
]
}
}
},
{
"type": "deep",
"id": "4",
"attributes": {},
"relationships": {
"deps": {
"data": []
}
}
},
{
"type": "deep",
"id": "3",
"attributes": {},
"relationships": {
"deps": {
"data": [
{
"type": "deep",
"id": "4"
}
]
}
}
}
]
}`))
})
})

Context("Slice fields", func() {
Expand Down Expand Up @@ -819,7 +900,7 @@ var _ = Describe("Marshalling", func() {
})
})

Context("test reduceDuplicates", func() {
Context("test filterDuplicates", func() {
input := []MarshalIdentifier{
User{ID: 314, Name: "User314"},
Comment{ID: 314},
Expand Down

0 comments on commit d3dd640

Please sign in to comment.