Skip to content

Commit

Permalink
Some code improvements
Browse files Browse the repository at this point in the history
* Rewrite empty string checks more idiomatically.
* gofmt envconfig_test.go file.
* Fix typo in specified word.
* strings.ReplaceAll instead of strings.Replace with -1.
* !strings.Contains instead of strings.Index == -1 comparison.
  • Loading branch information
sosiska authored and kelseyhightower committed Sep 13, 2022
1 parent c974cae commit 10e87fe
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions envconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func processField(value string, field reflect.Value) error {
sl := reflect.MakeSlice(typ, 0, 0)
if typ.Elem().Kind() == reflect.Uint8 {
sl = reflect.ValueOf([]byte(value))
} else if len(strings.TrimSpace(value)) != 0 {
} else if strings.TrimSpace(value) != "" {
vals := strings.Split(value, ",")
sl = reflect.MakeSlice(typ, len(vals), len(vals))
for i, val := range vals {
Expand All @@ -318,7 +318,7 @@ func processField(value string, field reflect.Value) error {
field.Set(sl)
case reflect.Map:
mp := reflect.MakeMap(typ)
if len(strings.TrimSpace(value)) != 0 {
if strings.TrimSpace(value) != "" {
pairs := strings.Split(value, ",")
for _, pair := range pairs {
kvpair := strings.Split(pair, ":")
Expand Down
2 changes: 1 addition & 1 deletion envconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ func TestCheckDisallowedIgnored(t *testing.T) {

func TestErrorMessageForRequiredAltVar(t *testing.T) {
var s struct {
Foo string `envconfig:"BAR" required:"true"`
Foo string `envconfig:"BAR" required:"true"`
}

os.Clearenv()
Expand Down
4 changes: 2 additions & 2 deletions usage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestMain(m *testing.M) {
}

func compareUsage(want, got string, t *testing.T) {
got = strings.Replace(got, " ", ".", -1)
got = strings.ReplaceAll(got, " ", ".")
if want != got {
shortest := len(want)
if len(got) < shortest {
Expand Down Expand Up @@ -137,7 +137,7 @@ func TestUsageUnknownKeyFormat(t *testing.T) {
if err == nil {
t.Errorf("expected 'unknown key' error, but got no error")
}
if strings.Index(err.Error(), unknownError) == -1 {
if !strings.Contains(err.Error(), unknownError) {
t.Errorf("expected '%s', but got '%s'", unknownError, err.Error())
}
}
Expand Down

0 comments on commit 10e87fe

Please sign in to comment.