diff --git a/altsrc/flag.go b/altsrc/flag.go index 84ef009a5b..9f2af7bddd 100644 --- a/altsrc/flag.go +++ b/altsrc/flag.go @@ -72,7 +72,7 @@ func (f *GenericFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourc } if value != nil { eachName(f.Name, func(name string) { - f.set.Set(f.Name, value.String()) + f.set.Set(name, value.String()) }) } } @@ -92,7 +92,7 @@ func (f *StringSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputS if value != nil { var sliceValue cli.StringSlice = value eachName(f.Name, func(name string) { - underlyingFlag := f.set.Lookup(f.Name) + underlyingFlag := f.set.Lookup(name) if underlyingFlag != nil { underlyingFlag.Value = &sliceValue } @@ -114,7 +114,7 @@ func (f *IntSliceFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour if value != nil { var sliceValue cli.IntSlice = value eachName(f.Name, func(name string) { - underlyingFlag := f.set.Lookup(f.Name) + underlyingFlag := f.set.Lookup(name) if underlyingFlag != nil { underlyingFlag.Value = &sliceValue } @@ -135,7 +135,7 @@ func (f *BoolFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCo } if value { eachName(f.Name, func(name string) { - f.set.Set(f.Name, strconv.FormatBool(value)) + f.set.Set(name, strconv.FormatBool(value)) }) } } @@ -153,7 +153,7 @@ func (f *BoolTFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceC } if !value { eachName(f.Name, func(name string) { - f.set.Set(f.Name, strconv.FormatBool(value)) + f.set.Set(name, strconv.FormatBool(value)) }) } } @@ -171,7 +171,7 @@ func (f *StringFlag) ApplyInputSourceValue(context *cli.Context, isc InputSource } if value != "" { eachName(f.Name, func(name string) { - f.set.Set(f.Name, value) + f.set.Set(name, value) }) } } @@ -189,7 +189,7 @@ func (f *IntFlag) ApplyInputSourceValue(context *cli.Context, isc InputSourceCon } if value > 0 { eachName(f.Name, func(name string) { - f.set.Set(f.Name, strconv.FormatInt(int64(value), 10)) + f.set.Set(name, strconv.FormatInt(int64(value), 10)) }) } } @@ -207,7 +207,7 @@ func (f *DurationFlag) ApplyInputSourceValue(context *cli.Context, isc InputSour } if value > 0 { eachName(f.Name, func(name string) { - f.set.Set(f.Name, value.String()) + f.set.Set(name, value.String()) }) } } @@ -226,7 +226,7 @@ func (f *Float64Flag) ApplyInputSourceValue(context *cli.Context, isc InputSourc if value > 0 { floatStr := float64ToString(value) eachName(f.Name, func(name string) { - f.set.Set(f.Name, floatStr) + f.set.Set(name, floatStr) }) } } diff --git a/altsrc/json_source_context.go b/altsrc/json_source_context.go index 47ce82c49b..34db265006 100644 --- a/altsrc/json_source_context.go +++ b/altsrc/json_source_context.go @@ -182,7 +182,17 @@ func (x *jsonSource) BoolT(name string) (bool, error) { } func (x *jsonSource) getValue(key string) (interface{}, error) { - return jsonGetValue(key, x.deserialized) + parts := strings.Split(key, ",") + var ret interface{} + var err error + for _, name := range parts { + name = strings.Trim(name, " ") + ret, err = jsonGetValue(name, x.deserialized) + if err == nil && ret != nil { + break + } + } + return ret, err } func jsonGetValue(key string, m map[string]interface{}) (interface{}, error) { diff --git a/altsrc/map_input_source.go b/altsrc/map_input_source.go index a111eeee28..89bb5aef32 100644 --- a/altsrc/map_input_source.go +++ b/altsrc/map_input_source.go @@ -39,23 +39,33 @@ func nestedVal(name string, tree map[interface{}]interface{}) (interface{}, bool return nil, false } +func (fsm *MapInputSource) getValue(key string) (interface{}, bool) { + parts := strings.Split(key, ",") + var ret interface{} + var exists bool = false + for _, name := range parts { + name = strings.Trim(name, " ") + ret, exists = fsm.valueMap[name] + if exists { + break + } + ret, exists = nestedVal(name, fsm.valueMap) + if exists { + break + } + } + return ret, exists +} + // Int returns an int from the map if it exists otherwise returns 0 func (fsm *MapInputSource) Int(name string) (int, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(int) - if !isType { - return 0, incorrectTypeForFlagError(name, "int", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(int) + value, isType := genericValue.(int) if !isType { - return 0, incorrectTypeForFlagError(name, "int", nestedGenericValue) + return 0, incorrectTypeForFlagError(name, "int", genericValue) } - return otherValue, nil + return value, nil } return 0, nil @@ -63,21 +73,13 @@ func (fsm *MapInputSource) Int(name string) (int, error) { // Duration returns a duration from the map if it exists otherwise returns 0 func (fsm *MapInputSource) Duration(name string) (time.Duration, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(time.Duration) - if !isType { - return 0, incorrectTypeForFlagError(name, "duration", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(time.Duration) + value, isType := genericValue.(time.Duration) if !isType { - return 0, incorrectTypeForFlagError(name, "duration", nestedGenericValue) + return 0, incorrectTypeForFlagError(name, "duration", genericValue) } - return otherValue, nil + return value, nil } return 0, nil @@ -85,21 +87,13 @@ func (fsm *MapInputSource) Duration(name string) (time.Duration, error) { // Float64 returns an float64 from the map if it exists otherwise returns 0 func (fsm *MapInputSource) Float64(name string) (float64, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(float64) - if !isType { - return 0, incorrectTypeForFlagError(name, "float64", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(float64) + value, isType := genericValue.(float64) if !isType { - return 0, incorrectTypeForFlagError(name, "float64", nestedGenericValue) + return 0, incorrectTypeForFlagError(name, "float64", genericValue) } - return otherValue, nil + return value, nil } return 0, nil @@ -107,21 +101,13 @@ func (fsm *MapInputSource) Float64(name string) (float64, error) { // String returns a string from the map if it exists otherwise returns an empty string func (fsm *MapInputSource) String(name string) (string, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(string) - if !isType { - return "", incorrectTypeForFlagError(name, "string", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(string) + value, isType := genericValue.(string) if !isType { - return "", incorrectTypeForFlagError(name, "string", nestedGenericValue) + return "", incorrectTypeForFlagError(name, "string", genericValue) } - return otherValue, nil + return value, nil } return "", nil @@ -129,21 +115,18 @@ func (fsm *MapInputSource) String(name string) (string, error) { // StringSlice returns an []string from the map if it exists otherwise returns nil func (fsm *MapInputSource) StringSlice(name string) ([]string, error) { - otherGenericValue, exists := fsm.valueMap[name] + genericValue, exists := fsm.getValue(name) if !exists { - otherGenericValue, exists = nestedVal(name, fsm.valueMap) - if !exists { - return nil, nil - } + return nil, nil } - otherValue, isType := otherGenericValue.([]interface{}) + value, isType := genericValue.([]interface{}) if !isType { - return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue) + return nil, incorrectTypeForFlagError(name, "[]interface{}", genericValue) } - var stringSlice = make([]string, 0, len(otherValue)) - for i, v := range otherValue { + var stringSlice = make([]string, 0, len(value)) + for i, v := range value { stringValue, isType := v.(string) if !isType { @@ -158,21 +141,18 @@ func (fsm *MapInputSource) StringSlice(name string) ([]string, error) { // IntSlice returns an []int from the map if it exists otherwise returns nil func (fsm *MapInputSource) IntSlice(name string) ([]int, error) { - otherGenericValue, exists := fsm.valueMap[name] + genericValue, exists := fsm.getValue(name) if !exists { - otherGenericValue, exists = nestedVal(name, fsm.valueMap) - if !exists { - return nil, nil - } + return nil, nil } - otherValue, isType := otherGenericValue.([]interface{}) + value, isType := genericValue.([]interface{}) if !isType { - return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue) + return nil, incorrectTypeForFlagError(name, "[]interface{}", genericValue) } - var intSlice = make([]int, 0, len(otherValue)) - for i, v := range otherValue { + var intSlice = make([]int, 0, len(value)) + for i, v := range value { intValue, isType := v.(int) if !isType { @@ -187,21 +167,13 @@ func (fsm *MapInputSource) IntSlice(name string) ([]int, error) { // Generic returns an cli.Generic from the map if it exists otherwise returns nil func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(cli.Generic) - if !isType { - return nil, incorrectTypeForFlagError(name, "cli.Generic", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(cli.Generic) + value, isType := genericValue.(cli.Generic) if !isType { - return nil, incorrectTypeForFlagError(name, "cli.Generic", nestedGenericValue) + return nil, incorrectTypeForFlagError(name, "cli.Generic", genericValue) } - return otherValue, nil + return value, nil } return nil, nil @@ -209,21 +181,13 @@ func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) { // Bool returns an bool from the map otherwise returns false func (fsm *MapInputSource) Bool(name string) (bool, error) { - otherGenericValue, exists := fsm.valueMap[name] + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := otherGenericValue.(bool) + value, isType := genericValue.(bool) if !isType { - return false, incorrectTypeForFlagError(name, "bool", otherGenericValue) + return false, incorrectTypeForFlagError(name, "bool", genericValue) } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) - if exists { - otherValue, isType := nestedGenericValue.(bool) - if !isType { - return false, incorrectTypeForFlagError(name, "bool", nestedGenericValue) - } - return otherValue, nil + return value, nil } return false, nil @@ -231,21 +195,13 @@ func (fsm *MapInputSource) Bool(name string) (bool, error) { // BoolT returns an bool from the map otherwise returns true func (fsm *MapInputSource) BoolT(name string) (bool, error) { - otherGenericValue, exists := fsm.valueMap[name] - if exists { - otherValue, isType := otherGenericValue.(bool) - if !isType { - return true, incorrectTypeForFlagError(name, "bool", otherGenericValue) - } - return otherValue, nil - } - nestedGenericValue, exists := nestedVal(name, fsm.valueMap) + genericValue, exists := fsm.getValue(name) if exists { - otherValue, isType := nestedGenericValue.(bool) + value, isType := genericValue.(bool) if !isType { - return true, incorrectTypeForFlagError(name, "bool", nestedGenericValue) + return true, incorrectTypeForFlagError(name, "bool", genericValue) } - return otherValue, nil + return value, nil } return true, nil