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(Flags): panic on user errors #256

Merged
merged 3 commits into from
Mar 8, 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
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
5 changes: 1 addition & 4 deletions z/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,15 @@ func (sf *SuperFlag) MergeAndCheckDefault(flag string) *SuperFlag {
return sf
}
src := parseFlag(flag)
/* TODO: better way to do this? maybe don't panic
numKeys := len(sf.m)
for k := range src {
if _, ok := sf.m[k]; ok {
numKeys--
}
}
if numKeys != 0 {
msg := fmt.Sprintf("Found invalid options in %s. Valid options: %v", sf, flag)
panic(msg)
panic(fmt.Sprintf("Found invalid options in %s. Valid options: %v", sf, flag))
}
*/
for k, v := range src {
if _, ok := sf.m[k]; !ok {
sf.m[k] = v
Expand Down
16 changes: 10 additions & 6 deletions z/flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@ func TestFlag(t *testing.T) {
// bool-key and int-key should not be overwritten. Only other-key should be set.
sf.MergeAndCheckDefault(def)

/* TODO
c := func() {
// Has a typo.
require.Panics(t, func() {
// typo
NewSuperFlag("boolo-key=true").MergeAndCheckDefault(def)
}
require.Panics(t, c)
*/
})

require.Equal(t, true, sf.GetBool("bool-key"))
require.Equal(t, uint64(5), sf.GetUint64("int-key"))
Expand All @@ -35,3 +32,10 @@ func TestFlag(t *testing.T) {
require.Equal(t, time.Hour*12, sf.GetDuration("duration-hours"))
require.Equal(t, time.Hour*24*30, sf.GetDuration("duration-days"))
}

func TestFlagDefault(t *testing.T) {
def := `one=false; two=; three=;`
f := NewSuperFlag(`one=true; two=4;`).MergeAndCheckDefault(def)
require.Equal(t, true, f.GetBool("one"))
require.Equal(t, int64(4), f.GetInt64("two"))
}