-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traverse.go
121 lines (100 loc) · 2.69 KB
/
traverse.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
113
114
115
116
117
118
119
120
121
// Copyright (c) 2023, Roel Schut. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package env
import (
"reflect"
"github.com/go-pogo/env/envtag"
"github.com/go-pogo/rawconv"
)
func init() {
unmarshaler.Register(
reflect.TypeOf((*Unmarshaler)(nil)).Elem(),
func(val rawconv.Value, dest any) error {
return dest.(Unmarshaler).UnmarshalEnv(val.Bytes())
},
)
marshaler.Register(
reflect.TypeOf((*Marshaler)(nil)).Elem(),
func(v any) (string, error) {
b, err := v.(Marshaler).MarshalEnv()
if err != nil {
return "", err
}
return string(b), err
},
)
}
func typeKnownByUnmarshaler(typ reflect.Type) bool {
return unmarshaler.Func(typ) != nil
}
func typeKnownByMarshaler(typ reflect.Type) bool {
return marshaler.Func(typ) != nil
}
type TagOptions = envtag.Options
type traverser struct {
TagOptions
isTypeKnown func(reflect.Type) bool
handleField func(reflect.Value, envtag.Tag) error
}
func (t *traverser) start(pv reflect.Value) error {
return t.traverse(pv, "", false)
}
const panicPtr = "env: ptr values should always be resolved; this is a bug!"
func (t *traverser) traverse(pv reflect.Value, prefix string, include bool) error {
// todo: dit moet anders, in het geval van encode zou pv.Interface() wss. prima nil kunnen zijn
pv = indirect(pv)
pt := pv.Type()
for i, l := 0, pv.NumField(); i < l; i++ {
field, rv := pt.Field(i), pv.Field(i)
kind := underlyingKind(field.Type)
if kind == reflect.Invalid || kind == reflect.Uintptr || kind == reflect.Chan || kind == reflect.Func || kind == reflect.UnsafePointer {
// unsupported types
continue
} else if kind == reflect.Ptr {
panic(panicPtr)
}
opts := t.TagOptions
opts.StrictTags = opts.StrictTags && !include
tag, _ := envtag.ParseStructField(opts, field, prefix)
if tag.ShouldIgnore() {
continue
}
if kind != reflect.Struct || t.isTypeKnown(rv.Type()) {
if err := t.handleField(rv, tag); err != nil {
return err
}
continue
}
var p string
if tag.Inline || field.Anonymous {
p = prefix
} else {
p = tag.Name
}
if err := t.traverse(rv, p, include || tag.Include); err != nil {
return err
} else {
// no error, continue to next field
continue
}
}
return nil
}
// underlyingKind resolves the underlying reflect.Kind of typ.
func underlyingKind(typ reflect.Type) reflect.Kind {
if k := typ.Kind(); k != reflect.Ptr {
return k
}
return underlyingKind(typ.Elem())
}
func indirect(v reflect.Value) reflect.Value {
for v.Kind() == reflect.Ptr {
if v.IsNil() {
// create a pointer to the type v points to
v.Set(reflect.New(v.Type().Elem()))
}
v = v.Elem()
}
return v
}