Skip to content

Cannot Marshal with top-level Meta #343

@ussu

Description

@ussu

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
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions