-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
204 lines (196 loc) · 5.38 KB
/
writer.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package cfgenv
import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
"strings"
)
// Write writes the current config
//
// the type of T must be a struct
//
// Use any options (such as PrefixOption, SeparatorOption, NamingOption or multiple CustomSetterOption) to alter
// loading behaviour
func Write(w io.Writer, cfg any, options ...any) error {
o, err := buildOpts(options...)
if err != nil {
return err
}
v := reflect.ValueOf(cfg)
if v.Kind() != reflect.Ptr {
return errors.New("cfg not a pointer")
} else {
v = v.Elem()
if v.Kind() != reflect.Struct {
return errors.New("cfg not a struct")
}
}
return write(w, v, o.prefix.GetPrefix(), true, o)
}
// ExampleOf writes an example of the specified T config
//
// the type of T must be a struct
//
// Use any options (such as PrefixOption, SeparatorOption, NamingOption or multiple CustomSetterOption) to alter
// loading behaviour
func ExampleOf[T any](w io.Writer, options ...any) error {
var cfg T
return Example(w, &cfg, options...)
}
// Example writes an example of the config
//
// the supplied cfg arg must be a pointer to a struct
//
// Use any options (such as PrefixOption, SeparatorOption, NamingOption or multiple CustomSetterOption) to alter
// loading behaviour
func Example(w io.Writer, cfg any, options ...any) error {
o, err := buildOpts(options...)
if err != nil {
return err
}
v := reflect.ValueOf(cfg)
if v.Kind() != reflect.Ptr {
return errors.New("cfg not a pointer")
} else {
v = v.Elem()
if v.Kind() != reflect.Struct {
return errors.New("cfg not a struct")
}
}
return write(w, v, o.prefix.GetPrefix(), false, o)
}
func write(w io.Writer, v reflect.Value, prefix string, actual bool, options *opts) error {
seen := map[string]bool{}
added := map[string]string{}
err := writeValue(w, v, prefix, actual, options, seen, added)
if err != nil {
return err
}
for k, v := range added {
if !seen[k] {
seen[k] = true
if _, err = w.Write([]byte(k + "=" + v + "\n")); err != nil {
return err
}
}
}
return err
}
func writeValue(w io.Writer, v reflect.Value, prefix string, actual bool, options *opts, seen map[string]bool, added map[string]string) error {
t := v.Type()
for f := 0; f < t.NumField(); f++ {
if fld := t.Field(f); fld.IsExported() {
fi, err := getFieldInfo(fld, options)
if err != nil {
return err
}
name := options.naming.BuildName(prefix, options.separator.GetSeparator(), fld, fi.name)
if !seen[name] {
seen[name] = true
if fi.isStruct {
fv := v.Field(f)
if fi.pointer {
if fv.IsNil() && actual {
continue
} else if fv.IsNil() {
fv = reflect.New(fv.Type().Elem()).Elem()
} else {
fv = fv.Elem()
}
}
pfx := addPrefixes(prefix, fi.prefix, options.separator.GetSeparator())
if err = write(w, fv, pfx, actual, options); err != nil {
return err
}
} else if !actual {
if !fi.isPrefixedMap {
if err = writeExampleValue(w, name, v.Field(f), fi); err != nil {
return err
}
}
} else if fi.isPrefixedMap {
m := v.Field(f).Interface().(map[string]string)
for k, v := range m {
added[k] = v
}
} else if err = writeActualValue(w, name, v.Field(f), fi); err != nil {
return err
}
}
}
}
return nil
}
func writeExampleValue(w io.Writer, name string, fv reflect.Value, fi *fieldInfo) error {
eg := "<value>"
if fi.hasDefault {
eg = fi.defaultValue
} else if fi.customSetter == nil {
switch fv.Type().Kind() {
case reflect.String:
eg = "<string>"
case reflect.Bool:
eg = "true|false"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
eg = "0"
case reflect.Float32, reflect.Float64:
eg = "0.0"
case reflect.Slice:
eg = fmt.Sprintf("value%svalue%s...", fi.delimiter, fi.delimiter)
case reflect.Map:
eg = fmt.Sprintf("key%svalue%skey%svalue%s...", fi.separator, fi.delimiter, fi.separator, fi.delimiter)
}
}
_, err := w.Write([]byte(name + "=" + eg + "\n"))
return err
}
func writeActualValue(w io.Writer, name string, fv reflect.Value, fi *fieldInfo) error {
eg := "<value>"
skip := false
if fi.customSetter == nil {
if fi.pointer {
skip = fv.IsNil()
fv = fv.Elem()
}
if !skip {
switch fv.Type().Kind() {
case reflect.String:
eg = fv.String()
case reflect.Bool:
if fv.Bool() {
eg = "true"
} else {
eg = "false"
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
eg = fmt.Sprintf("%d", fv.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
eg = fmt.Sprintf("%d", fv.Uint())
case reflect.Float32:
eg = strconv.FormatFloat(fv.Float(), 'f', -1, 32)
case reflect.Float64:
eg = strconv.FormatFloat(fv.Float(), 'f', -1, 64)
case reflect.Slice:
items := make([]string, 0)
for i := 0; i < fv.Len(); i++ {
items = append(items, fmt.Sprintf("%v", fv.Index(i).Interface()))
}
eg = strings.Join(items, fi.delimiter)
case reflect.Map:
items := make([]string, 0)
for _, mk := range fv.MapKeys() {
mv := fv.MapIndex(mk)
items = append(items, fmt.Sprintf("%v%s%v", mk.Interface(), fi.separator, mv.Interface()))
}
eg = strings.Join(items, fi.delimiter)
}
}
}
var err error
if !skip {
_, err = w.Write([]byte(name + "=" + eg + "\n"))
}
return err
}