Skip to content

Commit 390412e

Browse files
committedJun 15, 2023
docs: clarify custom parser funcs and required fields
closes #268
1 parent 787f006 commit 390412e

File tree

1 file changed

+37
-19
lines changed

1 file changed

+37
-19
lines changed
 

‎README.md

+37-19
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[![Coverage Status](https://img.shields.io/codecov/c/gh/caarlos0/env.svg?logo=codecov&style=for-the-badge)](https://codecov.io/gh/caarlos0/env)
55
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=for-the-badge)](https://pkg.go.dev/github.com/caarlos0/env/v8)
66

7-
A simple and zero-dependencies library to parse environment variables into structs.
7+
A simple and zero-dependencies library to parse environment variables into
8+
`struct`s.
89

910
## Example
1011

@@ -125,8 +126,10 @@ for more info.
125126

126127
Env supports by default anything that implements the `TextUnmarshaler` interface.
127128
That includes things like `time.Time` for example.
128-
The upside is that depending on the format you need, you don't need to change anything.
129-
The downside is that if you do need time in another format, you'll need to create your own type.
129+
The upside is that depending on the format you need, you don't need to change
130+
anything.
131+
The downside is that if you do need time in another format, you'll need to
132+
create your own type.
130133

131134
Its fairly straightforward:
132135

@@ -148,19 +151,28 @@ And then you can parse `Config` with `env.Parse`.
148151

149152
## Required fields
150153

151-
The `env` tag option `required` (e.g., `env:"tagKey,required"`) can be added to ensure that some environment variable is set.
152-
In the example above, an error is returned if the `config` struct is changed to:
154+
The `env` tag option `required` (e.g., `env:"tagKey,required"`) can be added to
155+
ensure that some environment variable is set. In the example above, an error is
156+
returned if the `config` struct is changed to:
153157

154158
```go
155159
type config struct {
156160
SecretKey string `env:"SECRET_KEY,required"`
157161
}
158162
```
159163

164+
> **Warning**
165+
>
166+
> Note that being set is not the same as being empty.
167+
> If the variable is set, but empty, the field will have its type's default
168+
> value.
169+
> This also means that custom parser funcs will not be invoked.
170+
160171
## Not Empty fields
161172

162-
While `required` demands the environment variable to be set, it doesn't check its value.
163-
If you want to make sure the environment is set and not empty, you need to use the `notEmpty` tag option instead (`env:"SOME_ENV,notEmpty"`).
173+
While `required` demands the environment variable to be set, it doesn't check
174+
its value. If you want to make sure the environment is set and not empty, you
175+
need to use the `notEmpty` tag option instead (`env:"SOME_ENV,notEmpty"`).
164176

165177
Example:
166178

@@ -186,9 +198,9 @@ type config struct {
186198
## From file
187199

188200
The `env` tag option `file` (e.g., `env:"tagKey,file"`) can be added
189-
to in order to indicate that the value of the variable shall be loaded from a file. The path of that file is given
190-
by the environment variable associated with it
191-
Example below
201+
in order to indicate that the value of the variable shall be loaded from a
202+
file.
203+
The path of that file is given by the environment variable associated with it:
192204

193205
```go
194206
package main
@@ -269,9 +281,11 @@ func main() {
269281

270282
### Environment
271283

272-
By setting the `Options.Environment` map you can tell `Parse` to add those `keys` and `values`
273-
as env vars before parsing is done. These envs are stored in the map and never actually set by `os.Setenv`.
274-
This option effectively makes `env` ignore the OS environment variables: only the ones provided in the option are used.
284+
By setting the `Options.Environment` map you can tell `Parse` to add those
285+
`keys` and `values` as `env` vars before parsing is done.
286+
These `envs` are stored in the map and never actually set by `os.Setenv`.
287+
This option effectively makes `env` ignore the OS environment variables: only
288+
the ones provided in the option are used.
275289

276290
This can make your testing scenarios a bit more clean and easy to handle.
277291

@@ -307,8 +321,8 @@ func main() {
307321

308322
### Changing default tag name
309323

310-
You can change what tag name to use for setting the env vars by setting the `Options.TagName`
311-
variable.
324+
You can change what tag name to use for setting the env vars by setting the
325+
`Options.TagName` variable.
312326

313327
For example
314328

@@ -391,7 +405,8 @@ func main() {
391405

392406
### On set hooks
393407

394-
You might want to listen to value sets and, for example, log something or do some other kind of logic.
408+
You might want to listen to value sets and, for example, log something or do
409+
some other kind of logic.
395410
You can do this by passing a `OnSet` option:
396411

397412
```go
@@ -429,7 +444,8 @@ func main() {
429444

430445
## Making all fields to required
431446

432-
You can make all fields that don't have a default value be required by setting the `RequiredIfNoDef: true` in the `Options`.
447+
You can make all fields that don't have a default value be required by setting
448+
the `RequiredIfNoDef: true` in the `Options`.
433449

434450
For example
435451

@@ -464,8 +480,10 @@ func main() {
464480

465481
## Defaults from code
466482

467-
You may define default value also in code, by initialising the config data before it's filled by `env.Parse`.
468-
Default values defined as struct tags will overwrite existing values during Parse.
483+
You may define default value also in code, by initialising the config data
484+
before it's filled by `env.Parse`.
485+
Default values defined as struct tags will overwrite existing values during
486+
Parse.
469487

470488
```go
471489
package main

0 commit comments

Comments
 (0)
Please sign in to comment.