Skip to content

Commit

Permalink
refactor: enable gocritic linter and fix lint issues (#342)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Nov 26, 2024
1 parent a9fcfa3 commit f68d1dc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 21 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
- yodaStyleExpr
revive:
rules:
- name: line-length-limit
Expand All @@ -15,6 +24,7 @@ linters:
enable:
- thelper
- gofumpt
- gocritic
- tparallel
- unconvert
- unparam
Expand Down
26 changes: 14 additions & 12 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 @@ -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))
}

Expand All @@ -384,7 +386,7 @@ func doParseField(
}
}

if reflect.Struct == refField.Kind() {
if refField.Kind() == reflect.Struct {
return doParse(refField, processField, optionsWithEnvPrefix(refTypeField, opts))
}

Expand All @@ -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()
}

Expand All @@ -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
}
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 Down Expand Up @@ -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 Expand Up @@ -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()))
}
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 f68d1dc

Please sign in to comment.