-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
114 lines (104 loc) · 4 KB
/
example_test.go
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
package dotconfig_test
import (
"errors"
"fmt"
"strings"
"github.com/DeanPDX/dotconfig"
)
type AppConfig struct {
MaxBytesPerRequest int `env:"MAX_BYTES_PER_REQUEST"`
APIVersion float64 `env:"API_VERSION"`
IsDev bool `env:"IS_DEV"`
StripeSecret string `env:"STRIPE_SECRET"`
WelcomeMessage string `env:"WELCOME_MESSAGE"`
}
const appConfigSample = `
MAX_BYTES_PER_REQUEST="1024"
API_VERSION=1.19
# All of these are valie for booleans:
# 1, t, T, TRUE, true, True, 0, f, F, FALSE, false, False
IS_DEV='1'
STRIPE_SECRET='sk_test_insertkeyhere'
# Right now supporting newlines via "\n" in strings:
WELCOME_MESSAGE='Hello,\nWelcome to the app!\n-The App Dev Team'`
func ExampleFromReader() {
config, err := dotconfig.FromReader[AppConfig](strings.NewReader(appConfigSample))
if err != nil {
fmt.Printf("Didn't expect error. Got %v.", err)
}
// Don't do this in the real world, as your config will
// have secrets from a secret manager and you don't want
// to print them to the console.
fmt.Printf("App config loaded.\nMax Bytes: %v. Version: %v. Dev? %v. Stripe Secret: %v.\nWelcome Message:\n%v",
config.MaxBytesPerRequest, config.APIVersion, config.IsDev, config.StripeSecret, config.WelcomeMessage)
// Output:
// App config loaded.
// Max Bytes: 1024. Version: 1.19. Dev? true. Stripe Secret: sk_test_insertkeyhere.
// Welcome Message:
// Hello,
// Welcome to the app!
// -The App Dev Team
}
type ConfigWithErrors struct {
StripeSecret string `env:"SHOULD_BE_MISSING"`
Complex complex128 `env:"COMPLEX"`
WelcomeMessage string
RequiredField string `env:"REQUIRED_FIELD,required"` // Can't be zero-value
}
const exampleErrorsEnv = `
COMPLEX=asdf
REQUIRED_FIELD="" # Will cause error because zero-value`
func ExampleErrors() {
r := strings.NewReader(exampleErrorsEnv)
_, err := dotconfig.FromReader[ConfigWithErrors](r, dotconfig.EnforceStructTags)
if err != nil {
// Get error slice from err
errs := dotconfig.Errors(err)
for _, err := range errs {
// Handle various error types however you want
switch {
case errors.Is(errors.Unwrap(err), dotconfig.ErrMissingEnvVar):
// Handle missing environment variable
fmt.Printf("Missing env variable: %v\n", err)
case errors.Is(errors.Unwrap(err), dotconfig.ErrMissingStructTag):
// Handle missing struct tag
fmt.Printf("Missing struct tag: %v\n", err)
case errors.Is(errors.Unwrap(err), dotconfig.ErrUnsupportedFieldType):
// Handle unsupported field
fmt.Printf("Unsupported type: %v\n", err)
case errors.Is(errors.Unwrap(err), dotconfig.ErrMissingRequiredField):
// Handle required field
fmt.Printf("Required field can't be zero value: %v\n", err)
}
}
}
// Output:
// Missing env variable: key not present in ENV: SHOULD_BE_MISSING
// Unsupported type: unsupported field type: complex128
// Missing struct tag: missing struct tag on field: WelcomeMessage
// Required field can't be zero value: field must have non-zero value: REQUIRED_FIELD
}
type ConfigWithDefaults struct {
MaxBytesPerRequest int `env:"MAX_BYTES" default:"2048"`
IsDev bool `env:"DEVELOPMENT,optional"` // will default to zero value (false)
WelcomeMessage string `env:"APP_HELLO" default:"Hey!"`
AppVersion float64 `env:"APP_VERSION" default:"1.0"`
}
func Example_defaultValues() {
r := strings.NewReader(`APP_VERSION=2.38`)
conf, err := dotconfig.FromReader[ConfigWithDefaults](r, dotconfig.EnforceStructTags)
if err != nil {
fmt.Printf("Didn't expect error. Got %v.", err)
}
fmt.Println("App config loaded")
fmt.Println("Max bytes:", conf.MaxBytesPerRequest) // 2048 from default tag
fmt.Println("Is dev?", conf.IsDev) // False because optional so zero value
fmt.Println("Welcome message:", conf.WelcomeMessage) // "Hey!" from default tag
fmt.Println("App version:", conf.AppVersion) // 2.38 because ENV value overrides default
// Output:
// App config loaded
// Max bytes: 2048
// Is dev? false
// Welcome message: Hey!
// App version: 2.38
}