Skip to content

Commit

Permalink
Consider links when marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
mfcochauxlaberge authored Oct 8, 2022
1 parent 8543be9 commit 9808dd5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
14 changes: 12 additions & 2 deletions document.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,12 +159,22 @@ func MarshalDocument(doc *Document, url *URL) ([]byte, error) {
plMap["meta"] = doc.Meta
}

links := doc.Links

if url != nil {
plMap["links"] = map[string]string{
"self": doc.PrePath + url.String(),
if links == nil {
links = map[string]Link{}
}

links["self"] = Link{
HRef: doc.PrePath + url.String(),
}
}

if links != nil {
plMap["links"] = links
}

plMap["jsonapi"] = map[string]string{"version": "1.0"}

return json.Marshal(plMap)
Expand Down
15 changes: 15 additions & 0 deletions document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,21 @@ func TestMarshalDocument(t *testing.T) {
return []Error{err1, err2}
}(),
},
}, {
name: "links",
doc: &Document{
Links: map[string]Link{
"self": {HRef: "http://example.com"},
"some_link": {HRef: "http://example.org"},
"other_link": {
HRef: "http://example.com/other/path",
Meta: map[string]interface{}{
"field1": "value1",
"field2": 123,
},
},
},
},
},
}

Expand Down
17 changes: 17 additions & 0 deletions testdata/goldenfiles/marshaling/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"data": null,
"jsonapi": {
"version": "1.0"
},
"links": {
"other_link": {
"href": "http://example.com/other/path",
"meta": {
"field1": "value1",
"field2": 123
}
},
"self": "/fake/path?fields%5Bmocktype%",
"some_link": "http://example.org"
}
}

0 comments on commit 9808dd5

Please sign in to comment.