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 bug where string slice flag is not assigned #102623

Merged
merged 3 commits into from
Jun 30, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package flag

import (
goflag "flag"
"fmt"
"strings"

"github.com/spf13/pflag"
Expand Down Expand Up @@ -45,9 +46,11 @@ func (s *StringSlice) String() string {
}

func (s *StringSlice) Set(val string) error {
if s.value == nil || !s.changed {
v := make([]string, 0)
s.value = &v
vazmin marked this conversation as resolved.
Show resolved Hide resolved
if s.value == nil {
return fmt.Errorf("no target (nil pointer to []string)")
vazmin marked this conversation as resolved.
Show resolved Hide resolved
}
if !s.changed {
*s.value = make([]string, 0)
vazmin marked this conversation as resolved.
Show resolved Hide resolved
}
*s.value = append(*s.value, val)
s.changed = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ func TestStringSlice(t *testing.T) {
} else if err != nil {
t.Errorf("%d: expected nil error, got %v", i, err)
}
if !reflect.DeepEqual(*v.value, test.expected) {
t.Errorf("%d: expected %+v, got %+v", i, test.expected, *v.value)
if !reflect.DeepEqual(s, test.expected) {
Copy link
Contributor Author

@vazmin vazmin Jun 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s is the flag, the expected should be compared with s. When compared with s, the previous test failed

t.Errorf("%d: expected %+v, got %+v", i, test.expected, s)
}
if v.changed != test.changed {
t.Errorf("%d: expected %t got %t", i, test.changed, v.changed)
Expand Down