MCPcopy Create free account
hub / github.com/DataDog/jsonapi / ExampleMarshal_meta

Function ExampleMarshal_meta

marshal_example_test.go:46–66  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

44}
45
46func ExampleMarshal_meta() {
47 type ArticleMeta struct {
48 Views int `json:"views"`
49 }
50 type Article struct {
51 ID string `jsonapi:"primary,articles"`
52 Title string `jsonapi:"attribute" json:"title"`
53 Meta *ArticleMeta `jsonapi:"meta"`
54 }
55
56 a := Article{ID: "1", Title: "Hello World", Meta: &ArticleMeta{Views: 10}}
57 m := map[string]any{"foo": "bar"}
58
59 b, err := jsonapi.Marshal(&a, jsonapi.MarshalMeta(m))
60 if err != nil {
61 panic(err)
62 }
63
64 fmt.Printf("%s", string(b))
65 // Output: {"data":{"id":"1","type":"articles","attributes":{"title":"Hello World"},"meta":{"views":10}},"meta":{"foo":"bar"}}
66}

Callers

nothing calls this directly

Calls 2

MarshalFunction · 0.92
MarshalMetaFunction · 0.92

Tested by

no test coverage detected