Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added EditorConfig #327

Merged
merged 11 commits into from
Oct 8, 2024
31 changes: 31 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
root = true
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 120
ij_visual_guides = 100,120,150
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved

[{go.mod,go.sum,*.go}]
insert_final_newline = true
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
indent_size = tab
indent_style = tab
tab_width = 2
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved

[Makefile]
max_line_length = off
insert_final_newline = true
indent_size = tab
indent_style = tab
tab_width = 2
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved

[*.md]
max_line_length = off
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
trim_trailing_whitespace = false
indent_size = tab
indent_style = space
tab_width = 2
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved

[.mailmap]
max_line_length = off
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.txt
- uses: goreleaser/goreleaser-action@v6
if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable'
if: success() && startsWith(github.ref, 'refs/tags/') && matrix.os == 'ubuntu-latest' && matrix.go-version == 'stable' # editorconfig-checker-disable-line
BorzdeG marked this conversation as resolved.
Show resolved Hide resolved
with:
version: latest
distribution: goreleaser-pro
Expand Down
9 changes: 8 additions & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
name: golangci-lint
name: linters
on:
push:
pull_request:
jobs:
editorconfig:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: editorconfig-checker/action-editorconfig-checker@main
- run: editorconfig-checker

golangci:
name: lint
runs-on: ubuntu-latest
Expand Down
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ err := env.Parse(&cfg)
cfg, err := env.ParseAs[config]()
```

You can see the full documentation and list of examples at
[pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11).
You can see the full documentation and list of examples at [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11).

---

## Used and supported by

<p>
<a href="https://encore.dev">
<img src="https://user-images.githubusercontent.com/78424526/214602214-52e0483a-b5fc-4d4c-b03e-0b7b23e012df.svg" width="120px" alt="encore icon"></img>
<img src="https://user-images.githubusercontent.com/78424526/214602214-52e0483a-b5fc-4d4c-b03e-0b7b23e012df.svg" width="120px" alt="encore icon" />
</a>
<br/>
<br/>
Expand All @@ -48,19 +47,15 @@ You can see the full documentation and list of examples at

- `Parse`: parse the current environment into a type
- `ParseAs`: parse the current environment into a type using generics
- `ParseWithOptions`: parse the current environment into a type with custom
options
- `ParseAsithOptions`: parse the current environment into a type with custom
options and using generics
- `ParseWithOptions`: parse the current environment into a type with custom options
- `ParseAsithOptions`: parse the current environment into a type with custom options and using generics
- `Must`: can be used to wrap `Parse.*` calls to panic on error
- `GetFieldParams`: get the `env` parsed options for a type
- `GetFieldParamsWithOptions`: get the `env` parsed options for a type with
custom options
- `GetFieldParamsWithOptions`: get the `env` parsed options for a type with custom options

### Supported types

Out of the box all built-in types are supported, plus a few others that
are commonly used.
Out of the box all built-in types are supported, plus a few others that are commonly used.

Complete list:

Expand All @@ -82,24 +77,19 @@ Complete list:
- `encoding.TextUnmarshaler`
- `url.URL`

Pointers, slices and slices of pointers, and maps of those types are also
supported.
Pointers, slices and slices of pointers, and maps of those types are also supported.

You may also add custom parsers for your types.

### Tags

The following tags are provided:

- `env`: sets the environment variable name and optionally takes the tag options
described below
- `env`: sets the environment variable name and optionally takes the tag options described below
- `envDefault`: sets the default value for the field
- `envPrefix`: can be used in a field that is a complex type to set a prefix to
all environment variables used in it
- `envSeparator`: sets the character to be used to separate items in slices and
maps (default: `,`)
- `envKeyValSeparator`: sets the character to be used to separate keys and their
values in maps (default: `:`)
- `envPrefix`: can be used in a field that is a complex type to set a prefix to all environment variables used in it
- `envSeparator`: sets the character to be used to separate items in slices and maps (default: `,`)
- `envKeyValSeparator`: sets the character to be used to separate keys and their values in maps (default: `:`)

### `env` tag options

Expand All @@ -118,20 +108,16 @@ There are a few options available in the functions that end with `WithOptions`:

- `Environment`: keys and values to be used instead of `os.Environ()`
- `TagName`: specifies another tag name to use rather than the default `env`
- `RequiredIfNoDef`: set all `env` fields as required if they do not declare
`envDefault`
- `OnSet`: allows to hook into the `env` parsing and do something when a value
is set
- `RequiredIfNoDef`: set all `env` fields as required if they do not declare `envDefault`
- `OnSet`: allows to hook into the `env` parsing and do something when a value is set
- `Prefix`: prefix to be used in all environment variables
- `UseFieldNameByDefault`: defines whether or not `env` should use the field name by default if the `env` key is missing
- `FuncMap`: custom parse functions for custom types

### Documentation and examples

Examples are live in
[pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11),
and also in the
[example test file](./example_test.go).
Examples are live in [pkg.go.dev](https://pkg.go.dev/github.com/caarlos0/env/v11),
and also in the [example test file](./example_test.go).

## Badges

Expand Down
21 changes: 18 additions & 3 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,12 @@ type ParserFunc func(v string) (interface{}, error)
type OnSetFn func(tag string, value interface{}, isDefault bool)

// processFieldFn is a function which takes all information about a field and processes it.
type processFieldFn func(refField reflect.Value, refTypeField reflect.StructField, opts Options, fieldParams FieldParams) error
type processFieldFn func(
refField reflect.Value,
refTypeField reflect.StructField,
opts Options,
fieldParams FieldParams,
) error

// Options for the parser.
type Options struct {
Expand Down Expand Up @@ -310,7 +315,12 @@ func doParse(ref reflect.Value, processField processFieldFn, opts Options) error
return agrErr
}

func doParseField(refField reflect.Value, refTypeField reflect.StructField, processField processFieldFn, opts Options) error {
func doParseField(
refField reflect.Value,
refTypeField reflect.StructField,
processField processFieldFn,
opts Options,
) error {
if !refField.CanSet() {
return nil
}
Expand Down Expand Up @@ -537,7 +547,12 @@ func parseFieldParams(field reflect.StructField, opts Options) (FieldParams, err
func get(fieldParams FieldParams, opts Options) (val string, err error) {
var exists, isDefault bool

val, exists, isDefault = getOr(fieldParams.Key, fieldParams.DefaultValue, fieldParams.HasDefaultValue, opts.Environment)
val, exists, isDefault = getOr(
fieldParams.Key,
fieldParams.DefaultValue,
fieldParams.HasDefaultValue,
opts.Environment,
)

if fieldParams.Expand {
val = os.Expand(val, opts.getRawEnv)
Expand Down
Loading
Loading