Skip to content

Commit 1282042

Browse files
committedAug 7, 2024··
docs: more examples
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
1 parent 5d7eb0c commit 1282042

File tree

1 file changed

+62
-17
lines changed

1 file changed

+62
-17
lines changed
 

‎example_test.go

+62-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package env
22

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -99,6 +100,24 @@ func ExampleParse_unset() {
99100
// Output: {Secret:1234} -
100101
}
101102

103+
// You can use `envSeparator` to define which character should be used to
104+
// separate array items in a string.
105+
// Similarly, you can use `envKeyValSeparator` to define which character should
106+
// be used to separate a key from a value in a map.
107+
// The defaults are `,` and `:`, respectively.
108+
func ExampleParse_seprator() {
109+
type Config struct {
110+
Map map[string]string `env:"CUSTOM_MAP" envSeparator:"-" envKeyValSeparator:"|"`
111+
}
112+
os.Setenv("CUSTOM_MAP", "k1|v1-k2|v2")
113+
var cfg Config
114+
if err := Parse(&cfg); err != nil {
115+
fmt.Println(err)
116+
}
117+
fmt.Printf("%+v", cfg)
118+
// Output: {Map:map[k1:v1 k2:v2]}
119+
}
120+
102121
// If you set the `expand` option, environment variables (either in `${var}` or
103122
// `$var` format) in the string will be replaced according with the actual
104123
// value of the variable. For example:
@@ -319,6 +338,22 @@ func ExampleParse_prefix() {
319338
// Output: {A:{Foo:a} B:{Foo:b}}
320339
}
321340

341+
// Setting prefixes for the entire config.
342+
func ExampleParseWithOptions_prefix() {
343+
type Config struct {
344+
Foo string `env:"FOO"`
345+
}
346+
os.Setenv("MY_APP_FOO", "a")
347+
var cfg Config
348+
if err := ParseWithOptions(&cfg, Options{
349+
Prefix: "MY_APP_",
350+
}); err != nil {
351+
fmt.Println(err)
352+
}
353+
fmt.Printf("%+v", cfg)
354+
// Output: {Foo:a}
355+
}
356+
322357
// Use a different tag name than `env`.
323358
func ExampleParseWithOptions_tagName() {
324359
type Config struct {
@@ -383,29 +418,39 @@ func ExampleParse_fromFile() {
383418

384419
func ExampleParse_errorHandling() {
385420
type Config struct {
386-
Username string `env:"USERNAME" envDefault:"admin"`
387-
Password string `env:"PASSWORD,notEmpty"`
421+
Username string `env:"EX_ERR_USERNAME" envDefault:"admin"`
422+
Password string `env:"EX_ERR_PASSWORD,notEmpty"`
388423
}
389424

390425
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)
426+
if err := Parse(&cfg); err != nil {
427+
if errors.Is(err, EmptyEnvVarError{}) {
428+
fmt.Println("oopsie")
429+
}
430+
aggErr := AggregateError{}
431+
if ok := errors.As(err, &aggErr); ok {
432+
for _, er := range aggErr.Errors {
433+
switch v := er.(type) {
434+
// Handle the error types you need:
435+
// ParseError
436+
// NotStructPtrError
437+
// NoParserError
438+
// NoSupportedTagOptionError
439+
// EnvVarIsNotSetError
440+
// EmptyEnvVarError
441+
// LoadFileContentError
442+
// ParseValueError
443+
case EmptyEnvVarError:
444+
fmt.Println("daisy")
445+
default:
446+
fmt.Printf("Unknown error type %v", v)
447+
}
405448
}
406449
}
407450
}
408451

409452
fmt.Printf("%+v", cfg)
410-
// Output: {Username:admin Password:}
453+
// Output: oopsie
454+
// daisy
455+
// {Username:admin Password:}
411456
}

0 commit comments

Comments
 (0)
Please sign in to comment.