Skip to content

Commit

Permalink
Fixed the code based on comments from the EditorConfig linter
Browse files Browse the repository at this point in the history
  • Loading branch information
BorzdeG committed Aug 22, 2024
1 parent 6da32f5 commit 8ca224d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 38 deletions.
14 changes: 13 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,27 @@ charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
max_line_length = 120
ij_visual_guides = 100,120,150

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

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

[*.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
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,3 @@ coverage.txt
bin
card.png
dist

!.editorconfig
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
54 changes: 27 additions & 27 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func TestParsesEnv_Map(t *testing.T) {
MapStringString map[string]string `env:"MAP_STRING_STRING" envSeparator:","`
MapStringInt64 map[string]int64 `env:"MAP_STRING_INT64"`
MapStringBool map[string]bool `env:"MAP_STRING_BOOL" envSeparator:";"`
CustomSeparatorMapStringString map[string]string `env:"CUSTOM_SEPARATOR_MAP_STRING_STRING" envSeparator:"," envKeyValSeparator:"|"`
CustomSeparatorMapStringString map[string]string `env:"CUSTOM_SEPARATOR_MAP_STRING_STRING" envSeparator:"," envKeyValSeparator:"|"` // editorconfig-checker-disable-line
}

mss := map[string]string{
Expand Down Expand Up @@ -614,7 +614,7 @@ func TestParsesEnvInnerFails(t *testing.T) {
}
t.Setenv("NUMBER", "not-a-number")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -630,7 +630,7 @@ func TestParsesEnvInnerFailsMultipleErrors(t *testing.T) {
}
t.Setenv("NUMBER", "not-a-number")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax; required environment variable "AGE" is not set`)
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; parse error on field "Number" of type "int": strconv.ParseInt: parsing "not-a-number": invalid syntax; required environment variable "AGE" is not set`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
Expand All @@ -648,7 +648,7 @@ func TestParsesEnvInnerInvalid(t *testing.T) {
InnerStruct: &InnerStruct{},
}
err := Parse(&cfg)
isErrorWithMessage(t, err, `env: parse error on field "Number" of type "uint": strconv.ParseUint: parsing "-547": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Number" of type "uint": strconv.ParseUint: parsing "-547": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand Down Expand Up @@ -691,49 +691,49 @@ func TestPassReference(t *testing.T) {
func TestInvalidBool(t *testing.T) {
t.Setenv("BOOL", "should-be-a-bool")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Bool" of type "bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax; parse error on field "BoolPtr" of type "*bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Bool" of type "bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax; parse error on field "BoolPtr" of type "*bool": strconv.ParseBool: parsing "should-be-a-bool": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidInt(t *testing.T) {
t.Setenv("INT", "should-be-an-int")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Int" of type "int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax; parse error on field "IntPtr" of type "*int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Int" of type "int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax; parse error on field "IntPtr" of type "*int": strconv.ParseInt: parsing "should-be-an-int": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidUint(t *testing.T) {
t.Setenv("UINT", "-44")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Uint" of type "uint": strconv.ParseUint: parsing "-44": invalid syntax; parse error on field "UintPtr" of type "*uint": strconv.ParseUint: parsing "-44": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Uint" of type "uint": strconv.ParseUint: parsing "-44": invalid syntax; parse error on field "UintPtr" of type "*uint": strconv.ParseUint: parsing "-44": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidFloat32(t *testing.T) {
t.Setenv("FLOAT32", "AAA")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Float32" of type "float32": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float32Ptr" of type "*float32": strconv.ParseFloat: parsing "AAA": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Float32" of type "float32": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float32Ptr" of type "*float32": strconv.ParseFloat: parsing "AAA": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidFloat64(t *testing.T) {
t.Setenv("FLOAT64", "AAA")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Float64" of type "float64": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float64Ptr" of type "*float64": strconv.ParseFloat: parsing "AAA": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Float64" of type "float64": strconv.ParseFloat: parsing "AAA": invalid syntax; parse error on field "Float64Ptr" of type "*float64": strconv.ParseFloat: parsing "AAA": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidUint64(t *testing.T) {
t.Setenv("UINT64", "AAA")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Uint64" of type "uint64": strconv.ParseUint: parsing "AAA": invalid syntax; parse error on field "Uint64Ptr" of type "*uint64": strconv.ParseUint: parsing "AAA": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Uint64" of type "uint64": strconv.ParseUint: parsing "AAA": invalid syntax; parse error on field "Uint64Ptr" of type "*uint64": strconv.ParseUint: parsing "AAA": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidInt64(t *testing.T) {
t.Setenv("INT64", "AAA")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Int64" of type "int64": strconv.ParseInt: parsing "AAA": invalid syntax; parse error on field "Int64Ptr" of type "*int64": strconv.ParseInt: parsing "AAA": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "Int64" of type "int64": strconv.ParseInt: parsing "AAA": invalid syntax; parse error on field "Int64Ptr" of type "*int64": strconv.ParseInt: parsing "AAA": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -743,7 +743,7 @@ func TestInvalidInt64Slice(t *testing.T) {
BadFloats []int64 `env:"BADINTS"`
}
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]int64": strconv.ParseInt: parsing "A": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]int64": strconv.ParseInt: parsing "A": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -753,7 +753,7 @@ func TestInvalidUInt64Slice(t *testing.T) {
BadFloats []uint64 `env:"BADINTS"`
}
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]uint64": strconv.ParseUint: parsing "A": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]uint64": strconv.ParseUint: parsing "A": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -763,7 +763,7 @@ func TestInvalidFloat32Slice(t *testing.T) {
BadFloats []float32 `env:"BADFLOATS"`
}
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float32": strconv.ParseFloat: parsing "A": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float32": strconv.ParseFloat: parsing "A": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -773,7 +773,7 @@ func TestInvalidFloat64Slice(t *testing.T) {
BadFloats []float64 `env:"BADFLOATS"`
}
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float64": strconv.ParseFloat: parsing "A": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "BadFloats" of type "[]float64": strconv.ParseFloat: parsing "A": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -783,21 +783,21 @@ func TestInvalidBoolsSlice(t *testing.T) {
BadBools []bool `env:"BADBOOLS"`
}
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "BadBools" of type "[]bool": strconv.ParseBool: parsing "faaaalse": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "BadBools" of type "[]bool": strconv.ParseBool: parsing "faaaalse": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidDuration(t *testing.T) {
t.Setenv("DURATION", "should-be-a-valid-duration")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Duration" of type "time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"; parse error on field "DurationPtr" of type "*time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"`)
isErrorWithMessage(t, err, `env: parse error on field "Duration" of type "time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"; parse error on field "DurationPtr" of type "*time.Duration": unable to parse duration: time: invalid duration "should-be-a-valid-duration"`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

func TestInvalidDurations(t *testing.T) {
t.Setenv("DURATIONS", "1s,contains-an-invalid-duration,3s")
err := Parse(&Config{})
isErrorWithMessage(t, err, `env: parse error on field "Durations" of type "[]time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"; parse error on field "DurationPtrs" of type "[]*time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"`)
isErrorWithMessage(t, err, `env: parse error on field "Durations" of type "[]time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"; parse error on field "DurationPtrs" of type "[]*time.Duration": unable to parse duration: time: invalid duration "contains-an-invalid-duration"`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -813,7 +813,7 @@ func TestParseStructWithInvalidFieldKind(t *testing.T) {
}
t.Setenv("BLAH", "a")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "WontWorkByte" of type "uint8": strconv.ParseUint: parsing "a": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "WontWorkByte" of type "uint8": strconv.ParseUint: parsing "a": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -835,7 +835,7 @@ func TestBadSeparator(t *testing.T) {

t.Setenv("WONTWORK", "1,2,3,4")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "WontWork" of type "[]int": strconv.ParseInt: parsing "1,2,3,4": invalid syntax`)
isErrorWithMessage(t, err, `env: parse error on field "WontWork" of type "[]int": strconv.ParseInt: parsing "1,2,3,4": invalid syntax`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand Down Expand Up @@ -1320,7 +1320,7 @@ func TestTextUnmarshalerError(t *testing.T) {
}
t.Setenv("UNMARSHALER", "invalid")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "Unmarshaler" of type "env.unmarshaler": time: invalid duration "invalid"`)
isErrorWithMessage(t, err, `env: parse error on field "Unmarshaler" of type "env.unmarshaler": time: invalid duration "invalid"`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -1330,7 +1330,7 @@ func TestTextUnmarshalersError(t *testing.T) {
}
t.Setenv("UNMARSHALERS", "1s,invalid")
err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "Unmarshalers" of type "[]env.unmarshaler": time: invalid duration "invalid"`)
isErrorWithMessage(t, err, `env: parse error on field "Unmarshalers" of type "[]env.unmarshaler": time: invalid duration "invalid"`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand All @@ -1350,7 +1350,7 @@ func TestParseInvalidURL(t *testing.T) {
t.Setenv("EXAMPLE_URL_2", "nope://s s/")

err := Parse(&config{})
isErrorWithMessage(t, err, `env: parse error on field "ExampleURL" of type "url.URL": unable to parse URL: parse "nope://s s/": invalid character " " in host name`)
isErrorWithMessage(t, err, `env: parse error on field "ExampleURL" of type "url.URL": unable to parse URL: parse "nope://s s/": invalid character " " in host name`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, ParseError{}))
}

Expand Down Expand Up @@ -1452,7 +1452,7 @@ func TestFileBadFile(t *testing.T) {
}

err := Parse(&config{})
isErrorWithMessage(t, err, fmt.Sprintf(`env: could not load content of file "%s" from variable SECRET_KEY: open %s: %s`, filename, filename, oserr))
isErrorWithMessage(t, err, fmt.Sprintf(`env: could not load content of file "%s" from variable SECRET_KEY: open %s: %s`, filename, filename, oserr)) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, LoadFileContentError{}))
}

Expand Down Expand Up @@ -1524,7 +1524,7 @@ func TestRequiredIfNoDefOption(t *testing.T) {

t.Run("missing", func(t *testing.T) {
err := ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; required environment variable "FRUIT" is not set`)
isErrorWithMessage(t, err, `env: required environment variable "NAME" is not set; required environment variable "FRUIT" is not set`) // editorconfig-checker-disable-line
isTrue(t, errors.Is(err, EnvVarIsNotSetError{}))
t.Setenv("NAME", "John")
err = ParseWithOptions(&cfg, Options{RequiredIfNoDef: true})
Expand Down Expand Up @@ -1584,7 +1584,7 @@ func TestPrefix(t *testing.T) {
Clean Config
}
cfg := ComplexConfig{}
isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_HOME": "/foo", "BAR_HOME": "/bar", "HOME": "/clean"}}))
isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_HOME": "/foo", "BAR_HOME": "/bar", "HOME": "/clean"}})) // editorconfig-checker-disable-line
isEqual(t, "/foo", cfg.Foo.Home)
isEqual(t, "/bar", cfg.Bar.Home)
isEqual(t, "/clean", cfg.Clean.Home)
Expand All @@ -1605,7 +1605,7 @@ func TestPrefixPointers(t *testing.T) {
Bar: &Test{},
Clean: &Test{},
}
isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_TEST": "kek", "BAR_TEST": "lel", "TEST": "clean"}}))
isNoErr(t, ParseWithOptions(&cfg, Options{Environment: map[string]string{"FOO_TEST": "kek", "BAR_TEST": "lel", "TEST": "clean"}})) // editorconfig-checker-disable-line
isEqual(t, "kek", cfg.Foo.Str)
isEqual(t, "lel", cfg.Bar.Str)
isEqual(t, "clean", cfg.Clean.Str)
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 8ca224d

Please sign in to comment.