satisfy linters

This commit is contained in:
Willy Kloucek
2022-01-03 08:55:22 +01:00
committed by Jörn Friedrich Dreyer
parent a839288212
commit e89071200f
3 changed files with 28 additions and 30 deletions

View File

@@ -18,7 +18,7 @@ import (
// ErrInvalidTarget indicates that the target value passed to
// Decode is invalid. Target must be a non-nil pointer to a struct.
var ErrInvalidTarget = errors.New("target must be non-nil pointer to struct that has at least one exported field with a valid env tag.")
var ErrInvalidTarget = errors.New("target must be non-nil pointer to struct that has at least one exported field with a valid env tag")
var ErrNoTargetFieldsAreSet = errors.New("none of the target fields were set from environment variables")
// FailureFunc is called when an error is encountered during a MustDecode
@@ -199,7 +199,9 @@ func decode(target interface{}, strict bool) (int, error) {
return 0, err
}
} else if f.Kind() == reflect.Slice {
decodeSlice(&f, env)
if err := decodeSlice(&f, env); err != nil {
return 0, err
}
} else {
if err := decodePrimitiveType(&f, env); err != nil && strict {
return 0, err
@@ -210,7 +212,7 @@ func decode(target interface{}, strict bool) (int, error) {
return setFieldCount, nil
}
func decodeSlice(f *reflect.Value, env string) {
func decodeSlice(f *reflect.Value, env string) error {
parts := strings.Split(env, ";")
values := parts[:0]
@@ -225,11 +227,15 @@ func decodeSlice(f *reflect.Value, env string) {
if valuesCount > 0 {
for i := 0; i < valuesCount; i++ {
e := slice.Index(i)
decodePrimitiveType(&e, values[i])
err := decodePrimitiveType(&e, values[i])
if err != nil {
return err
}
}
}
f.Set(slice)
return nil
}
func decodePrimitiveType(f *reflect.Value, env string) error {
@@ -290,8 +296,7 @@ func decodePrimitiveType(f *reflect.Value, env string) error {
// MustDecode calls Decode and terminates the process if any errors
// are encountered.
func MustDecode(target interface{}) {
err := Decode(target)
if err != nil {
if err := Decode(target); err != nil {
FailureFunc(err)
}
}
@@ -299,8 +304,7 @@ func MustDecode(target interface{}) {
// MustStrictDecode calls StrictDecode and terminates the process if any errors
// are encountered.
func MustStrictDecode(target interface{}) {
err := StrictDecode(target)
if err != nil {
if err := StrictDecode(target); err != nil {
FailureFunc(err)
}
}

View File

@@ -9,7 +9,6 @@ import (
"reflect"
"sort"
"strconv"
"sync"
"testing"
"time"
)
@@ -62,8 +61,6 @@ type testConfig struct {
DefaultSliceInt []int `env:"TEST_UNSET,asdf=asdf,default=1;2;3"`
DefaultDuration time.Duration `env:"TEST_UNSET,asdf=asdf,default=24h"`
DefaultURL *url.URL `env:"TEST_UNSET,default=http://example.com"`
cantInterfaceField sync.Mutex
}
type testConfigNoSet struct {
@@ -83,11 +80,12 @@ type testConfigOverride struct {
}
type testNoExportedFields struct {
aString string `env:"TEST_STRING"`
anInt64 int64 `env:"TEST_INT64"`
aUint16 uint16 `env:"TEST_UINT16"`
aFloat64 float64 `env:"TEST_FLOAT64"`
aBool bool `env:"TEST_BOOL"`
// folowing unexported fields are used for tests
aString string `env:"TEST_STRING"` //nolint:structcheck,unused
anInt64 int64 `env:"TEST_INT64"` //nolint:structcheck,unused
aUint16 uint16 `env:"TEST_UINT16"` //nolint:structcheck,unused
aFloat64 float64 `env:"TEST_FLOAT64"` //nolint:structcheck,unused
aBool bool `env:"TEST_BOOL"` //nolint:structcheck,unused
}
type testNoTags struct {
@@ -223,9 +221,9 @@ func TestDecode(t *testing.T) {
}
urlVal, _ := url.Parse("https://example.com")
expectedUrlSlice := []*url.URL{urlVal}
if !reflect.DeepEqual(tc.URLSlice, expectedUrlSlice) {
t.Fatalf("Expected %s, got %s", expectedUrlSlice, tc.URLSlice)
expectedURLSlice := []*url.URL{urlVal}
if !reflect.DeepEqual(tc.URLSlice, expectedURLSlice) {
t.Fatalf("Expected %s, got %s", expectedURLSlice, tc.URLSlice)
}
if tc.UnsetString != "" {
@@ -366,7 +364,7 @@ func TestDecodeErrors(t *testing.T) {
}
var tc testConfig
err = Decode(tc)
err = Decode(tc) //nolint:govet
if err != ErrInvalidTarget {
t.Fatal("Should have gotten an error decoding into a non-pointer")
}
@@ -413,10 +411,9 @@ func TestDecodeErrors(t *testing.T) {
var tcrd testConfigRequiredDefault
defer func() {
if r := recover(); r != nil {
}
recover()
}()
err = Decode(&tcrd)
_ = Decode(&tcrd)
t.Fatal("This should not have been reached. A panic should have occured.")
}
@@ -464,8 +461,7 @@ func ExampleDecode() {
os.Setenv("EXAMPLE_STRING", "an example!")
var e Example
err := Decode(&e)
if err != nil {
if err := Decode(&e); err != nil {
panic(err)
}
@@ -500,7 +496,7 @@ type testConfigExport struct {
UnsetURL *url.URL `env:"TEST_UNSET_URL"`
UnusedField string
unexportedField string
unexportedField string //nolint:structcheck,unused
IgnoredPtr *bool `env:"TEST_IGNORED_POINTER"`
@@ -619,8 +615,7 @@ func TestExport(t *testing.T) {
tc.NestedPtr = &nestedConfigExportPointer{}
tc.NoConfigPtrSet = &noConfig{}
err := Decode(&tc)
if err != nil {
if err := Decode(&tc); err != nil {
t.Fatal(err)
}

View File

@@ -215,12 +215,11 @@ func (i *Indexer) FindByPartial(t interface{}, field string, pattern string) ([]
// Update updates all indexes on a value <from> to a value <to>.
func (i *Indexer) Update(from, to interface{}) error {
typeNameFrom := getTypeFQN(from)
typeNameTo := getTypeFQN(to)
i.mu.Lock(typeNameFrom)
defer i.mu.Unlock(typeNameFrom)
if typeNameFrom != typeNameTo {
if typeNameTo := getTypeFQN(to); typeNameFrom != typeNameTo {
return fmt.Errorf("update types do not match: from %v to %v", typeNameFrom, typeNameTo)
}