-
Notifications
You must be signed in to change notification settings - Fork 2
/
providers_test.go
126 lines (111 loc) · 3.2 KB
/
providers_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
package valix
import (
"encoding/json"
"github.com/stretchr/testify/require"
"reflect"
"strings"
"testing"
)
func TestDecoderProvider(t *testing.T) {
defer func() {
DefaultDecoderProvider = &defaultDecoderProvider{}
}()
jStr := `{
"foo": 1
}`
d := getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), nil)
obj := map[string]interface{}{}
err := d.Decode(&obj)
require.NoError(t, err)
require.Equal(t, float64(1), obj["foo"])
d = getDefaultDecoderProvider().NewDecoder(strings.NewReader(jStr), true)
obj = map[string]interface{}{}
err = d.Decode(&obj)
require.NoError(t, err)
jn, ok := obj["foo"].(json.Number)
require.True(t, ok)
require.Equal(t, "1", jn.String())
v := &Validator{
UseNumber: false,
}
d = getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), v)
obj = map[string]interface{}{}
err = d.Decode(&obj)
require.NoError(t, err)
require.Equal(t, float64(1), obj["foo"])
v.UseNumber = true
d = getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), v)
obj = map[string]interface{}{}
err = d.Decode(&obj)
require.NoError(t, err)
jn, ok = obj["foo"].(json.Number)
require.True(t, ok)
require.Equal(t, "1", jn.String())
v.IgnoreUnknownProperties = false
d = getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), v)
type unknownPtyTest struct {
Bar string
}
ts := &unknownPtyTest{}
err = d.Decode(ts)
require.Error(t, err)
require.Equal(t, "json: unknown field \"foo\"", err.Error())
v.IgnoreUnknownProperties = true
d = getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), v)
ts = &unknownPtyTest{}
err = d.Decode(ts)
require.NoError(t, err)
// and nil replacement protection...
DefaultDecoderProvider = nil
d = getDefaultDecoderProvider().NewDecoderFor(strings.NewReader(jStr), v)
ts = &unknownPtyTest{}
err = d.Decode(ts)
require.NoError(t, err)
}
func TestDefaultPropertyNameProvider(t *testing.T) {
defer func() {
DefaultPropertyNameProvider = &defaultPropertyNameProvider{}
}()
type namesStruct struct {
Foo string `json:"foo,omitempty"`
}
ns := namesStruct{}
ty := reflect.TypeOf(ns)
fld := ty.Field(0)
name, ok := getDefaultPropertyNameProvider().NameFor(fld)
require.True(t, ok)
require.Equal(t, "foo", name)
type namesStruct2 struct {
Foo string
}
ns2 := namesStruct2{}
ty = reflect.TypeOf(ns2)
fld = ty.Field(0)
name, ok = getDefaultPropertyNameProvider().NameFor(fld)
require.True(t, ok)
require.Equal(t, "Foo", name)
// try replacement...
DefaultPropertyNameProvider = &testAutoLowerFirstLetter{}
name, ok = getDefaultPropertyNameProvider().NameFor(fld)
require.True(t, ok)
require.Equal(t, "foo", name)
// and nil replacement protection...
DefaultPropertyNameProvider = nil
name, ok = getDefaultPropertyNameProvider().NameFor(fld)
require.True(t, ok)
require.Equal(t, "Foo", name)
}
type testAutoLowerFirstLetter struct{}
func (dpnp *testAutoLowerFirstLetter) NameFor(field reflect.StructField) (name string, ok bool) {
result := field.Name
if tag, ok := field.Tag.Lookup(tagNameJson); ok {
if cAt := strings.Index(tag, ","); cAt != -1 {
result = tag[0:cAt]
} else {
result = tag
}
} else {
result = strings.ToLower(result[:1]) + result[1:]
}
return result, true
}