Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix errcheck related to type assertion #654

Merged
merged 2 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion cmdutil/when.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmdutil

import (
"fmt"
"os"
"strings"

Expand Down Expand Up @@ -46,8 +47,10 @@ func IsAllowedToExecute(when string) (bool, error) {
}
if got, err := expr.Run(program, whenEnv); err != nil {
return false, errors.WithStack(err)
} else if got, ok := got.(bool); !ok {
return false, fmt.Errorf("expected bool, but got %T", got)
} else {
return got.(bool), nil
return got, nil
}
}

Expand Down
15 changes: 13 additions & 2 deletions cmdutil/when_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,26 @@ func TestIsAllowedToExecute(t *testing.T) {
want: false,
errorContains: "unknown name NoneSuchVariable",
},
{
name: "Expression produces an integer",
envset: map[string]string{},
when: "123",
want: false,
errorContains: "expected bool, but got int",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
NewWhenEnv = func() *WhenEnv { return &WhenEnv{Env: tt.envset} }
got, err := IsAllowedToExecute(tt.when)
if err != nil {
if tt.errorContains != nil {
if !strings.Contains(err.Error(), tt.errorContains.(string)) {
t.Errorf("Error %v does not contain %s", err, tt.errorContains)
if errStr, ok := tt.errorContains.(string); ok {
if !strings.Contains(err.Error(), errStr) {
t.Errorf("Error %v does not contain %s", err, errStr)
}
} else {
t.Errorf("errorContains should be a string, but got %T", tt.errorContains)
}
} else {
t.Error(err)
Expand Down
4 changes: 3 additions & 1 deletion config/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ func (f *Format) UnmarshalYAML(data []byte) error {
case []interface{}:
values := []string{}
for _, vv := range v {
values = append(values, vv.(string))
if str, ok := vv.(string); ok {
values = append(values, str)
}
}
f.HideColumnsWithoutValues = values
}
Expand Down
4 changes: 3 additions & 1 deletion dict/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ func New() Dict {

func (d *Dict) Lookup(k string) string {
if v, ok := d.s.Load(k); ok {
return v.(string)
if str, ok := v.(string); ok {
return str
}
}
return k
}
Expand Down
Loading