-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathnullable_test.go
More file actions
167 lines (143 loc) · 4.94 KB
/
nullable_test.go
File metadata and controls
167 lines (143 loc) · 4.94 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package nullable_test
import (
"encoding/json"
"testing"
"github.com/oapi-codegen/nullable"
"gopkg.in/yaml.v3"
"github.com/stretchr/testify/require"
)
type Obj struct {
Foo nullable.Nullable[string] `json:"foo,omitempty",yaml:"foo,omitempty"` // note "omitempty" is important for fields that are optional
}
func TestNullable(t *testing.T) {
// --- parsing from json and serializing back to JSON
// -- case where there is an actual value
data := `{"foo":"bar"}`
// deserialize from json
myObj := parse(data, t)
require.Equal(t, myObj, Obj{Foo: nullable.Nullable[string]{true: "bar"}})
require.False(t, myObj.Foo.IsNull())
require.True(t, myObj.Foo.IsSpecified())
value, err := myObj.Foo.Get()
require.NoError(t, err)
require.Equal(t, "bar", value)
require.Equal(t, "bar", myObj.Foo.MustGet())
// serialize back to json: leads to the same data
require.Equal(t, data, serialize(myObj, t))
// -- case where no value is specified: parsed from JSON
data = `{}`
// deserialize from json
myObj = parse(data, t)
require.Equal(t, myObj, Obj{Foo: nil})
require.False(t, myObj.Foo.IsNull())
require.False(t, myObj.Foo.IsSpecified())
_, err = myObj.Foo.Get()
require.ErrorContains(t, err, "value is not specified")
// serialize back to json: leads to the same data
require.Equal(t, data, serialize(myObj, t))
// -- case where the specified value is explicitly null
data = `{"foo":null}`
// deserialize from json
myObj = parse(data, t)
require.Equal(t, myObj, Obj{Foo: nullable.Nullable[string]{false: ""}})
require.True(t, myObj.Foo.IsNull())
require.True(t, myObj.Foo.IsSpecified())
_, err = myObj.Foo.Get()
require.ErrorContains(t, err, "value is null")
require.Panics(t, func() { myObj.Foo.MustGet() })
// serialize back to json: leads to the same data
require.Equal(t, data, serialize(myObj, t))
// --- building objects from a Go client
// - case where there is an actual value
myObj = Obj{}
myObj.Foo.Set("bar")
require.Equal(t, `{"foo":"bar"}`, serialize(myObj, t))
// - case where the value should be unspecified
myObj = Obj{}
// do nothing: unspecified by default
require.Equal(t, `{}`, serialize(myObj, t))
// explicitly mark unspecified
myObj.Foo.SetUnspecified()
require.Equal(t, `{}`, serialize(myObj, t))
// - case where the value should be null
myObj = Obj{}
myObj.Foo.SetNull()
require.Equal(t, `{"foo":null}`, serialize(myObj, t))
}
func parse(data string, t *testing.T) Obj {
var myObj Obj
err := json.Unmarshal([]byte(data), &myObj)
require.NoError(t, err)
return myObj
}
func serialize(o Obj, t *testing.T) string {
data, err := json.Marshal(o)
require.NoError(t, err)
return string(data)
}
func TestNullableYAML(t *testing.T) {
// --- parsing from json and serializing back to JSON
// -- case where there is an actual value
data := `foo: bar`
// deserialize from json
myObj := parseYAML(data, t)
require.Equal(t, myObj, Obj{Foo: nullable.Nullable[string]{true: "bar"}})
require.False(t, myObj.Foo.IsNull())
require.True(t, myObj.Foo.IsSpecified())
value, err := myObj.Foo.Get()
require.NoError(t, err)
require.Equal(t, "bar", value)
require.Equal(t, "bar", myObj.Foo.MustGet())
// serialize back to json: leads to the same data
require.Equal(t, data, serializeYAML(myObj, t))
// -- case where no value is specified: parsed from JSON
data = ``
// deserialize from json
myObj = parseYAML(data, t)
require.Equal(t, myObj, Obj{Foo: nil})
require.False(t, myObj.Foo.IsNull())
require.False(t, myObj.Foo.IsSpecified())
_, err = myObj.Foo.Get()
require.ErrorContains(t, err, "value is not specified")
// serialize back to json: leads to the same data
require.Equal(t, data, serializeYAML(myObj, t))
// -- case where the specified value is explicitly null
data = `foo:null`
// deserialize from json
myObj = parseYAML(data, t)
require.Equal(t, myObj, Obj{Foo: nullable.Nullable[string]{false: ""}})
require.True(t, myObj.Foo.IsNull())
require.True(t, myObj.Foo.IsSpecified())
_, err = myObj.Foo.Get()
require.ErrorContains(t, err, "value is null")
require.Panics(t, func() { myObj.Foo.MustGet() })
// serialize back to json: leads to the same data
require.Equal(t, data, serializeYAML(myObj, t))
// --- building objects from a Go client
// - case where there is an actual value
myObj = Obj{}
myObj.Foo.Set("bar")
require.Equal(t, `foo:"bar"`, serialize(myObj, t))
// - case where the value should be unspecified
myObj = Obj{}
// do nothing: unspecified by default
require.Equal(t, ``, serializeYAML(myObj, t))
// explicitly mark unspecified
myObj.Foo.SetUnspecified()
require.Equal(t, ``, serializeYAML(myObj, t))
// - case where the value should be null
myObj = Obj{}
myObj.Foo.SetNull()
require.Equal(t, `foo:null`, serialize(myObj, t))
}
func parseYAML(data string, t *testing.T) Obj {
var myObj Obj
err := yaml.Unmarshal([]byte(data), &myObj)
require.NoError(t, err)
return myObj
}
func serializeYAML(o Obj, t *testing.T) string {
data, err := yaml.Marshal(o)
require.NoError(t, err)
return string(data)
}