-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathexample_test.go
142 lines (120 loc) · 2.92 KB
/
example_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package refmt_test
import (
"bytes"
"fmt"
"strings"
"github.com/polydawn/refmt"
"github.com/polydawn/refmt/json"
"github.com/polydawn/refmt/obj/atlas"
)
func ExampleNewMarshaller_json() {
var buf bytes.Buffer
encoder := refmt.NewMarshaller(json.EncodeOptions{}, &buf)
err := encoder.Marshal(map[string]interface{}{
"x": "a",
"y": 1,
})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
// Output:
// {"x":"a","y":1}
// <nil>
}
func ExampleNewMarshaller_json_prettyprint() {
var buf bytes.Buffer
encoder := refmt.NewMarshaller(json.EncodeOptions{
Line: []byte{'\n'},
Indent: []byte{'\t'},
}, &buf)
err := encoder.Marshal(map[string]interface{}{
"x": "a",
"y": 1,
})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
// Output:
// {
// "x": "a",
// "y": 1
// }
//
// <nil>
}
func ExampleNewMarshallerAtlased_autogeneratedAtlas() {
type MyType struct {
X string
Y int
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
StructMap().Autogenerate().
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
// Output:
// {"x":"a","y":1}
// <nil>
}
func ExampleNewMarshallerAtlased_customStructMapping() {
type MyType struct {
X string
Y int
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
StructMap().
AddField("X", atlas.StructMapEntry{SerialName: "overrideName"}).
// and no "Y" mapping at all!
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"a", 1})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
// Output:
// {"overrideName":"a"}
// <nil>
}
func ExampleNewMarshallerAtlased_transformFuncs() {
type MyType struct {
X string
Y string
Z string
}
MyType_AtlasEntry := atlas.BuildEntry(MyType{}).
Transform().
TransformMarshal(atlas.MakeMarshalTransformFunc(
func(x MyType) (string, error) {
return string(x.X) + ":" + string(x.Y) + ":" + string(x.Z), nil
})).
TransformUnmarshal(atlas.MakeUnmarshalTransformFunc(
func(x string) (MyType, error) {
ss := strings.Split(x, ":")
if len(ss) != 3 {
return MyType{}, fmt.Errorf("parsing MyType: string must have 3 parts, separated by colon")
}
return MyType{ss[0], ss[1], ss[2]}, nil
})).
Complete()
atl := atlas.MustBuild(
MyType_AtlasEntry,
// this is a vararg... stack more entries here!
)
var buf bytes.Buffer
encoder := refmt.NewMarshallerAtlased(json.EncodeOptions{}, &buf, atl)
err := encoder.Marshal(MyType{"serializes", "as", "string!"})
fmt.Println(buf.String())
fmt.Printf("%v\n", err)
// Output:
// "serializes:as:string!"
// <nil>
}