-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinline_test.go
60 lines (54 loc) · 1.53 KB
/
inline_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package claxon
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInline(t *testing.T) {
assert := assert.New(t)
require := require.New(t)
// Serialize ordinary data, if no Claxon data is present, then no impact on
// the data being serialized
foo := SampleProperties{
X: 5,
Y: "hello",
Z: true,
}
data, err := json.Marshal(Embed(foo, Claxon{}))
require.NoError(err)
assert.Equal(`{"x":5,"y":"hello","z":true}`, string(data))
// You can use embedded structs to combine data and metadata and then just
// use the `json` package ot serialize
hyperfoo := Embed(
SampleProperties{X: 5,
Y: "hello",
Z: true,
},
Claxon{
Schema: "https://example.com/schema",
Self: "/me",
Links: []Link{
{
Rel: "item",
Href: "/child1",
Title: "The Favorite",
},
{
Rel: "item",
Href: "/child2",
},
},
})
data, err = json.Marshal(hyperfoo)
require.NoError(err)
assert.Equal(`{"$schema":"https://example.com/schema","$self":"/me","x":5,"y":"hello","z":true,"$links":{"item":[{"href":"/child1","title":"The Favorite"},{"href":"/child2"}]}}`, string(data))
// Finally, you can take ordinary data and annotate it with Claxon data
// using a `Marshal` function with two arguments.
data, err = InlineMarshal(foo, Claxon{
Schema: "https://example.com/schema",
Self: "/me",
})
require.NoError(err)
assert.Equal(`{"$schema":"https://example.com/schema","$self":"/me","x":5,"y":"hello","z":true}`, string(data))
}