forked from elastic/go-ucfg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unpack.go
247 lines (208 loc) · 6.18 KB
/
unpack.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ucfg
import "reflect"
// Unpacker type used by Unpack to allow types to implement custom configuration
// unpacking.
type Unpacker interface {
// Unpack is called if a setting of field has a type implementing Unpacker.
//
// The interface{} value passed to Unpack can be of type: bool, int64, uint64,
// float64, string, []interface{} or map[string]interface{}.
Unpack(interface{}) error
}
// BoolUnpacker interface specializes the Unpacker interface
// by casting values to bool when calling Unpack.
type BoolUnpacker interface {
Unpack(b bool) error
}
// IntUnpacker interface specializes the Unpacker interface
// by casting values to int64 when calling Unpack.
type IntUnpacker interface {
Unpack(i int64) error
}
// UintUnpacker interface specializes the Unpacker interface
// by casting values to uint64 when calling Unpack.
type UintUnpacker interface {
Unpack(u uint64) error
}
// FloatUnpacker interface specializes the Unpacker interface
// by casting values to float64 when calling Unpack.
type FloatUnpacker interface {
Unpack(f float64) error
}
// StringUnpacker interface specializes the Unpacker interface
// by casting values to string when calling Unpack.
type StringUnpacker interface {
Unpack(s string) error
}
// ConfigUnpacker interface specializes the Unpacker interface
// by passing the the *Config object directly instead of
// transforming the *Config object into map[string]interface{}.
type ConfigUnpacker interface {
Unpack(c *Config) error
}
var (
// unpacker interface types
tUnpacker = reflect.TypeOf((*Unpacker)(nil)).Elem()
tBoolUnpacker = reflect.TypeOf((*BoolUnpacker)(nil)).Elem()
tIntUnpacker = reflect.TypeOf((*IntUnpacker)(nil)).Elem()
tUintUnpacker = reflect.TypeOf((*UintUnpacker)(nil)).Elem()
tFloatUnpacker = reflect.TypeOf((*FloatUnpacker)(nil)).Elem()
tStringUnpacker = reflect.TypeOf((*StringUnpacker)(nil)).Elem()
tConfigUnpacker = reflect.TypeOf((*ConfigUnpacker)(nil)).Elem()
tUnpackers = [...]reflect.Type{
tUnpacker,
tBoolUnpacker,
tIntUnpacker,
tUintUnpacker,
tFloatUnpacker,
tStringUnpacker,
tConfigUnpacker,
}
)
// valueIsUnpacker checks if v implements the Unpacker interface.
// If there exists a pointer to v, the pointer to v is also tested.
func valueIsUnpacker(v reflect.Value) (reflect.Value, bool) {
for {
if implementsUnpacker(v.Type()) {
return v, true
}
if !v.CanAddr() {
break
}
v = v.Addr()
}
return reflect.Value{}, false
}
func typeIsUnpacker(t reflect.Type) (reflect.Value, bool) {
if implementsUnpacker(t) {
return reflect.New(t).Elem(), true
}
if implementsUnpacker(reflect.PtrTo(t)) {
return reflect.New(t), true
}
return reflect.Value{}, false
}
func implementsUnpacker(t reflect.Type) bool {
// ucfg.Config or structures that can be casted to ucfg.Config are not
// Unpackers.
if tConfig.ConvertibleTo(chaseTypePointers(t)) {
return false
}
for _, tUnpack := range tUnpackers {
if t.Implements(tUnpack) {
return true
}
}
if t.NumMethod() == 0 {
return false
}
// test if object has 'Unpack' method
method, ok := t.MethodByName("Unpack")
if !ok {
return false
}
// check method input and output parameters to match the ConfigUnpacker interface:
// func (to *T) Unpack(cfg *TConfig) error
// with T being the method receiver (input paramter 0)
// and TConfig being the aliased config type to convert to (input parameter 1)
paramCountCheck := method.Type.NumIn() == 2 && method.Type.NumOut() == 1
if !paramCountCheck {
return false
}
if !method.Type.Out(0).Implements(tError) {
// return variable is not compatible to `error` type
return false
}
// method receiver is known, check config parameters being compatible
tIn := method.Type.In(1)
return tConfig.ConvertibleTo(tIn) || tConfigPtr.ConvertibleTo(tIn)
}
func unpackWith(opts *options, v reflect.Value, with value) Error {
// short circuit nil values
if isNil(with) {
return nil
}
ctx := with.Context()
meta := with.meta()
var err error
value := v.Interface()
switch u := value.(type) {
case Unpacker:
var reified interface{}
if reified, err = with.reify(opts); err == nil {
err = u.Unpack(reified)
}
case BoolUnpacker:
var b bool
if b, err = with.toBool(opts); err == nil {
err = u.Unpack(b)
}
case IntUnpacker:
var n int64
if n, err = with.toInt(opts); err == nil {
err = u.Unpack(n)
}
case UintUnpacker:
var n uint64
if n, err = with.toUint(opts); err == nil {
err = u.Unpack(n)
}
case FloatUnpacker:
var f float64
if f, err = with.toFloat(opts); err == nil {
err = u.Unpack(f)
}
case StringUnpacker:
var s string
if s, err = with.toString(opts); err == nil {
err = u.Unpack(s)
}
case ConfigUnpacker:
var c *Config
if c, err = with.toConfig(opts); err == nil {
err = u.Unpack(c)
}
default:
var c *Config
if c, err = with.toConfig(opts); err == nil {
err = reflectUnpackWithConfig(v, c)
}
}
if err != nil {
return raisePathErr(err, meta, "", ctx.path("."))
}
return nil
}
func reflectUnpackWithConfig(v reflect.Value, c *Config) error {
method, _ := v.Type().MethodByName("Unpack")
tIn := method.Type.In(1)
var rc reflect.Value
switch {
case tConfig.ConvertibleTo(tIn):
rc = reflect.ValueOf(*c)
case tConfigPtr.ConvertibleTo(tIn):
rc = reflect.ValueOf(c)
}
results := method.Func.Call([]reflect.Value{v, rc.Convert(tIn)})
ifc := results[0].Convert(tError).Interface()
if ifc == nil {
return nil
}
return ifc.(error)
}