-
Notifications
You must be signed in to change notification settings - Fork 1
/
json_undefined_test.go
94 lines (72 loc) · 2.47 KB
/
json_undefined_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
package unusual_generics_test
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/xakep666/unusual_generics"
)
func TestJSONUndefined(t *testing.T) {
var testJSON = []byte(`{"field_a": "some text", "field_b": null}`)
t.Run("With JSONUndefined", func(t *testing.T) {
var result struct {
FieldA unusual_generics.JSONUndefined[string] `json:"field_a"`
FieldB unusual_generics.JSONUndefined[string] `json:"field_b"`
FieldC unusual_generics.JSONUndefined[string] `json:"field_c"`
}
require.NoError(t, json.Unmarshal(testJSON, &result))
assert.True(t, result.FieldA.Defined)
assert.Equal(t, "some text", *result.FieldA.Value)
assert.True(t, result.FieldB.Defined)
assert.Nil(t, result.FieldB.Value)
assert.False(t, result.FieldC.Defined)
})
t.Run("Without JSONUndefined", func(t *testing.T) {
var result struct {
FieldA *string `json:"field_a"`
FieldB *string `json:"field_b"`
FieldC *string `json:"field_c"`
}
require.NoError(t, json.Unmarshal(testJSON, &result))
assert.Equal(t, "some text", *result.FieldA)
assert.Nil(t, result.FieldB)
assert.Nil(t, result.FieldC)
})
}
func ExampleJSONUndefined() {
var testJSON = []byte(`{"field_a": "some text", "field_b": null}`)
var result struct {
FieldA unusual_generics.JSONUndefined[string] `json:"field_a"`
FieldB unusual_generics.JSONUndefined[string] `json:"field_b"`
FieldC unusual_generics.JSONUndefined[string] `json:"field_c"`
}
if err := json.Unmarshal(testJSON, &result); err != nil {
panic(err)
}
fmt.Printf("FieldA: Defined=%t, Value=%s\n", result.FieldA.Defined, *result.FieldA.Value)
fmt.Printf("FieldB: Defined=%t, Value=%v\n", result.FieldB.Defined, result.FieldB.Value)
fmt.Printf("FieldC: Defined=%t, Value=%v\n", result.FieldC.Defined, result.FieldC.Value)
// Output:
// FieldA: Defined=true, Value=some text
// FieldB: Defined=true, Value=<nil>
// FieldC: Defined=false, Value=<nil>
}
func ExampleJSONUndefined_only_pointers() {
var testJSON = []byte(`{"field_a": "some text", "field_b": null}`)
var result struct {
FieldA *string `json:"field_a"`
FieldB *string `json:"field_b"`
FieldC *string `json:"field_c"`
}
if err := json.Unmarshal(testJSON, &result); err != nil {
panic(err)
}
fmt.Printf("FieldA: %s\n", *result.FieldA)
fmt.Printf("FieldB: %v\n", result.FieldB)
fmt.Printf("FieldC: %v\n", result.FieldC)
// Output:
// FieldA: some text
// FieldB: <nil>
// FieldC: <nil>
}