forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listmap.go
58 lines (52 loc) · 1.5 KB
/
listmap.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
package listmap
import (
"bytes"
"fmt"
"reflect"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
// DoUnmarshalTOML unmarshals either a list of maps or just a single map into dst.
// The argument dst must be a pointer to a slice.
func DoUnmarshalTOML(dst, src interface{}) error {
dstV := reflect.Indirect(reflect.ValueOf(dst))
if !dstV.CanSet() {
return errors.New("dst must be settable")
}
dstK := dstV.Kind()
if dstK != reflect.Slice {
return errors.New("dst must be a slice")
}
srcV := reflect.ValueOf(src)
srcK := srcV.Kind()
var srvValues []reflect.Value
switch srcK {
case reflect.Slice:
l := srcV.Len()
srvValues = make([]reflect.Value, l)
for i := 0; i < l; i++ {
srvValues[i] = srcV.Index(i)
}
case reflect.Map:
srvValues = []reflect.Value{srcV}
default:
return fmt.Errorf("src must be a slice or map, got %v", srcK)
}
// We want to preserve the TOML decoding behavior exactly,
// so we first re-encode the src data and then decode again,
// only this time directly into the element of the slice.
var buf bytes.Buffer
dstV.Set(reflect.MakeSlice(dstV.Type(), len(srvValues), len(srvValues)))
for i, v := range srvValues {
if err := toml.NewEncoder(&buf).Encode(v.Interface()); err != nil {
return errors.Wrap(err, "failed to reencode toml data")
}
newValue := reflect.New(dstV.Type().Elem())
if _, err := toml.Decode(buf.String(), newValue.Interface()); err != nil {
return err
}
dstV.Index(i).Set(reflect.Indirect(newValue))
buf.Reset()
}
return nil
}