-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
144 lines (120 loc) · 2.9 KB
/
value.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
package conf
import (
"encoding"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
type value interface {
Parse(string) error
Set(any)
}
type genericValue[T any] struct{ dst *T }
func (v genericValue[T]) Set(value any) {
*v.dst = value.(T)
}
func (v genericValue[T]) Parse(text string) error {
return parse(reflect.ValueOf(v.dst), text, true)
}
func parse(v reflect.Value, text string, topLevel bool) error {
if unmarshaler, ok := v.Interface().(encoding.TextUnmarshaler); ok {
return unmarshaler.UnmarshalText([]byte(text))
}
if unmarshaler, ok := v.Interface().(encoding.BinaryUnmarshaler); ok {
return unmarshaler.UnmarshalBinary([]byte(text))
}
t := v.Type()
for t.Kind() == reflect.Pointer {
if v.IsNil() {
v.Set(reflect.New(t))
}
t = t.Elem()
v = v.Elem()
}
switch t.Kind() {
case reflect.String:
v.SetString(text)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Kind() == reflect.Int64 && t.PkgPath() == "time" && t.Name() == "Duration" {
d, err := time.ParseDuration(text)
if err != nil {
return err
}
v.SetInt(int64(d))
break
}
val, err := strconv.ParseInt(text, 0, t.Bits())
if err != nil {
return err
}
v.SetInt(val)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
val, err := strconv.ParseUint(text, 0, t.Bits())
if err != nil {
return err
}
v.SetUint(val)
case reflect.Bool:
val, err := strconv.ParseBool(text)
if err != nil {
return err
}
v.SetBool(val)
case reflect.Float32, reflect.Float64:
val, err := strconv.ParseFloat(text, t.Bits())
if err != nil {
return err
}
v.SetFloat(val)
case reflect.Slice:
if !topLevel {
return fmt.Errorf("cannot support deep slices")
}
if t.Elem().Kind() == reflect.Uint8 {
v.Set(reflect.ValueOf([]byte(text)))
return nil
}
if strings.TrimSpace(text) == "" {
v.Set(reflect.MakeSlice(t, 0, 0))
return nil
}
items := strings.Split(text, ",")
slice := reflect.MakeSlice(t, len(items), len(items))
for i, subtext := range items {
if err := parse(slice.Index(i), subtext, false); err != nil {
return err
}
}
v.Set(slice)
case reflect.Map:
if !topLevel {
return fmt.Errorf("cannot support deep maps")
}
text = strings.TrimSpace(text)
if text == "" {
return nil
}
target := reflect.MakeMap(t)
for _, elem := range strings.Split(text, ",") {
key, value, ok := strings.Cut(elem, "=")
if !ok {
continue
}
k := reflect.New(t.Key()).Elem()
if err := parse(k, key, false); err != nil {
return fmt.Errorf("failed to parse key: %s: %w", key, err)
}
v := reflect.New(t.Elem()).Elem()
if err := parse(v, value, false); err != nil {
return fmt.Errorf("failed to parse value at key: %s: %w", key, err)
}
target.SetMapIndex(k, v)
}
v.Set(target)
default:
return fmt.Errorf("destination type not supported: %s", t)
}
return nil
}