-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
99 lines (94 loc) · 2.23 KB
/
encode.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
package rsv
import (
"errors"
"reflect"
)
// MarshalRow marshals an interface into a slice of interface{}
// Will create a row that is as long as the maximum idx tag, with
// nil values for unspecified indices.
// for example, the following struct
// type A struct {
// Min int `idx:"0"`
// Max int `idx:"3"`
// }
// if A.Min = 1, and A.Max = 2, MarshalRow will produce the following row:
// []interface{1, nil, nil, 2}
func MarshalRow(i interface{}) (row []interface{}, err error) {
v := reflect.ValueOf(i)
t := reflect.TypeOf(i)
kind := v.Kind()
if kind == reflect.Ptr {
v = v.Elem()
t = t.Elem()
kind = v.Kind()
}
if kind != reflect.Struct {
panic("i should be a struct or ptr to a struct")
}
m := make(map[int]interface{})
for j := 0; j < v.NumField(); j++ {
fv := v.Field(j)
idx, _, err := getTagInfo(t.Field(j).Tag)
if err != nil && !errors.Is(err, errIdxTagNotSet{}) {
return row, err
}
if errors.Is(err, errIdxTagNotSet{}) && fv.Kind() != reflect.Struct {
continue
}
m, err = get(fv, idx, m)
if err != nil {
return nil, err
}
}
return buildSlice(m), nil
}
func get(v reflect.Value, idx int, m map[int]interface{}) (map[int]interface{}, error) {
switch v.Kind() {
case reflect.String:
m[idx] = v.String()
case reflect.Int, reflect.Int64, reflect.Int32:
m[idx] = v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
m[idx] = v.Uint()
case reflect.Float32, reflect.Float64:
m[idx] = v.Float()
case reflect.Ptr:
if v.CanAddr() {
return get(v.Elem(), idx, m)
}
m[idx] = nil
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
fv := v.Field(i)
idx, _, err := getTagInfo(v.Type().Field(i).Tag)
if err != nil && !errors.Is(err, errIdxTagNotSet{}) {
return nil, err
}
if errors.Is(err, errIdxTagNotSet{}) && fv.Kind() != reflect.Struct {
continue
}
m, err = get(fv, idx, m)
if err != nil {
return m, err
}
}
}
return m, nil
}
func buildSlice(m map[int]interface{}) []interface{} {
size := max(m) + 1
s := make([]interface{}, size)
for k, v := range m {
s[k] = v
}
return s
}
func max(m map[int]interface{}) int {
max := 0
for i := range m {
if max < i {
max = i
}
}
return max
}