forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml.go
73 lines (68 loc) · 1.65 KB
/
toml.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
package paladin
import (
"bytes"
"reflect"
"strconv"
"github.com/BurntSushi/toml"
"github.com/pkg/errors"
)
// TOML is toml map.
type TOML = Map
// Set set the map by value.
func (m *TOML) Set(text string) error {
if err := m.UnmarshalText([]byte(text)); err != nil {
return err
}
return nil
}
// UnmarshalText implemented toml.
func (m *TOML) UnmarshalText(text []byte) error {
raws := map[string]interface{}{}
if err := toml.Unmarshal(text, &raws); err != nil {
return err
}
values := map[string]*Value{}
for k, v := range raws {
k = KeyNamed(k)
rv := reflect.ValueOf(v)
switch rv.Kind() {
case reflect.Map:
buf := bytes.NewBuffer(nil)
err := toml.NewEncoder(buf).Encode(v)
// b, err := toml.Marshal(v)
if err != nil {
return err
}
// NOTE: value is map[string]interface{}
values[k] = &Value{val: v, raw: buf.String()}
case reflect.Slice:
raw := map[string]interface{}{
k: v,
}
buf := bytes.NewBuffer(nil)
err := toml.NewEncoder(buf).Encode(raw)
// b, err := toml.Marshal(raw)
if err != nil {
return err
}
// NOTE: value is []interface{}
values[k] = &Value{val: v, raw: buf.String()}
case reflect.Bool:
b := v.(bool)
values[k] = &Value{val: b, raw: strconv.FormatBool(b)}
case reflect.Int64:
i := v.(int64)
values[k] = &Value{val: i, raw: strconv.FormatInt(i, 10)}
case reflect.Float64:
f := v.(float64)
values[k] = &Value{val: f, raw: strconv.FormatFloat(f, 'f', -1, 64)}
case reflect.String:
s := v.(string)
values[k] = &Value{val: s, raw: s}
default:
return errors.Errorf("UnmarshalTOML: unknown kind(%v)", rv.Kind())
}
}
m.Store(values)
return nil
}