-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_mapper_test.go
More file actions
165 lines (146 loc) · 3.7 KB
/
Copy pathstruct_mapper_test.go
File metadata and controls
165 lines (146 loc) · 3.7 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
package dbx
import (
"reflect"
"testing"
"time"
)
func TestBuildStructIndex(t *testing.T) {
type sample struct {
ID int64
UserName string
Custom string `db:"custom_name"`
Skip string `db:"-"`
unexported string
}
idx := buildStructIndex(reflect.TypeOf(sample{}))
if _, ok := idx["id"]; !ok {
t.Fatal("expected id key")
}
if _, ok := idx["user_name"]; !ok {
t.Fatal("expected snake_case key")
}
if _, ok := idx["username"]; !ok {
t.Fatal("expected exact-name lowercase key")
}
if _, ok := idx["custom_name"]; !ok {
t.Fatal("expected tagged key")
}
if _, ok := idx["skip"]; ok {
t.Fatal("did not expect skipped key")
}
if _, ok := idx["unexported"]; ok {
t.Fatal("did not expect unexported field key")
}
}
func TestAssignValueAndConverters(t *testing.T) {
t.Run("nil sets zero", func(t *testing.T) {
v := "x"
rv := reflect.ValueOf(&v).Elem()
assignValue(rv, nil)
if v != "" {
t.Fatalf("expected zero string, got %q", v)
}
})
t.Run("byte slice to string", func(t *testing.T) {
var v string
assignValue(reflect.ValueOf(&v).Elem(), []byte("abc"))
if v != "abc" {
t.Fatalf("unexpected value: %q", v)
}
})
t.Run("bool from string", func(t *testing.T) {
var v bool
assignValue(reflect.ValueOf(&v).Elem(), "true")
if !v {
t.Fatal("expected true")
}
})
t.Run("bool from int", func(t *testing.T) {
var v bool
assignValue(reflect.ValueOf(&v).Elem(), int64(1))
if !v {
t.Fatal("expected true from 1")
}
assignValue(reflect.ValueOf(&v).Elem(), int64(0))
if v {
t.Fatal("expected false from 0")
}
})
t.Run("bool from bytes", func(t *testing.T) {
var v bool
assignValue(reflect.ValueOf(&v).Elem(), []byte("1"))
if !v {
t.Fatal("expected true from []byte(\"1\")")
}
assignValue(reflect.ValueOf(&v).Elem(), []byte("0"))
if v {
t.Fatal("expected false from []byte(\"0\")")
}
})
t.Run("duration from string", func(t *testing.T) {
var d time.Duration
assignValue(reflect.ValueOf(&d).Elem(), "150ms")
if d != 150*time.Millisecond {
t.Fatalf("unexpected duration: %v", d)
}
})
t.Run("int uint float from string", func(t *testing.T) {
var i int64
assignValue(reflect.ValueOf(&i).Elem(), "42")
if i != 42 {
t.Fatalf("unexpected int: %d", i)
}
var u uint64
assignValue(reflect.ValueOf(&u).Elem(), "7")
if u != 7 {
t.Fatalf("unexpected uint: %d", u)
}
var f float64
assignValue(reflect.ValueOf(&f).Elem(), "3.5")
if f != 3.5 {
t.Fatalf("unexpected float: %f", f)
}
})
t.Run("pointer recursion", func(t *testing.T) {
var p *int64
assignValue(reflect.ValueOf(&p).Elem(), "11")
if p == nil || *p != 11 {
t.Fatalf("unexpected pointer value: %#v", p)
}
})
t.Run("assignable and convertible", func(t *testing.T) {
var s string
assignValue(reflect.ValueOf(&s).Elem(), "ok")
if s != "ok" {
t.Fatalf("unexpected assignable result: %q", s)
}
var i int64
assignValue(reflect.ValueOf(&i).Elem(), int32(9))
if i != 9 {
t.Fatalf("unexpected convertible result: %d", i)
}
})
t.Run("non settable does not panic", func(t *testing.T) {
assignValue(reflect.ValueOf(1), 2)
})
}
func TestConverters(t *testing.T) {
if v, ok := toInt64(" 17 "); !ok || v != 17 {
t.Fatalf("toInt64 string failed: %v %v", v, ok)
}
if _, ok := toInt64(struct{}{}); ok {
t.Fatal("toInt64 should fail for struct")
}
if v, ok := toUint64(float32(8)); !ok || v != 8 {
t.Fatalf("toUint64 float failed: %v %v", v, ok)
}
if _, ok := toUint64("bad"); ok {
t.Fatal("toUint64 should fail for bad string")
}
if v, ok := toFloat64(uint16(3)); !ok || v != 3 {
t.Fatalf("toFloat64 uint failed: %v %v", v, ok)
}
if _, ok := toFloat64("nope"); ok {
t.Fatal("toFloat64 should fail for bad string")
}
}