You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you need only ENV variables and default values you can use a shorter form:
78
+
```go
79
+
err:= configuration.FromEnvAndDefault(&cfg)
78
80
```
79
81
80
82
@@ -83,54 +85,72 @@ You can specify one or more providers. They will be executed in order of definit
83
85
```go
84
86
[]Provider{
85
87
NewFlagProvider(&cfg), // 1
86
-
NewEnvProvider(), // 2
87
-
NewDefaultProvider(), // 3
88
+
NewEnvProvider(), // 2
89
+
NewDefaultProvider(), // 3
88
90
}
89
91
```
90
92
If provider set value successfully next ones will not be executed (if flag provider from the sample above found a value env and default providers are skipped).
91
93
The value of first successfully executed provider will be set.
92
94
If none of providers found value - an application will be terminated.
93
-
This behavior can be changed with `configurator.SetOnFailFn` method.
95
+
This behavior can be changed with `configurator.OnFailFnOpt` option:
96
+
```go
97
+
err:= configuration.New(
98
+
&cfg,
99
+
configuration.NewEnvProvider(),
100
+
configuration.NewDefaultProvider()).
101
+
SetOptions(
102
+
configuration.OnFailFnOpt(func(err error) {
103
+
log.Println(err)
104
+
}),
105
+
).InitValues()
106
+
```
107
+
94
108
95
109
### Custom provider
96
110
You can define a custom provider which should satisfy next interface:
97
111
```go
98
112
typeProviderinterface {
99
-
Provide(field reflect.StructField, v reflect.Value, pathToField ...string) error
113
+
Name() string
114
+
Init(ptr any) error
115
+
Provide(field reflect.StructField, v reflect.Value) error
100
116
}
101
117
```
102
118
103
119
### Default provider
104
120
Looks for `default` tag and set value from it:
105
121
```go
106
-
struct {
107
-
// ...
108
-
Namestring`default:"defaultName"`
109
-
// ...
110
-
}
122
+
struct {
123
+
// ...
124
+
Namestring`default:"defaultName"`
125
+
// ...
126
+
}
111
127
```
112
128
113
129
114
130
### Env provider
115
131
Looks for `env` tag and tries to find an ENV variable with the name from the tag (`AGE` in this example):
116
132
```go
117
-
struct {
118
-
// ...
119
-
Agebyte`env:"AGE"`
120
-
// ...
121
-
}
133
+
struct {
134
+
// ...
135
+
Agebyte`env:"AGE"`
136
+
// ...
137
+
}
138
+
```
139
+
Name inside tag `env:"<name>"` must be unique for each field. Only UPPER register for ENV vars is accepted:
140
+
```bash
141
+
bad_env_var_name=bad
142
+
GOOD_ENV_VAR_NAME=good
122
143
```
123
-
Name inside tag `env:"<name>"` must be unique for each field.
124
144
125
145
126
146
### Flag provider
127
147
Looks for `flag` tag and tries to set value from the command line flag `-first_name`
0 commit comments