-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathis.go
More file actions
161 lines (125 loc) · 3.11 KB
/
Copy pathis.go
File metadata and controls
161 lines (125 loc) · 3.11 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
package refl
import (
"fmt"
"reflect"
)
// IsSliceOrMap checks if variable is a slice/array/map or a pointer to it.
func IsSliceOrMap(i interface{}) bool {
if i == nil {
return false
}
t := DeepIndirect(reflect.TypeOf(i))
return t.Kind() == reflect.Slice || t.Kind() == reflect.Map || t.Kind() == reflect.Array
}
// IsStruct checks if variable is a struct or a pointer to a struct.
func IsStruct(i interface{}) bool {
if i == nil {
return false
}
t := reflect.TypeOf(i)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
return t.Kind() == reflect.Struct
}
// IsScalar checks if variable is an integer, float, complex, bool, string or a pointer to it.
func IsScalar(i interface{}) bool {
if i == nil {
return false
}
t := reflect.TypeOf(i)
for t.Kind() == reflect.Ptr {
t = t.Elem()
}
switch t.Kind() {
case reflect.String,
reflect.Bool,
reflect.Uint, reflect.Uint64, reflect.Uint32, reflect.Uint16, reflect.Uint8,
reflect.Int, reflect.Int64, reflect.Int32, reflect.Int16, reflect.Int8,
reflect.Float64, reflect.Float32,
reflect.Complex64, reflect.Complex128:
return true
}
return false
}
// FindEmbeddedSliceOrMap checks if variable has a slice/array/map or a pointer to it embedded.
func FindEmbeddedSliceOrMap(i interface{}) reflect.Type {
if i == nil {
return nil
}
t := DeepIndirect(reflect.TypeOf(i))
if t.Kind() != reflect.Struct {
return nil
}
for i := 0; i < t.NumField(); i++ {
f := t.Field(i)
if f.Anonymous {
v := reflect.Zero(f.Type).Interface()
if IsSliceOrMap(v) {
return f.Type
}
if t := FindEmbeddedSliceOrMap(v); t != nil {
return t
}
}
}
return nil
}
// IsZero reports whether v is the zero value for its type.
// It panics if the argument is invalid.
//
// Adapted from go1.13 reflect.IsZero.
func IsZero(v reflect.Value) bool {
return v.IsZero()
}
// As unwraps interface value to find value assignable to target.
func As(v interface{}, target interface{}) bool {
if v == nil {
return false
}
rvv := reflect.ValueOf(v)
rvt := reflect.TypeOf(v)
rtv := reflect.ValueOf(target)
rtt := rtv.Type()
if rtt.Kind() != reflect.Ptr || rtv.IsNil() {
panic("target must be a non-nil pointer")
}
targetType := rtt.Elem()
for {
if rvt.AssignableTo(targetType) {
rtv.Elem().Set(rvv)
return true
}
if rvt.Kind() != reflect.Ptr && rvt.Kind() != reflect.Interface {
break
}
rvv = rvv.Elem()
if rvv.Interface() == nil {
break
}
rvt = rvv.Type()
}
return false
}
// NoEmptyFields checks structure (or a pointer to it) for potentially uninitialized fields.
// Fields with zero values are considered uninitialized.
func NoEmptyFields(l interface{}) error {
v := reflect.ValueOf(l)
for v.Kind() == reflect.Ptr || v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() != reflect.Struct {
return fmt.Errorf("%w, %s received", ErrStructExpected, v.Type().String())
}
var missing []string
for i := 0; i < v.NumField(); i++ {
f := v.Field(i)
if f.IsZero() {
missing = append(missing, v.Type().Field(i).Name)
}
}
if len(missing) > 0 {
return fmt.Errorf("%w: %v", ErrEmptyFields, missing)
}
return nil
}