-
Notifications
You must be signed in to change notification settings - Fork 21
/
util.go
38 lines (33 loc) · 955 Bytes
/
util.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
package mold
import (
"reflect"
)
// extractType gets the actual underlying type of field value.
func (t *Transformer) extractType(current reflect.Value) (reflect.Value, reflect.Kind) {
switch current.Kind() {
case reflect.Ptr:
if current.IsNil() {
return current, reflect.Ptr
}
return t.extractType(current.Elem())
case reflect.Interface:
if current.IsNil() {
return current, reflect.Interface
}
return t.extractType(current.Elem())
default:
if fn := t.interceptors[current.Type()]; fn != nil {
return t.extractType(fn(current))
}
return current, current.Kind()
}
}
// HasValue determines if a reflect.Value is it's default value
func HasValue(field reflect.Value) bool {
switch field.Kind() {
case reflect.Slice, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Chan, reflect.Func:
return !field.IsNil()
default:
return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface()
}
}