@@ -7,6 +7,25 @@ import (
7
7
"reflect"
8
8
)
9
9
10
+ // Basic package usage example.
11
+ func Example () {
12
+ type Config struct {
13
+ Foo string `env:"FOO"`
14
+ }
15
+
16
+ os .Setenv ("FOO" , "bar" )
17
+
18
+ // parse:
19
+ var cfg1 Config
20
+ _ = Parse (& cfg1 )
21
+
22
+ // parse with generics:
23
+ cfg2 , _ := ParseAs [Config ]()
24
+
25
+ fmt .Print (cfg1 .Foo , cfg2 .Foo )
26
+ // Output: barbar
27
+ }
28
+
10
29
// Parse the environment into a struct.
11
30
func ExampleParse () {
12
31
type Config struct {
@@ -124,8 +143,8 @@ func ExampleParse_init() {
124
143
// during `Parse`.
125
144
func ExampleParse_setDefaults () {
126
145
type Config struct {
127
- Foo string `env:"FOO "`
128
- Bar string `env:"BAR " envDefault:"bar"`
146
+ Foo string `env:"DEF_FOO "`
147
+ Bar string `env:"DEF_BAR " envDefault:"bar"`
129
148
}
130
149
cfg := Config {
131
150
Foo : "foo" ,
@@ -358,3 +377,35 @@ func ExampleParse_fromFile() {
358
377
fmt .Printf ("%+v" , cfg )
359
378
// Output: {Secret:super secret}
360
379
}
380
+
381
+ // TODO: envSeperator
382
+ //
383
+
384
+ func ExampleParse_errorHandling () {
385
+ type Config struct {
386
+ Username string `env:"USERNAME" envDefault:"admin"`
387
+ Password string `env:"PASSWORD,notEmpty"`
388
+ }
389
+
390
+ var cfg Config
391
+ err := Parse (& cfg )
392
+ if e , ok := err .(* AggregateError ); ok {
393
+ for _ , er := range e .Errors {
394
+ switch v := er .(type ) {
395
+ case ParseError :
396
+ // handle it
397
+ case NotStructPtrError :
398
+ // handle it
399
+ case NoParserError :
400
+ // handle it
401
+ case NoSupportedTagOptionError :
402
+ // handle it
403
+ default :
404
+ fmt .Printf ("Unknown error type %v" , v )
405
+ }
406
+ }
407
+ }
408
+
409
+ fmt .Printf ("%+v" , cfg )
410
+ // Output: {Username:admin Password:}
411
+ }
0 commit comments