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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
package envelope
import (
"fmt"
"net"
"net/url"
"reflect"
"strconv"
"strings"
"time"
)
// TypeParser defines an interface for custom type parsing of values
// from environment variables
type TypeParser interface {
// Parse converts a string value from an environment variable to a value
// that can be set on a struct field
Parse(value string) (any, error)
}
// TypeParserFunc is a function type that implements TypeParser
type TypeParserFunc func(value string) (any, error)
func (f TypeParserFunc) Parse(value string) (any, error) {
return f(value)
}
var typeParserRegistry = map[reflect.Type]TypeParser{}
// RegisterTypeParser registers a custom type parser for a specific type.
//
// This allows you to extend the LoadFromEnv function to support additional types
// beyond the built-in ones. The parser will be called when LoadFromEnv encounters
// a field of the specified type.
//
// You can also use this function to override the default parsers for built-in types.
// For example, you could register a custom time.Time parser that uses a different
// date format than the default RFC3339.
//
// Example:
//
// // Register a custom parser for UserID type
// type UserID int64
//
// RegisterTypeParser(reflect.TypeOf(UserID(0)), TypeParserFunc(func(value string) (any, error) {
// id, err := strconv.ParseInt(value, 10, 64)
// if err != nil {
// return nil, err
// }
// return UserID(id), nil
// }))
//
// // Override the default time.Time parser to use a custom format
// RegisterTypeParser(reflect.TypeOf(time.Time{}), TypeParserFunc(func(value string) (any, error) {
// return time.Parse("2006-01-02", value)
// }))
func RegisterTypeParser(t reflect.Type, parser TypeParser) {
typeParserRegistry[t] = parser
}
func init() {
// String parser
RegisterTypeParser(reflect.TypeOf(""), TypeParserFunc(func(value string) (any, error) {
return value, nil
}))
// Int parsers
RegisterTypeParser(reflect.TypeOf(int(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseInt(value, 10, 0)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as an integer: %w", value, err)
}
return int(i), nil
}))
RegisterTypeParser(reflect.TypeOf(int8(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseInt(value, 10, 8)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as an int8: %w", value, err)
}
return int8(i), nil
}))
RegisterTypeParser(reflect.TypeOf(int16(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseInt(value, 10, 16)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as an int16: %w", value, err)
}
return int16(i), nil
}))
RegisterTypeParser(reflect.TypeOf(int32(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseInt(value, 10, 32)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as an int32: %w", value, err)
}
return int32(i), nil
}))
RegisterTypeParser(reflect.TypeOf(int64(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as an int64: %w", value, err)
}
return i, nil
}))
// Uint parsers
RegisterTypeParser(reflect.TypeOf(uint(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseUint(value, 10, 0)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a uint: %w", value, err)
}
return uint(i), nil
}))
RegisterTypeParser(reflect.TypeOf(uint8(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseUint(value, 10, 8)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a uint8: %w", value, err)
}
return uint8(i), nil
}))
RegisterTypeParser(reflect.TypeOf(uint16(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseUint(value, 10, 16)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a uint16: %w", value, err)
}
return uint16(i), nil
}))
RegisterTypeParser(reflect.TypeOf(uint32(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseUint(value, 10, 32)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a uint32: %w", value, err)
}
return uint32(i), nil
}))
RegisterTypeParser(reflect.TypeOf(uint64(0)), TypeParserFunc(func(value string) (any, error) {
i, err := strconv.ParseUint(value, 10, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a uint64: %w", value, err)
}
return uint64(i), nil
}))
// Bool parser
RegisterTypeParser(reflect.TypeOf(bool(false)), TypeParserFunc(func(value string) (any, error) {
lower := strings.ToLower(value)
if lower == "on" || lower == "yes" || lower == "y" {
return true, nil
} else if lower == "off" || lower == "no" || lower == "n" {
return false, nil
}
b, err := strconv.ParseBool(value)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a bool: %w", value, err)
}
return b, nil
}))
// Float parsers
RegisterTypeParser(reflect.TypeOf(float32(0)), TypeParserFunc(func(value string) (any, error) {
f, err := strconv.ParseFloat(value, 32)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a float32: %w", value, err)
}
return float32(f), nil
}))
RegisterTypeParser(reflect.TypeOf(float64(0)), TypeParserFunc(func(value string) (any, error) {
f, err := strconv.ParseFloat(value, 64)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as a float64: %w", value, err)
}
return f, nil
}))
// Time.Time parser
RegisterTypeParser(reflect.TypeOf(time.Time{}), TypeParserFunc(func(value string) (any, error) {
t, err := time.Parse(time.RFC3339, value)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as time (RFC3339 format expected): %w", value, err)
}
return t, nil
}))
// time.Duration parser
RegisterTypeParser(reflect.TypeOf(time.Duration(0)), TypeParserFunc(func(value string) (any, error) {
d, err := time.ParseDuration(value)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as duration: %w", value, err)
}
return d, nil
}))
// net.IP parser
RegisterTypeParser(reflect.TypeOf(net.IP{}), TypeParserFunc(func(value string) (any, error) {
ip := net.ParseIP(value)
if ip == nil {
return nil, fmt.Errorf("unable to parse %s as IP address", value)
}
return ip, nil
}))
// net.IPNet parser (CIDR notation)
RegisterTypeParser(reflect.TypeOf(net.IPNet{}), TypeParserFunc(func(value string) (any, error) {
_, ipNet, err := net.ParseCIDR(value)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as CIDR: %w", value, err)
}
return *ipNet, nil
}))
// url.URL parser
RegisterTypeParser(reflect.TypeOf(url.URL{}), TypeParserFunc(func(value string) (any, error) {
u, err := url.Parse(value)
if err != nil {
return nil, fmt.Errorf("unable to parse %s as URL: %w", value, err)
}
return *u, nil
}))
// []string parser (comma separated)
RegisterTypeParser(reflect.TypeOf([]string{}), TypeParserFunc(func(value string) (any, error) {
if value == "" {
return []string{}, nil
}
return strings.Split(value, ","), nil
}))
// []int parser (comma separated)
RegisterTypeParser(reflect.TypeOf([]int{}), TypeParserFunc(func(value string) (any, error) {
if value == "" {
return []int{}, nil
}
parts := strings.Split(value, ",")
result := make([]int, 0, len(parts))
for _, part := range parts {
trimmed := strings.TrimSpace(part)
i, err := strconv.Atoi(trimmed)
if err != nil {
return nil, fmt.Errorf("unable to parse '%s' as int in comma-separated list: %w", trimmed, err)
}
result = append(result, i)
}
return result, nil
}))
}
|