You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
Repository navigation
This repository was archived by the owner on Sep 4, 2025. It is now read-only.
@gopherfortress we recently added features to easily add "links" when serializing/marshaling. Unfortunately the same cannot be said for deserializing/unmarshaling.
However there is still a way you can parse them out:
funcTestManyPayload_withLinks(t*testing.T) {
firstPageURL:="http://somesite.com/movies?page[limit]=50&page[offset]=50"prevPageURL:="http://somesite.com/movies?page[limit]=50&page[offset]=0"nextPageURL:="http://somesite.com/movies?page[limit]=50&page[offset]=100"lastPageURL:="http://somesite.com/movies?page[limit]=50&page[offset]=500"sample:=map[string]interface{}{
"data": []interface{}{
map[string]interface{}{
"type": "posts",
"id": "1",
"attributes": map[string]interface{}{
"body": "First",
"title": "Post",
},
},
map[string]interface{}{
"type": "posts",
"id": "2",
"attributes": map[string]interface{}{
"body": "Second",
"title": "Post",
},
},
},
"links": map[string]interface{}{
KeyFirstPage: firstPageURL,
KeyPreviousPage: prevPageURL,
KeyNextPage: nextPageURL,
KeyLastPage: lastPageURL,
},
}
data, err:=json.Marshal(sample)
iferr!=nil {
t.Fatal(err)
}
in:=bytes.NewReader(data)
payload:=new(ManyPayload)
iferr=json.NewDecoder(in).Decode(payload); err!=nil {
t.Fatal(err)
}
ifpayload.Links==nil {
t.Fatal("Was expecting a non nil ptr Link field")
}
links:=*payload.Linksfirst, ok:=links[KeyFirstPage]
if!ok {
t.Fatal("Was expecting a non nil ptr Link field")
}
ife, a:=firstPageURL, first; e!=a {
t.Fatalf("Was expecting links.%s to have a value of %s, got %s", KeyFirstPage, e, a)
}
}
My concern is that since links are at the very bottom of the document then it is possible that, assuming a big JSON document, twice the work will be done being done, once for decoding the pagination links and once for decoding the rest.
Unfortunately the same cannot be said for deserializing/unmarshaling.
Assuming there is indeed twice the work being done, any plans to support unmarshaling of pagination links?
@gopherfortress if all you are after is the "links" member there is definitely a more performant solution. The code sample I posted will traverse all of the "data" member which could be fairly lengthy. Take a look at our source code; I think you want to start by altering the OnePayload or ManyPayload to fit your use case of only parsing "links".
As for fully supporting unmarshalling "links" that is a goal - however initial investigation leads me to believe adding such a feature (in a simple way) would mean breaking the existing unmarshal method signatures. We still need to put more thought into this.
Activity
aren55555 commentedon Jan 25, 2017
@gopherfortress we recently added features to easily add
"links"when serializing/marshaling. Unfortunately the same cannot be said for deserializing/unmarshaling.However there is still a way you can parse them out:
gopherfortress commentedon Jan 25, 2017
Thanks for the answer!
Any idea of the amount of work that is being done when doing the
My concern is that since links are at the very bottom of the document then it is possible that, assuming a big JSON document, twice the work will be done being done, once for decoding the pagination links and once for decoding the rest.
Assuming there is indeed twice the work being done, any plans to support unmarshaling of pagination links?
Thank you!
aren55555 commentedon Jan 25, 2017
@gopherfortress if all you are after is the
"links"member there is definitely a more performant solution. The code sample I posted will traverse all of the"data"member which could be fairly lengthy. Take a look at our source code; I think you want to start by altering theOnePayloadorManyPayloadto fit your use case of only parsing"links".As for fully supporting unmarshalling
"links"that is a goal - however initial investigation leads me to believe adding such a feature (in a simple way) would mean breaking the existing unmarshal method signatures. We still need to put more thought into this.