We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hi!
I could not find a way to easily to return a response with top-level custom metadata, i.e.
{ "data": ... "meta": "my data" }
but only found a way to add metadata at the resource level, e.g.
{ "data": [ { "meta": ... } ] }
I had to write the following helper function to achieve it. is there a better way?
import ( "encoding/json" "github.com/manyminds/api2go/jsonapi" ) func MarshalWithMeta(data interface{}, meta map[string]interface{}) ([]byte, error) { document, err := jsonapi.MarshalToStruct(data, nil) if err != nil { return nil, err } document.Meta = meta bytesResponse, err := json.Marshal(document) if err != nil { return nil, err } return bytesResponse, nil }
The text was updated successfully, but these errors were encountered:
I found this interface you can implement in the docs: https://pkg.go.dev/github.com/manyminds/api2go/jsonapi#MarshalMeta
So you can do it like this:
type Book struct { Id string } func (b Book) Meta() jsonapi.Meta { return jsonapi.Meta{ "key": "value" } }
and an instance will marshal into:
{ "data": { "meta": { "key": "value" }, "attributes": { "Id": "1" } } }
Sorry, something went wrong.
No branches or pull requests
Hi!
I could not find a way to easily to return a response with top-level custom metadata, i.e.
but only found a way to add metadata at the resource level, e.g.
I had to write the following helper function to achieve it. is there a better way?
The text was updated successfully, but these errors were encountered: