Skip to content

Commit

Permalink
refactor: enable gocritic linter and fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Nov 20, 2024
1 parent a9fcfa3 commit 162dd73
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 14 deletions.
10 changes: 10 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
linters-settings:
gocritic:
enabled-checks:
- emptyStringTest
- evalOrder
- paramTypeCombine
- preferStringWriter
- sprintfQuotedString
- stringConcatSimplify
- unnamedResult
revive:
rules:
- name: line-length-limit
Expand All @@ -15,6 +24,7 @@ linters:
enable:
- thelper
- gofumpt
- gocritic
- tparallel
- unconvert
- unparam
Expand Down
12 changes: 7 additions & 5 deletions env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -628,7 +630,7 @@ func get(fieldParams FieldParams, opts Options) (val string, err error) {
}

// split the env tag's key into the expected key and desired option, if any.
func parseKeyForOption(key string) (string, []string) {
func parseKeyForOption(key string) (ownKey string, tags []string) {
opts := strings.Split(key, ",")
return opts[0], opts[1:]
}
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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{}))
}

Expand Down
10 changes: 5 additions & 5 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package env
import (
"errors"
"fmt"
"io"
"os"
"reflect"
)
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 162dd73

Please sign in to comment.