-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicvar.go
94 lines (90 loc) · 2.31 KB
/
dynamicvar.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
package cfggo
import (
"encoding"
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
)
type dynamicVar struct {
config *Structure
name string
want reflect.Type
}
func (d *dynamicVar) Set(s string) error {
var value = reflect.New(d.want).Elem()
switch d.want.Kind() {
case reflect.Bool:
if len(s) == 0 {
value.SetBool(false)
} else {
lowerFirst := strings.ToLower(s)
if lowerFirst[0] == '1' || lowerFirst[0] == 't' || lowerFirst[0] == 'y' {
value.SetBool(true)
}
}
case reflect.String:
value.SetString(s)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
if _, err := fmt.Sscan(s, value.Addr().Interface()); err != nil {
return err
}
case reflect.Slice:
split := strings.Split(s, ",")
value.Set(reflect.MakeSlice(d.want, len(split), len(split)))
for i, v := range split {
if _, err := fmt.Sscan(v, value.Index(i).Addr().Interface()); err != nil {
return err
}
}
case reflect.Map:
split := strings.Split(s, ",")
value.Set(reflect.MakeMap(d.want))
for _, v := range split {
kv := strings.Split(v, ":")
if len(kv) != 2 {
return fmt.Errorf("invalid map value %s", v)
}
key := reflect.New(d.want.Key()).Elem()
if _, err := fmt.Sscan(kv[0], key.Addr().Interface()); err != nil {
return err
}
val := reflect.New(d.want.Elem()).Elem()
if _, err := fmt.Sscan(kv[1], val.Addr().Interface()); err != nil {
return err
}
value.SetMapIndex(key, val)
}
default:
unmarshaler, ok := value.Addr().Interface().(encoding.TextUnmarshaler)
if ok {
if err := unmarshaler.UnmarshalText([]byte(s)); err != nil {
return err
}
return d.config.Set(d.name, value.Interface())
}
jsonUnmarshaler, ok := value.Addr().Interface().(json.Unmarshaler)
if ok {
if err := jsonUnmarshaler.UnmarshalJSON([]byte(strconv.Quote(s))); err != nil {
return err
}
}
return fmt.Errorf("unsupported type %s", d.want)
}
if err := d.config.Set(d.name, value.Interface()); err != nil {
return err
}
// fmt.Println("Set", d.name, "to", value.Interface())
return nil
}
func (d *dynamicVar) String() string {
if d.config == nil {
return ""
}
val, ok := d.config.Get(d.name)
if !ok {
return ""
}
return fmt.Sprint(val)
}