This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalue.go
141 lines (118 loc) · 2.82 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
package env
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(envvar string) (err error) {
return parse(reflect.ValueOf(v.dst), envvar, 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 {
t = t.Elem()
if v.IsNil() {
v.Set(reflect.New(t))
}
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)))
break
}
if strings.TrimSpace(text) == "" {
v.Set(reflect.MakeSlice(t, 0, 0))
}
items := strings.Split(text, ",")
slice := reflect.MakeSlice(t, len(items), len(items))
for i, subtext := range items {
err := parse(slice.Index(i), subtext, false)
if 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)
}
return nil
}