|
1 | 1 | package env
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "errors" |
4 | 5 | "fmt"
|
5 | 6 | "io"
|
6 | 7 | "os"
|
@@ -99,6 +100,24 @@ func ExampleParse_unset() {
|
99 | 100 | // Output: {Secret:1234} -
|
100 | 101 | }
|
101 | 102 |
|
| 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 | + |
102 | 121 | // If you set the `expand` option, environment variables (either in `${var}` or
|
103 | 122 | // `$var` format) in the string will be replaced according with the actual
|
104 | 123 | // value of the variable. For example:
|
@@ -319,6 +338,22 @@ func ExampleParse_prefix() {
|
319 | 338 | // Output: {A:{Foo:a} B:{Foo:b}}
|
320 | 339 | }
|
321 | 340 |
|
| 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 | + |
322 | 357 | // Use a different tag name than `env`.
|
323 | 358 | func ExampleParseWithOptions_tagName() {
|
324 | 359 | type Config struct {
|
@@ -383,29 +418,39 @@ func ExampleParse_fromFile() {
|
383 | 418 |
|
384 | 419 | func ExampleParse_errorHandling() {
|
385 | 420 | 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"` |
388 | 423 | }
|
389 | 424 |
|
390 | 425 | 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 | + } |
405 | 448 | }
|
406 | 449 | }
|
407 | 450 | }
|
408 | 451 |
|
409 | 452 | fmt.Printf("%+v", cfg)
|
410 |
| - // Output: {Username:admin Password:} |
| 453 | + // Output: oopsie |
| 454 | + // daisy |
| 455 | + // {Username:admin Password:} |
411 | 456 | }
|
0 commit comments