-
Notifications
You must be signed in to change notification settings - Fork 1
/
unmarshaler.go
56 lines (44 loc) · 1.18 KB
/
unmarshaler.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
package gofig
import "reflect"
// Unmarshaler is an interface implemented by types that can unmarshal a values themselves.
type Unmarshaler interface {
UnmarshalGoFig(value interface{}) error
}
// unmarshaler checks to see if the given field implements the Unmarshaler interface.
// If it does the Unmarshaler is returned, else nil is returned.
// Lifted from go json stdlib
func unmarshaler(value reflect.Value) Unmarshaler {
if value.Kind() != reflect.Ptr && value.Type().Name() != "" && value.CanAddr() {
value = value.Addr()
}
for {
if value.Kind() == reflect.Interface && !value.IsNil() {
e := value.Elem()
if e.Kind() == reflect.Ptr && !e.IsNil() && e.Elem().Kind() == reflect.Ptr {
value = e
continue
}
}
if value.Kind() != reflect.Ptr {
break
}
if value.CanSet() {
break
}
if value.Elem().Kind() == reflect.Interface && value.Elem().Elem() == value {
value = value.Elem()
break
}
if value.IsNil() {
value.Set(reflect.New(value.Type().Elem()))
}
if value.Type().NumMethod() > 0 && value.CanInterface() {
if u, ok := value.Interface().(Unmarshaler); ok {
return u
}
break
}
value = value.Elem()
}
return nil
}