Skip to content

Commit

Permalink
ci: add EditorConfig (#327)
Browse files Browse the repository at this point in the history
* 1. Added [EditorConfig](https://editorconfig.org/)
1. Fixed the text formatting in the README

* 1. Fixed the text formatting in the README

* 1. Partially rolled back text formatting edits in README - GitHub does not work correctly with formatting

* Added EditorConfig linter in GitHub Actions

* Fixed the code based on comments from the EditorConfig linter

* Replaced the linter and corrected the comments from the new linter

* Revert EditorConfig linter

* Fixer review comments

* Fixed review comments

* Fixed review comments
  • Loading branch information
BorzdeG authored Oct 8, 2024
1 parent 0847ba1 commit b76caa9
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 42 deletions.
28 changes: 28 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 120

[{go.mod,go.sum,*.go}]
insert_final_newline = true
indent_size = tab
indent_style = tab
tab_width = 4

[Makefile]
max_line_length = off
insert_final_newline = true
indent_size = tab
indent_style = tab
tab_width = 4

[*.md]
max_line_length = off
trim_trailing_whitespace = false
indent_size = tab
indent_style = space
tab_width = 2

[.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
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
14 changes: 14 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
linters-settings:
revive:
rules:
- name: line-length-limit
arguments: [120]

issues:
exclude-rules:
- path: _test\.go
linters:
- revive
text: "line-length-limit:"

linters:
enable:
- thelper
Expand All @@ -6,3 +19,4 @@ linters:
- unconvert
- unparam
- wastedassign
- revive
44 changes: 15 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,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 @@ -54,19 +53,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 @@ -89,24 +84,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 @@ -125,20 +115,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 @@ -125,7 +125,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 @@ -332,7 +337,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 @@ -559,7 +569,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
9 changes: 5 additions & 4 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// editorconfig-checker-disable-file
package env

import (
Expand Down Expand Up @@ -485,7 +486,7 @@ func TestParseCustomMapType(t *testing.T) {

var cfg config
isNoErr(t, ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
reflect.TypeOf(custommap{}): func(value string) (interface{}, error) {
reflect.TypeOf(custommap{}): func(_ string) (interface{}, error) {
return custommap(map[string]bool{}), nil
},
}}))
Expand Down Expand Up @@ -547,7 +548,7 @@ func TestParseMapCustomKeyTypeError(t *testing.T) {

var cfg config
err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
reflect.TypeOf(CustomKey("")): func(value string) (interface{}, error) {
reflect.TypeOf(CustomKey("")): func(_ string) (interface{}, error) {
return nil, fmt.Errorf("custom error")
},
}})
Expand All @@ -565,7 +566,7 @@ func TestParseMapCustomValueTypeError(t *testing.T) {

var cfg config
err := ParseWithOptions(&cfg, Options{FuncMap: map[reflect.Type]ParserFunc{
reflect.TypeOf(Customval("")): func(value string) (interface{}, error) {
reflect.TypeOf(Customval("")): func(_ string) (interface{}, error) {
return nil, fmt.Errorf("custom error")
},
}})
Expand Down Expand Up @@ -1148,7 +1149,7 @@ func TestCustomParserError(t *testing.T) {
name string
}

customParserFunc := func(v string) (interface{}, error) {
customParserFunc := func(_ string) (interface{}, error) {
return nil, errors.New("something broke")
}

Expand Down
3 changes: 2 additions & 1 deletion env_tomap_windows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ package env

import "testing"

// On Windows, environment variables can start with '='. This test verifies this behavior without relying on a Windows environment.
// On Windows, environment variables can start with '='.
// This test verifies this behavior without relying on a Windows environment.
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58
func TestToMapWindows(t *testing.T) {
envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR", "FOO=", "BAR"}
Expand Down
3 changes: 2 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import (
"strings"
)

// An aggregated error wrapper to combine gathered errors. This allows either to display all errors or convert them individually
// An aggregated error wrapper to combine gathered errors.
// This allows either to display all errors or convert them individually
// List of the available errors
// ParseError
// NotStructPtrError
Expand Down
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
module github.com/caarlos0/env/v11

retract v11.0.1 // v11.0.1 accidentally introduced a breaking change regarding the behavior of nil pointers. You can now chose to auto-initialize them by setting the `init` tag option.
// v11.0.1 accidentally introduced a breaking change regarding the behavior of nil pointers.
// You can now chose to auto-initialize them by setting the `init` tag option.
retract v11.0.1

retract v11.2.0 // v11.2.0 accidentally introduced a breaking change regarding the behavior of nil slices of complex types.
// v11.2.0 accidentally introduced a breaking change regarding the behavior of nil slices of complex types.
retract v11.2.0

go 1.18

0 comments on commit b76caa9

Please sign in to comment.