-
Notifications
You must be signed in to change notification settings - Fork 211
/
addresslist.go
41 lines (36 loc) · 961 Bytes
/
addresslist.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
package mapstructureutil
import (
"reflect"
"github.com/mitchellh/mapstructure"
"github.com/spacemeshos/go-spacemesh/p2p"
)
// AddressListDecodeFunc mapstructure decode func for p2p.AddressList.
// AddressList can be represented either by a string or by a slice of strings.
func AddressListDecodeFunc() mapstructure.DecodeHookFunc {
return func(f, t reflect.Type, data any) (any, error) {
if t != reflect.TypeOf(p2p.AddressList(nil)) {
return data, nil
}
switch f.Kind() {
case reflect.Slice:
v := reflect.ValueOf(data)
items := make([]string, v.Len())
for n := 0; n < v.Len(); n++ {
item := v.Index(n).Interface()
switch s := item.(type) {
case string:
items[n] = s
default:
return data, nil
}
}
r, err := p2p.AddressListFromStringSlice(items)
return r, err
case reflect.String:
r, err := p2p.AddressListFromString(data.(string))
return r, err
default:
return data, nil
}
}
}