-
Notifications
You must be signed in to change notification settings - Fork 1
/
dump.go
197 lines (170 loc) · 4.27 KB
/
dump.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
// Copyright (c) 2019 Dean Jackson <deanishe@deanishe.net>
// MIT Licence applies http://opensource.org/licenses/MIT
package env
import (
"encoding"
"errors"
"fmt"
"os"
"reflect"
"strconv"
"strings"
)
// sentinel error returned by toString to indicate that Dump should try
// further methods to convert a value to a string.
var errUnknownType = errors.New("unknown type")
// DumpOption is a configuration option to Dump.
type DumpOption func(d *dumper)
var (
// IgnoreZeroValues excludes zero values from the returned map of variables.
// Non-nil slices are unaffected by the setting: an empty string is returned
// for empty slices regardless.
IgnoreZeroValues DumpOption = func(d *dumper) { d.noZero = true }
)
// VarNameFunc specifies a different function to generate the names of the
// variables returned by Dump.
func VarNameFunc(fun func(string) string) DumpOption {
return func(d *dumper) {
d.nameFunc = fun
}
}
// Dump extracts a struct's fields to a map of variables.
// By default, the names (map keys) of the variables are generated using
// VarName. Pass the VarNameFunc option to generate custom keys.
func Dump(v interface{}, opt ...DumpOption) (map[string]string, error) {
d := &dumper{
nameFunc: VarName,
}
for _, o := range opt {
o(d)
}
return d.dump(v)
}
// Export extracts a struct's fields' values (via Dump) and exports them to the
// environment (via os.Setenv). It accepts the same options as Dump.
func Export(v interface{}, opt ...DumpOption) error {
vars, err := Dump(v, opt...)
if err != nil {
return err
}
for k, v := range vars {
if err := os.Setenv(k, v); err != nil {
return err
}
}
return nil
}
// dumper reads a struct's fields and returns them as a map[string]string.
type dumper struct {
noZero bool
nameFunc func(string) string
}
func (d *dumper) dump(v interface{}) (map[string]string, error) {
vars := map[string]string{}
rv := reflect.ValueOf(v)
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
if rv.Kind() != reflect.Struct {
return nil, ErrNotStruct
}
rvType := rv.Type()
for i := 0; i < rvType.NumField(); i++ {
var (
val = rv.Field(i)
field = rvType.Field(i)
name = field.Name
key = field.Tag.Get("env")
)
if d.noZero && val.IsZero() {
continue
}
// skip unexported fields
if string(name[0]) == strings.ToLower(string(name[0])) || key == "-" {
continue
}
if key == "" {
key = d.nameFunc(name)
}
if val.Kind() == reflect.Ptr && val.IsNil() {
vars[key] = ""
continue
}
if val.Kind() == reflect.Slice {
s, err := dumpSlice(val)
if err != nil {
return nil, err
}
if s == "" && d.noZero {
continue
}
vars[key] = s
continue
}
s, err := toString(val)
if err != nil && err != errUnknownType {
return nil, err
}
if err != errUnknownType {
vars[key] = s
continue
}
if val.Kind() == reflect.Ptr {
val = val.Elem()
}
if val.Kind() == reflect.Struct {
m, err := d.dump(val.Interface())
if err != nil {
return nil, err
}
for k, v := range m {
vars[k] = v
}
continue
}
}
return vars, nil
}
func dumpSlice(rv reflect.Value) (string, error) {
var values []string
for i := 0; i < rv.Len(); i++ {
v := rv.Index(i)
s, err := toString(v)
if err != nil && err != errUnknownType {
return "", err
}
values = append(values, s)
continue
}
return strings.Join(values, ","), nil
}
func toString(rv reflect.Value) (value string, err error) {
if tm, ok := rv.Interface().(encoding.TextMarshaler); ok {
data, err := tm.MarshalText()
if err != nil {
return "", err
}
return string(data), nil
}
if s, ok := rv.Interface().(fmt.Stringer); ok {
return s.String(), nil
}
if rv.Kind() == reflect.Ptr {
rv = rv.Elem()
}
switch rv.Kind() {
case reflect.String:
return rv.String(), nil
case reflect.Bool:
return strconv.FormatBool(rv.Bool()), nil
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(rv.Int(), 10), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.FormatUint(rv.Uint(), 10), nil
case reflect.Float32:
return strconv.FormatFloat(rv.Float(), 'f', -1, 32), nil
case reflect.Float64:
return strconv.FormatFloat(rv.Float(), 'f', -1, 64), nil
}
return "", errUnknownType
}