-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnull_string_test.go
125 lines (118 loc) · 2.97 KB
/
null_string_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
package typ
import (
"database/sql/driver"
"encoding/json"
"reflect"
"testing"
)
type NullStringBytes struct{}
var (
nullStringReflectTypes = []reflect.Type{
reflect.TypeOf(&NullString{}),
reflect.TypeOf(&NotNullString{}),
}
)
func init() {
// Test Data
matrixSuite.Register(reflect.TypeOf(&NullString{}), []dataItem{
{reflect.ValueOf(&NullString{}), nil},
})
matrixSuite.Register(reflect.TypeOf(&NotNullString{}), []dataItem{
{reflect.ValueOf(&NotNullString{}), nil},
})
// Converters
// - from &Null*{} to JSONToken
matrixSuite.SetConverters(nullStringReflectTypes, jsonTokenReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
present bool
)
switch tv := from.(type) {
case *NullString:
v, null, present = tv.V(), !tv.Valid(), tv.Present()
case *NotNullString:
v, null, _ = tv.V(), !tv.Valid(), tv.Present()
present = true
}
rv := reflect.ValueOf(v)
b, err := json.Marshal(v)
if null || !present {
b, err = []byte("null"), nil
}
if !rv.IsValid() {
return nil, false
}
jt := JSONToken{from, rv.Type(), b, err}
return jt, err == nil
})
// - from &Null*{} to SQLValueType
matrixSuite.SetConverters(nullStringReflectTypes, sqlValueReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
)
switch tv := from.(type) {
case *NullString:
v, null = tv.V(), !tv.Valid()
case *NotNullString:
v, null = tv.V(), !tv.Valid()
}
if null || v == nil {
return SQLValueType{}, true
}
rv := reflect.ValueOf(v)
cv, valid, _ := matrixSuite.Convert(rv.Interface(), to)
if !valid {
return SQLValueType{}, false
}
return cv, driver.IsValue(cv.(SQLValueType).SQLValue)
})
// For other types
matrixSuite.SetConverters(interfaceReflectTypes, nullStringReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
return nil, false
})
}
func TestNullString(t *testing.T) {
for _, nv := range []interface{}{
&NullString{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testSet(t, nv)
testClone(t, nv)
testNType(t, nv, NString)
}
}
func TestNotNullString(t *testing.T) {
for _, nv := range []interface{}{
&NotNullString{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testSet(t, nv)
testClone(t, nv)
testNType(t, nv, NNString)
}
}
func TestNullStringSlice(t *testing.T) {
ns := []StringAccessor{
NString("t"),
NString("f"),
&NullString{StringCommon{Error: ErrDefaultValue}},
}
sl := StringSlice(ns, false)
if len(sl) != len(ns) || cap(sl) != cap(ns) {
t.Errorf("NullStringSlice(%v, false), slice length not equal", ns)
}
sl = StringSlice(ns, true)
if len(sl) != len(ns)-1 || cap(sl) != cap(ns) {
t.Errorf("NullStringSlice(%v, true), slice length not equal", ns)
}
}