-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype_pointer.go
71 lines (58 loc) · 1.05 KB
/
type_pointer.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
package envconf
import (
"reflect"
"github.com/antonmashko/envconf/external"
)
type ptrType struct {
*configField
f field //pointer value
v reflect.Value
hasValue bool
tmp *reflect.Value
}
func newPtrType(v reflect.Value, f *configField) *ptrType {
return &ptrType{
f: emptyField{},
configField: f,
v: v,
tmp: nil,
}
}
func (p *ptrType) externalSource() external.ExternalSource {
return p.f.externalSource()
}
func (p *ptrType) isSet() bool {
return p.hasValue
}
func (p *ptrType) init() error {
tmp := p.v
for tmp.Kind() == reflect.Ptr {
if tmp.IsNil() {
nv := reflect.New(tmp.Type().Elem())
if p.tmp == nil {
p.tmp = &nv
} else {
tmp.Set(nv)
}
tmp = nv
} else {
tmp = tmp.Elem()
}
}
p.f = createFieldFromValue(tmp, p.configField)
return p.f.init()
}
func (p *ptrType) define() error {
err := p.f.define()
if err != nil {
return err
}
if p.f.isSet() && p.tmp != nil {
if !p.v.CanSet() {
return nil
}
p.v.Set(*p.tmp)
p.hasValue = true
}
return nil
}