-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache_test.go
163 lines (155 loc) · 5.37 KB
/
cache_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
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
package csv
import (
"net"
"reflect"
"testing"
"time"
testifyassert "github.com/stretchr/testify/assert"
)
type MarshallableTime struct {
time.Time
}
func (m *MarshallableTime) UnmarshalCSV(data string) error {
t, err := time.Parse(time.RFC3339, data)
if err != nil {
return err
}
m.Time = t
return nil
}
func (m MarshallableTime) MarshalCSV() (string, error) {
return m.Time.Format(time.RFC3339), nil
}
type TestStruct struct {
Time MarshallableTime `csv:"marshallableTime,required"`
IP net.IP `csv:"ip,required"`
String string `csv:"string,required"`
Int int `csv:"int,required"`
Int8 int8 `csv:"int8,required"`
Int16 int16 `csv:"int16,required"`
Int32 int32 `csv:"int32,required"`
Int64 int64 `csv:"int64,required"`
Uint uint `csv:"uint,required"`
Uint8 uint8 `csv:"uint8,required"`
Uint16 uint16 `csv:"uint16,required"`
Uint32 uint32 `csv:"uint32,required"`
Uint64 uint64 `csv:"uint64,required"`
Float32 float32 `csv:"float32,required"`
Float64 float64 `csv:"float64,required"`
Bool bool `csv:"bool,required"`
}
type TestStructPtr struct {
Time *MarshallableTime `csv:"marshallableTime,required"`
IP *net.IP `csv:"ip,required"`
String *string `csv:"string,required"`
Int *int `csv:"int,required"`
Int8 *int8 `csv:"int8,required"`
Int16 *int16 `csv:"int16,required"`
Int32 *int32 `csv:"int32,required"`
Int64 *int64 `csv:"int64,required"`
Uint *uint `csv:"uint,required"`
Uint8 *uint8 `csv:"uint8,required"`
Uint16 *uint16 `csv:"uint16,required"`
Uint32 *uint32 `csv:"uint32,required"`
Uint64 *uint64 `csv:"uint64,required"`
Float32 *float32 `csv:"float32,required"`
Float64 *float64 `csv:"float64,required"`
Bool *bool `csv:"bool,required"`
}
func Test_Cache(t *testing.T) {
assert := testifyassert.New(t)
instructions := fieldCache.GetTypeDataFor(reflect.TypeOf(TestStruct{}))
tmp1 := TestStruct{
String: "a string",
Int: 32,
Int8: 12,
Int16: 5235,
Int32: 1561235,
Int64: 16561354,
Uint: 1252,
Uint8: 12,
Uint16: 1256,
Uint32: 123672,
Uint64: 565124,
Float32: 5125.23,
Float64: 5151267.53235,
Bool: true,
Time: MarshallableTime{
Time: time.Now().Truncate(time.Second),
},
IP: net.IPv4(10, 10, 10, 10),
}
var tmp2 TestStruct
var tmp3 TestStructPtr
valOfTmp1 := reflect.ValueOf(tmp1)
valOfTmp2 := reflect.ValueOf(&tmp2)
valOfTmp3 := reflect.ValueOf(&tmp3)
for _, field := range instructions.Fields() {
val, err := field.InstructionData().GetEncoder()(valOfTmp1.Field(field.Idx))
assert.NoError(err)
assert.NotEmpty(val)
newTypedVal, err := field.InstructionData().GetDecoder()(val, false)
assert.NoError(err)
assert.NotEmpty(newTypedVal)
for _, currentValue := range []reflect.Value{valOfTmp2, valOfTmp3} {
targetField := currentValue.Elem().Field(field.Idx)
if targetField.Kind() != reflect.Ptr && reflect.ValueOf(newTypedVal).Kind() == reflect.Ptr {
targetField.Set(reflect.ValueOf(newTypedVal).Elem())
} else if targetField.Kind() == reflect.Ptr && reflect.ValueOf(newTypedVal).Kind() != reflect.Ptr {
if reflect.ValueOf(newTypedVal).CanAddr() {
targetField.Set(reflect.ValueOf(newTypedVal).Addr())
} else {
vOf := reflect.ValueOf(newTypedVal)
newCopy := reflect.New(vOf.Type()).Elem()
newCopy.Set(vOf)
targetField.Set(newCopy.Addr())
}
} else {
targetField.Set(reflect.ValueOf(newTypedVal))
}
}
}
assert.Equal(tmp1, tmp2)
assert.Equal(tmp1.Time, *tmp3.Time)
assert.Equal(tmp1.String, *tmp3.String)
assert.Equal(tmp1.Int, *tmp3.Int)
assert.Equal(tmp1.Int8, *tmp3.Int8)
assert.Equal(tmp1.Int16, *tmp3.Int16)
assert.Equal(tmp1.Int32, *tmp3.Int32)
assert.Equal(tmp1.Int64, *tmp3.Int64)
assert.Equal(tmp1.Uint, *tmp3.Uint)
assert.Equal(tmp1.Uint8, *tmp3.Uint8)
assert.Equal(tmp1.Uint16, *tmp3.Uint16)
assert.Equal(tmp1.Uint32, *tmp3.Uint32)
assert.Equal(tmp1.Uint64, *tmp3.Uint64)
assert.Equal(tmp1.Bool, *tmp3.Bool)
assert.Equal(tmp1.IP, *tmp3.IP)
assert.Equal(tmp1.Float32, *tmp3.Float32)
assert.Equal(tmp1.Float64, *tmp3.Float64)
var tmp4 TestStruct
valOfTmp4 := reflect.ValueOf(&tmp4)
for _, field := range fieldCache.GetTypeDataFor(reflect.TypeOf(tmp3)).Fields() {
val, err := field.InstructionData().GetEncoder()(valOfTmp1.Field(field.Idx))
assert.NoError(err)
assert.NotEmpty(val)
newTypedVal, err := field.InstructionData().GetDecoder()(val, false)
assert.NoError(err)
assert.NotEmpty(newTypedVal)
targetField := valOfTmp4.Elem().Field(field.Idx)
if targetField.Kind() != reflect.Ptr && reflect.ValueOf(newTypedVal).Kind() == reflect.Ptr {
targetField.Set(reflect.ValueOf(newTypedVal).Elem())
} else if targetField.Kind() == reflect.Ptr && reflect.ValueOf(newTypedVal).Kind() != reflect.Ptr {
if reflect.ValueOf(newTypedVal).CanAddr() {
targetField.Set(reflect.ValueOf(newTypedVal).Addr())
} else {
vOf := reflect.ValueOf(newTypedVal)
newCopy := reflect.New(vOf.Type()).Elem()
newCopy.Set(vOf)
targetField.Set(newCopy.Addr())
}
} else {
targetField.Set(reflect.ValueOf(newTypedVal))
}
}
assert.Equal(tmp1, tmp4)
}