-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnull_interface_test.go
More file actions
129 lines (123 loc) · 3.24 KB
/
Copy pathnull_interface_test.go
File metadata and controls
129 lines (123 loc) · 3.24 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
package typ
import (
"database/sql/driver"
"encoding/json"
"reflect"
"testing"
)
var (
nullInterfaceReflectTypes = []reflect.Type{
reflect.TypeOf(&NullInterface{}),
reflect.TypeOf(&NotNullInterface{}),
}
)
func init() {
// Test Data
matrixSuite.Register(reflect.TypeOf(&NullInterface{}), []dataItem{
{reflect.ValueOf(&NullInterface{}), nil},
})
matrixSuite.Register(reflect.TypeOf(&NotNullInterface{}), []dataItem{
{reflect.ValueOf(&NotNullInterface{}), nil},
})
// Converters
// - from &Null*{} to JSONToken
matrixSuite.SetConverters(nullInterfaceReflectTypes, jsonTokenReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
present bool
)
switch tv := from.(type) {
case *NullInterface:
v, null, present = tv.V(), !tv.Valid(), tv.Present()
case *NotNullInterface:
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(nullInterfaceReflectTypes, sqlValueReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
var (
v interface{}
null bool
)
switch tv := from.(type) {
case *NullInterface:
v, null = tv.V(), !tv.Valid()
case *NotNullInterface:
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, nullInterfaceReflectTypes, func(from interface{}, to reflect.Type, opts ...interface{}) (interface{}, bool) {
if to == reflect.TypeOf(&NullInterface{}) {
return &NullInterface{InterfaceCommon{P: from}}, true
}
if to == reflect.TypeOf(&NotNullInterface{}) {
return &NotNullInterface{InterfaceCommon{P: from}}, true
}
return nil, false
})
}
func TestNullInterface(t *testing.T) {
for _, nv := range []interface{}{
&NullInterface{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testSet(t, nv)
testClone(t, nv)
testNType(t, nv, NInterface)
}
}
func TestNotNullInterface(t *testing.T) {
for _, nv := range []interface{}{
&NotNullInterface{},
} {
testMarshalJSON(t, nv)
testUnmarshalJSON(t, nv)
testScanSQL(t, nv)
testValueSQL(t, nv)
testTyp(t, nv)
testSet(t, nv)
testClone(t, nv)
testNType(t, nv, NNInterface)
}
}
func TestNullInterfaceSlice(t *testing.T) {
ns := []InterfaceAccessor{
NInterface(0),
NInterface(1),
&NullInterface{InterfaceCommon{Error: ErrDefaultValue}},
}
sl := InterfaceSlice(ns, false)
if len(sl) != len(ns) || cap(sl) != cap(ns) {
t.Errorf("NullInterfaceSlice(%v, false), slice length not equal", ns)
}
sl = InterfaceSlice(ns, true)
if len(sl) != len(ns)-1 || cap(sl) != cap(ns) {
t.Errorf("NullInterfaceSlice(%v, true), slice length not equal", ns)
}
}