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 retrieval of pflag stringSlice #240

Merged
merged 1 commit into from
Sep 22, 2016
Merged
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
23 changes: 13 additions & 10 deletions viper.go
Original file line number Diff line number Diff line change
Expand Up @@ -749,45 +749,48 @@ func (v *Viper) find(key string) interface{} {
// PFlag Override first
flag, exists := v.pflags[key]
if exists && flag.HasChanged() {
jww.TRACE.Println(key, "found in override (via pflag):", flag.ValueString())
jww.TRACE.Printf("%q found in pflag override (%s): %s", key, flag.ValueType(), flag.ValueString())
switch flag.ValueType() {
case "int", "int8", "int16", "int32", "int64":
return cast.ToInt(flag.ValueString())
case "bool":
return cast.ToBool(flag.ValueString())
case "stringSlice":
s := strings.TrimPrefix(flag.ValueString(), "[")

Choose a reason for hiding this comment

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

This wrongly trims the "[" and "]" in the flags.

Fix it by:

s := flag.ValueString()
return s[1:len(s)-1]

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@shenwei356, your solution is probably faster, but strings.TrimPrefix only removes a single "[" character. The problem with pflag was that it used strings.Trim which would remove multiples.

Choose a reason for hiding this comment

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

you are right!

return strings.TrimSuffix(s, "]")
default:
return flag.ValueString()
}
}

val, exists = v.override[key]
if exists {
jww.TRACE.Println(key, "found in override:", val)
jww.TRACE.Printf("%q found in override: %s", key, val)
return val
}

if v.automaticEnvApplied {
// even if it hasn't been registered, if automaticEnv is used,
// check any Get request
if val = v.getEnv(v.mergeWithEnvPrefix(key)); val != "" {
jww.TRACE.Println(key, "found in environment with val:", val)
jww.TRACE.Printf("%q found in environment: %s", key, val)
return val
}
}

envkey, exists := v.env[key]
if exists {
jww.TRACE.Println(key, "registered as env var", envkey)
jww.TRACE.Printf("%q registered as env var %q", key, envkey)
if val = v.getEnv(envkey); val != "" {
jww.TRACE.Println(envkey, "found in environment with val:", val)
jww.TRACE.Printf("%q found in environment: %s", envkey, val)
return val
}
jww.TRACE.Println(envkey, "env value unset:")
jww.TRACE.Printf("%q env value unset", envkey)
}

val, exists = v.config[key]
if exists {
jww.TRACE.Println(key, "found in config:", val)
jww.TRACE.Printf("%q found in config (%T): %s", key, val, val)
return val
}

Expand All @@ -800,7 +803,7 @@ func (v *Viper) find(key string) interface{} {
if reflect.TypeOf(source).Kind() == reflect.Map {
val := v.searchMap(cast.ToStringMap(source), path[1:])
if val != nil {
jww.TRACE.Println(key, "found in nested config:", val)
jww.TRACE.Printf("%q found in nested config: %s", key, val)
return val
}
}
Expand All @@ -809,13 +812,13 @@ func (v *Viper) find(key string) interface{} {

val, exists = v.kvstore[key]
if exists {
jww.TRACE.Println(key, "found in key/value store:", val)
jww.TRACE.Printf("%q found in key/value store: %s", key, val)
return val
}

val, exists = v.defaults[key]
if exists {
jww.TRACE.Println(key, "found in defaults:", val)
jww.TRACE.Printf("%q found in defaults: ", key, val)
return val
}

Expand Down