mirror of
https://github.com/spf13/viper
synced 2025-05-11 06:37:27 +00:00
Allow getting Sub from a prefix of pflags key(s)
* It seems common enough that flags may be defined from PFlags be retrieved as a sub Viper similar to getting it from Yaml or other configuration structures. This allows flags to be bound one-to-one with how a config file may be structured. * This is partially based on #331 for the find boolean flag * fixes #368
This commit is contained in:
parent
1750e0a9d3
commit
d57958a9ff
3 changed files with 42 additions and 6 deletions
|
@ -45,7 +45,7 @@ func TestBindFlagValueSet(t *testing.T) {
|
|||
|
||||
func TestBindFlagValue(t *testing.T) {
|
||||
var testString = "testing"
|
||||
var testValue = newStringValue(testString, &testString)
|
||||
var testValue = newStringValue(testString)
|
||||
|
||||
flag := &pflag.Flag{
|
||||
Name: "testflag",
|
||||
|
|
12
viper.go
12
viper.go
|
@ -1157,6 +1157,18 @@ func (v *Viper) find(lcaseKey string, flagDefault bool) interface{} {
|
|||
// last item, no need to check shadowing
|
||||
}
|
||||
|
||||
// it could also be a key prefix, search for that prefix to get the values from
|
||||
// pflags that match it
|
||||
sub := make(map[string]interface{})
|
||||
for key, val := range v.pflags {
|
||||
if flagDefault && strings.HasPrefix(key, lcaseKey) {
|
||||
sub[strings.TrimPrefix(key, lcaseKey+".")] = val.ValueString()
|
||||
}
|
||||
}
|
||||
if len(sub) != 0 {
|
||||
return sub
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -282,9 +282,8 @@ func initDirs(t *testing.T) (string, string, func()) {
|
|||
// stubs for PFlag Values
|
||||
type stringValue string
|
||||
|
||||
func newStringValue(val string, p *string) *stringValue {
|
||||
*p = val
|
||||
return (*stringValue)(p)
|
||||
func newStringValue(val string) *stringValue {
|
||||
return (*stringValue)(&val)
|
||||
}
|
||||
|
||||
func (s *stringValue) Set(val string) error {
|
||||
|
@ -960,7 +959,7 @@ func TestBindPFlagsNil(t *testing.T) {
|
|||
|
||||
func TestBindPFlag(t *testing.T) {
|
||||
var testString = "testing"
|
||||
var testValue = newStringValue(testString, &testString)
|
||||
var testValue = newStringValue(testString)
|
||||
|
||||
flag := &pflag.Flag{
|
||||
Name: "testflag",
|
||||
|
@ -1042,7 +1041,7 @@ func TestBoundCaseSensitivity(t *testing.T) {
|
|||
assert.Equal(t, "blue", Get("eyes"))
|
||||
|
||||
var testString = "green"
|
||||
var testValue = newStringValue(testString, &testString)
|
||||
var testValue = newStringValue(testString)
|
||||
|
||||
flag := &pflag.Flag{
|
||||
Name: "eyeballs",
|
||||
|
@ -1316,6 +1315,31 @@ func TestSub(t *testing.T) {
|
|||
assert.Equal(t, (*Viper)(nil), subv)
|
||||
}
|
||||
|
||||
func TestSubPflags(t *testing.T) {
|
||||
v := New()
|
||||
|
||||
// same as yamlExample, without hobbies
|
||||
v.BindPFlag("name", &pflag.Flag{Value: newStringValue("steve"), Changed: true})
|
||||
v.BindPFlag("clothing.jacket", &pflag.Flag{Value: newStringValue("leather"), Changed: true})
|
||||
v.BindPFlag("clothing.trousers", &pflag.Flag{Value: newStringValue("denim"), Changed: true})
|
||||
v.BindPFlag("clothing.pants.size", &pflag.Flag{Value: newStringValue("large"), Changed: true})
|
||||
v.BindPFlag("age", &pflag.Flag{Value: newStringValue("35"), Changed: true})
|
||||
v.BindPFlag("eyes", &pflag.Flag{Value: newStringValue("brown"), Changed: true})
|
||||
v.BindPFlag("beard", &pflag.Flag{Value: newStringValue("yes"), Changed: true})
|
||||
|
||||
subv := v.Sub("clothing")
|
||||
assert.Equal(t, v.Get("clothing.pants.size"), subv.Get("pants.size"))
|
||||
|
||||
subv = v.Sub("clothing.pants")
|
||||
assert.Equal(t, v.Get("clothing.pants.size"), subv.Get("size"))
|
||||
|
||||
subv = v.Sub("clothing.pants.size")
|
||||
assert.Equal(t, (*Viper)(nil), subv)
|
||||
|
||||
subv = v.Sub("missing.key")
|
||||
assert.Equal(t, (*Viper)(nil), subv)
|
||||
}
|
||||
|
||||
var hclWriteExpected = []byte(`"foos" = {
|
||||
"foo" = {
|
||||
"key" = 1
|
||||
|
|
Loading…
Add table
Reference in a new issue