Skip to content

Commit

Permalink
docs: clarify custom parser funcs and required fields
Browse files Browse the repository at this point in the history
closes #268
  • Loading branch information
caarlos0 committed Jun 15, 2023
1 parent 83df5eb commit 0476bcd
Showing 1 changed file with 37 additions and 19 deletions.
56 changes: 37 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[![Coverage Status](https://img.shields.io/codecov/c/gh/caarlos0/env.svg?logo=codecov&style=for-the-badge)](https://codecov.io/gh/caarlos0/env)
[![](http://img.shields.io/badge/godoc-reference-5272B4.svg?style=for-the-badge)](https://pkg.go.dev/github.com/caarlos0/env/v8)

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

## Example

Expand Down Expand Up @@ -125,8 +126,10 @@ for more info.

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

Its fairly straightforward:

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

## Required fields

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

```go
type config struct {
SecretKey string `env:"SECRET_KEY,required"`
}
```

> **Warning**
>
> Note that being set is not the same as being empty.
> If the variable is set, but empty, the field will have its type's default
> value.
> This also means that custom parser funcs will not be invoked.
## Not Empty fields

While `required` demands the environment variable to be set, it doesn't check its value.
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"`).
While `required` demands the environment variable to be set, it doesn't check
its value. 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"`).

Example:

Expand All @@ -186,9 +198,9 @@ type config struct {
## From file

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

```go
package main
Expand Down Expand Up @@ -269,9 +281,11 @@ func main() {

### Environment

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

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

Expand Down Expand Up @@ -307,8 +321,8 @@ func main() {

### Changing default tag name

You can change what tag name to use for setting the env vars by setting the `Options.TagName`
variable.
You can change what tag name to use for setting the env vars by setting the
`Options.TagName` variable.

For example

Expand Down Expand Up @@ -391,7 +405,8 @@ func main() {

### On set hooks

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

```go
Expand Down Expand Up @@ -429,7 +444,8 @@ func main() {

## Making all fields to required

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

For example

Expand Down Expand Up @@ -464,8 +480,10 @@ func main() {

## Defaults from code

You may define default value also in code, by initialising the config data before it's filled by `env.Parse`.
Default values defined as struct tags will overwrite existing values during Parse.
You may define default value also in code, by initialising the config data
before it's filled by `env.Parse`.
Default values defined as struct tags will overwrite existing values during
Parse.

```go
package main
Expand Down

0 comments on commit 0476bcd

Please sign in to comment.