Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
270 lines (222 loc) · 6.19 KB
/
Copy pathexample_test.go
File metadata and controls
270 lines (222 loc) · 6.19 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package env_test
import (
"fmt"
"log"
"strings"
"time"
"github.com/goloop/env/v2"
)
// Unmarshal reads values from the process environment into a struct. Field
// names are matched by the env tag (or the field name when the tag is empty).
func ExampleUnmarshal() {
env.Clear()
env.Set("HOST", "0.0.0.0")
env.Set("PORT", "8080")
env.Set("ALLOWED_HOSTS", "localhost:127.0.0.1")
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT" def:"80"`
AllowedHosts []string `env:"ALLOWED_HOSTS" sep:":"`
}
var c Config
if err := env.Unmarshal(&c); err != nil {
log.Fatal(err)
}
fmt.Println(c.Host)
fmt.Println(c.Port)
fmt.Println(c.AllowedHosts)
// Output:
// 0.0.0.0
// 8080
// [localhost 127.0.0.1]
}
// WithPrefix reads a namespaced subset of the environment. Levels are joined
// with "_", so WithPrefix("APP") maps the field PORT to APP_PORT. The same
// component struct can be reused for several prefixes.
func ExampleUnmarshal_withPrefix() {
env.Clear()
env.Set("APP_PORT", "8080")
env.Set("DB_PORT", "5432")
type Service struct {
Port int `env:"PORT"`
}
var app, db Service
env.Unmarshal(&app, env.WithPrefix("APP"))
env.Unmarshal(&db, env.WithPrefix("DB"))
fmt.Println(app.Port, db.Port)
// Output: 8080 5432
}
// UnmarshalMap decodes a struct from a map without touching the process
// environment - handy for tests and multi-tenant configuration.
func ExampleUnmarshalMap() {
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
}
data := map[string]string{"HOST": "localhost", "PORT": "9000"}
var c Config
if err := env.UnmarshalMap(data, &c); err != nil {
log.Fatal(err)
}
fmt.Println(c.Host, c.Port)
// Output: localhost 9000
}
// MarshalMap converts a struct into a map without changing the environment.
func ExampleMarshalMap() {
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
AllowedHosts []string `env:"ALLOWED_HOSTS" sep:":"`
}
c := Config{Host: "localhost", Port: 8080, AllowedHosts: []string{"a", "b"}}
m, err := env.MarshalMap(c)
if err != nil {
log.Fatal(err)
}
fmt.Println(m["HOST"])
fmt.Println(m["PORT"])
fmt.Println(m["ALLOWED_HOSTS"])
// Output:
// localhost
// 8080
// a:b
}
// Marshal writes a struct into the process environment.
func ExampleMarshal() {
env.Clear()
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
}
if err := env.Marshal(Config{Host: "localhost", Port: 8080}); err != nil {
log.Fatal(err)
}
fmt.Println(env.Get("HOST"), env.Get("PORT"))
// Output: localhost 8080
}
// Parse reads .env data from any io.Reader into a map. Double-quoted and
// unquoted values expand ${VAR}/$VAR; single quotes are literal.
func ExampleParse() {
env.Clear()
env.Set("USER", "goloop")
r := strings.NewReader(`
# a comment
HOST=localhost
GREETING="hello ${USER}"
LITERAL='hello ${USER}'
`)
m, err := env.Parse(r)
if err != nil {
log.Fatal(err)
}
fmt.Println(m["HOST"])
fmt.Println(m["GREETING"])
fmt.Println(m["LITERAL"])
// Output:
// localhost
// hello goloop
// hello ${USER}
}
// The required flag makes a field mandatory.
func ExampleUnmarshal_required() {
type Config struct {
Token string `env:"TOKEN,required"`
}
var c Config
err := env.UnmarshalMap(map[string]string{}, &c)
fmt.Println(err)
// Output: env: required key is not set: TOKEN
}
// time.Duration values are parsed with time.ParseDuration; time.Time uses the
// RFC3339 layout by default or a per-field layout tag.
func ExampleUnmarshal_time() {
type Config struct {
Timeout time.Duration `env:"TIMEOUT"`
StartAt time.Time `env:"START_AT" layout:"2006-01-02"`
}
data := map[string]string{"TIMEOUT": "1h30m", "START_AT": "2026-06-25"}
var c Config
if err := env.UnmarshalMap(data, &c); err != nil {
log.Fatal(err)
}
fmt.Println(c.Timeout)
fmt.Println(c.StartAt.Format("2006-01-02"))
// Output:
// 1h30m0s
// 2026-06-25
}
// WithParser registers a decoder for a type that does not implement
// encoding.TextUnmarshaler. It applies to the type and to slices, arrays and
// pointers of it (WithEncoder is the encode counterpart).
func ExampleWithParser() {
type Point struct{ X, Y int }
parse := func(s string) (Point, error) {
var p Point
_, err := fmt.Sscanf(s, "%d,%d", &p.X, &p.Y)
return p, err
}
type Config struct {
Origin Point `env:"ORIGIN"`
}
var c Config
if err := env.UnmarshalMap(map[string]string{"ORIGIN": "3,4"}, &c, env.WithParser(parse)); err != nil {
log.Fatal(err)
}
fmt.Println(c.Origin.X, c.Origin.Y)
// Output: 3 4
}
// MarshalString encodes a struct into a string of KEY=value lines (and pairs
// with UnmarshalString).
func ExampleMarshalString() {
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
}
s, err := env.MarshalString(Config{Host: "localhost", Port: 8080})
if err != nil {
log.Fatal(err)
}
fmt.Print(s)
// Output:
// HOST=localhost
// PORT=8080
}
// The Raw variants read and write values verbatim, without ${VAR}/$VAR
// expansion, so any value (here a literal $HOME) round-trips byte-for-byte.
func ExampleMarshalStringRaw() {
type Config struct {
Template string `env:"TEMPLATE"`
}
s, _ := env.MarshalStringRaw(Config{Template: "$HOME/app"})
var c Config
if err := env.UnmarshalStringRaw(s, &c); err != nil {
log.Fatal(err)
}
fmt.Println(c.Template)
// Output: $HOME/app
}
// WithRequiredAll makes every field mandatory, as if each carried ",required".
func ExampleWithRequiredAll() {
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT"`
}
var c Config
err := env.UnmarshalMap(map[string]string{"HOST": "localhost"}, &c, env.WithRequiredAll())
fmt.Println(err)
// Output: env: required key is not set: PORT
}
// Decoding follows encoding/json presence rules: an absent key leaves the field
// untouched (so an in-code default survives), while a def tag fills an absent key.
func ExampleUnmarshal_defaults() {
type Config struct {
Host string `env:"HOST"`
Port int `env:"PORT" def:"8080"`
}
c := Config{Host: "preset"} // HOST is absent below, so it keeps this value
if err := env.UnmarshalMap(map[string]string{}, &c); err != nil {
log.Fatal(err)
}
fmt.Println(c.Host, c.Port)
// Output: preset 8080
}