diff --git a/.golangci.yml b/.golangci.yml index 73db613..8e51bdc 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,4 +1,13 @@ linters-settings: + gocritic: + enabled-checks: + - emptyStringTest + - evalOrder + - paramTypeCombine + - preferStringWriter + - sprintfQuotedString + - stringConcatSimplify + - yodaStyleExpr revive: rules: - name: line-length-limit @@ -15,6 +24,7 @@ linters: enable: - thelper - gofumpt + - gocritic - tparallel - unconvert - unparam diff --git a/env.go b/env.go index b815e79..4fa5b2e 100644 --- a/env.go +++ b/env.go @@ -263,14 +263,16 @@ func ParseWithOptions(v interface{}, opts Options) error { // values from environment variables. func ParseAs[T any]() (T, error) { var t T - return t, Parse(&t) + err := Parse(&t) + return t, err } // ParseWithOptions parses the given struct type containing `env` tags and // loads its values from environment variables. func ParseAsWithOptions[T any](opts Options) (T, error) { var t T - return t, ParseWithOptions(&t, opts) + err := ParseWithOptions(&t, opts) + return t, err } // Must panic is if err is not nil, and returns t otherwise. @@ -355,10 +357,10 @@ func doParseField( if !refField.CanSet() { return nil } - if reflect.Ptr == refField.Kind() && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() { + if refField.Kind() == reflect.Ptr && refField.Elem().Kind() == reflect.Struct && !refField.IsNil() { return parseInternal(refField.Interface(), processField, optionsWithEnvPrefix(refTypeField, opts)) } - if reflect.Struct == refField.Kind() && refField.CanAddr() && refField.Type().Name() == "" { + if refField.Kind() == reflect.Struct && refField.CanAddr() && refField.Type().Name() == "" { return parseInternal(refField.Addr().Interface(), processField, optionsWithEnvPrefix(refTypeField, opts)) } @@ -384,7 +386,7 @@ func doParseField( } } - if reflect.Struct == refField.Kind() { + if refField.Kind() == reflect.Struct { return doParse(refField, processField, optionsWithEnvPrefix(refTypeField, opts)) } @@ -397,17 +399,17 @@ func doParseField( func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool { field := refTypeField.Type - if reflect.Ptr == field.Kind() { + if field.Kind() == reflect.Ptr { field = field.Elem() } - if reflect.Slice != field.Kind() { + if field.Kind() != reflect.Slice { return false } field = field.Elem() - if reflect.Ptr == field.Kind() { + if field.Kind() == reflect.Ptr { field = field.Elem() } @@ -422,7 +424,7 @@ func isSliceOfStructs(refTypeField reflect.StructField, opts Options) bool { } if !ignore { - ignore = reflect.Struct != field.Kind() + ignore = field.Kind() != reflect.Struct } return !ignore } @@ -603,7 +605,7 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) { defer os.Unsetenv(fieldParams.Key) } - if fieldParams.Required && !exists && len(fieldParams.OwnKey) > 0 { + if fieldParams.Required && !exists && fieldParams.OwnKey != "" { return "", newVarIsNotSetError(fieldParams.Key) } @@ -638,7 +640,7 @@ func getFromFile(filename string) (value string, err error) { return string(b), err } -func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists bool, isDefault bool) { +func getOr(key, defaultValue string, defExists bool, envs map[string]string) (val string, exists, isDefault bool) { value, exists := envs[key] switch { case (!exists || key == "") && defExists: @@ -794,7 +796,7 @@ func handleMap(field reflect.Value, value string, sf reflect.StructField, funcMa } func asTextUnmarshaler(field reflect.Value) encoding.TextUnmarshaler { - if reflect.Ptr == field.Kind() { + if field.Kind() == reflect.Ptr { if field.IsNil() { field.Set(reflect.New(field.Type().Elem())) } diff --git a/env_test.go b/env_test.go index 6017889..290b296 100644 --- a/env_test.go +++ b/env_test.go @@ -276,7 +276,7 @@ func TestParsesEnv(t *testing.T) { t.Setenv("URL", tos(url1)) t.Setenv("URLS", toss(url1, url2)) - t.Setenv("SEPSTRINGS", strings.Join([]string{str1, str2}, ":")) + t.Setenv("SEPSTRINGS", str1+":"+str2) nonDefinedStr := "nonDefinedStr" t.Setenv("NONDEFINED_STR", nonDefinedStr) @@ -1485,7 +1485,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 %q from variable SECRET_KEY: open %s: %s", filename, filename, oserr)) isTrue(t, errors.Is(err, LoadFileContentError{})) } diff --git a/error.go b/error.go index 087ef82..d615912 100644 --- a/error.go +++ b/error.go @@ -68,7 +68,7 @@ func newParseError(sf reflect.StructField, err error) error { } func (e ParseError) Error() string { - return fmt.Sprintf(`parse error on field "%s" of type "%s": %v`, e.Name, e.Type, e.Err) + return fmt.Sprintf("parse error on field %q of type %q: %v", e.Name, e.Type, e.Err) } // The error occurs when pass something that is not a pointer to a struct to Parse @@ -89,7 +89,7 @@ func newNoParserError(sf reflect.StructField) error { } func (e NoParserError) Error() string { - return fmt.Sprintf(`no parser found for field "%s" of type "%s"`, e.Name, e.Type) + return fmt.Sprintf("no parser found for field %q of type %q", e.Name, e.Type) } // This error occurs when the given tag is not supported. @@ -109,7 +109,7 @@ func (e NoSupportedTagOptionError) Error() string { // This error occurs when the required variable is not set. // -// deprecated: use VarIsNotSetError +// Deprecated: use VarIsNotSetError. type EnvVarIsNotSetError = VarIsNotSetError // This error occurs when the required variable is not set. @@ -127,7 +127,7 @@ func (e VarIsNotSetError) Error() string { // This error occurs when the variable which must be not empty is existing but has an empty value // -// deprecated: use EmptyVarError +// Deprecated: use EmptyVarError. type EmptyEnvVarError = EmptyVarError // This error occurs when the variable which must be not empty is existing but has an empty value @@ -155,7 +155,7 @@ func newLoadFileContentError(filename, key string, err error) error { } func (e LoadFileContentError) Error() string { - return fmt.Sprintf(`could not load content of file "%s" from variable %s: %v`, e.Filename, e.Key, e.Err) + return fmt.Sprintf("could not load content of file %q from variable %s: %v", e.Filename, e.Key, e.Err) } // This error occurs when it's impossible to convert value using given parser. diff --git a/example_test.go b/example_test.go index e1a5a92..533beea 100644 --- a/example_test.go +++ b/example_test.go @@ -3,7 +3,6 @@ package env import ( "errors" "fmt" - "io" "os" "reflect" ) @@ -400,7 +399,7 @@ func ExampleParseWithOptions_useFieldName() { // it. func ExampleParse_fromFile() { f, _ := os.CreateTemp("", "") - _, _ = io.WriteString(f, "super secret") + _, _ = f.WriteString("super secret") _ = f.Close() type Config struct {