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.
It is not understandable from the docs as to how to implement pagination using this library. Is the pagination feature missing from the library? If not, some example or documentation on this will be super useful.
We used to do this by manually calling MarshalOne and MarshaMany, getting back the data object, manually adding links meta, and then json encoding the resulting payload. Bit annoying, but it was the only way to do top level (non object specific) links and meta. The library changed quite a bit, so I'm not sure (yet) how we'll do it now.
I updated our original code today. Essentially we wrote a wrapper for this library with some helper functions. We implemented a MarshalMany function (which I can probably combine with our MarshalOne function, since the library handles slices vs ptrs internally now). Anyways, I think you want something like this:
//MarshalMany marshals many item payloads
func (p *Payload) MarshalMany(writer io.Writer) error {
i, err := convertToSliceInterface(&p.Data)
if err != nil {
return fmt.Errorf(" Internal Server Error")
}
payload, err := jsonapi.Marshal(i)
if err != nil {
return fmt.Errorf(" Internal Server Error")
}
payload.(*jsonapi.ManyPayload).Links = &p.Links.Links
payload.(*jsonapi.ManyPayload).Meta = &p.Meta.Meta
json.NewEncoder(writer).Encode(payload)
return nil
}
There's probably a nicer way to do this, but the problem is that you need a way to pass something more complex than a slice or a ptr to MarshalPayload. Ideally that itself could be an object, that looks a lot like the OnePayload object.
The information that is needed to do pagination (offset, limit) plus meta needs to be made available inside of the Linkable/Meta which means the implementer of LInkable/Meta needs to store it somewhere. For a Person struct it can be in internal fields that are not marshalled:
type Person struct{
ID int64 `jsonapi:"primary,taxon"`
Name string `jsonapi:"attr,name,omitempty"`
metainfo string
}
But you cannot attach these kinds of fields to a []Person.
Activity
svperfecta commentedon Aug 31, 2017
We used to do this by manually calling MarshalOne and MarshaMany, getting back the data object, manually adding links meta, and then json encoding the resulting payload. Bit annoying, but it was the only way to do top level (non object specific) links and meta. The library changed quite a bit, so I'm not sure (yet) how we'll do it now.
svperfecta commentedon Aug 31, 2017
I updated our original code today. Essentially we wrote a wrapper for this library with some helper functions. We implemented a MarshalMany function (which I can probably combine with our MarshalOne function, since the library handles slices vs ptrs internally now). Anyways, I think you want something like this:
There's probably a nicer way to do this, but the problem is that you need a way to pass something more complex than a slice or a ptr to MarshalPayload. Ideally that itself could be an object, that looks a lot like the OnePayload object.
gnewton commentedon Feb 25, 2018
The information that is needed to do pagination (offset, limit) plus meta needs to be made available inside of the Linkable/Meta which means the implementer of LInkable/Meta needs to store it somewhere. For a Person struct it can be in internal fields that are not marshalled:
But you cannot attach these kinds of fields to a []Person.
I would prefer changing the APIs (!!!) to be: