-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutility.go
112 lines (86 loc) · 1.91 KB
/
utility.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
package influxqu
import (
"encoding"
"fmt"
"reflect"
"time"
"github.com/shopspring/decimal"
)
func isValueEmpty(val interface{}) bool {
if val == nil {
return true
}
if reflect.TypeOf(val).Kind() == reflect.Ptr {
return reflect.ValueOf(val).IsNil()
}
if castVal, ok := val.(decimal.Decimal); ok {
return castVal.IsZero()
}
v := reflect.ValueOf(val)
return v.IsValid() && v.IsZero()
}
func getTypeInfo(i interface{}, val reflect.Value) (reflect.Type, reflect.Kind) {
var t reflect.Type
valKind := val.Kind()
if valKind == reflect.Slice {
if reflect.ValueOf(i).Kind() == reflect.Ptr {
t = reflect.TypeOf(i).Elem().Elem()
} else {
t = reflect.TypeOf(i).Elem()
}
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
valKind = t.Kind()
} else {
t = val.Type()
}
return t, valKind
}
func getFieldAsString(val reflect.Value, i int) (string, error) {
f := val.Field(i)
if f.Kind() == reflect.Ptr {
if f.IsNil() {
return "", nil
}
f = f.Elem()
}
if f.Kind() == reflect.String {
return f.String(), nil
}
if s, ok := f.Interface().(fmt.Stringer); ok {
return s.String(), nil
}
if s, ok := f.Interface().(encoding.TextMarshaler); ok {
b, err := s.MarshalText()
if err != nil {
return "", err
}
return string(b), nil
}
switch v := f.Interface().(type) {
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", v), nil
case float32, float64:
return fmt.Sprintf("%f", v), nil
case bool:
return fmt.Sprintf("%t", v), nil
case time.Time:
return v.Format(time.RFC3339Nano), nil
}
return "", &UnSupportedType{}
}
func getFieldAsTime(val reflect.Value, i int) (time.Time, error) {
f := val.Field(i)
if f.Kind() == reflect.Ptr {
if f.IsNil() {
return time.Time{}, &UnSupportedType{}
}
f = f.Elem()
}
t, ok := f.Interface().(time.Time)
if !ok {
return time.Time{}, &UnSupportedType{}
}
return t, nil
}