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

support for getting alternate name flag values from other file input … #792

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 9 additions & 9 deletions altsrc/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
}
}
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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))
})
}
}
Expand All @@ -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))
})
}
}
Expand All @@ -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)
})
}
}
Expand All @@ -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))
})
}
}
Expand All @@ -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())
})
}
}
Expand All @@ -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)
})
}
}
Expand Down
12 changes: 11 additions & 1 deletion altsrc/json_source_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
160 changes: 58 additions & 102 deletions altsrc/map_input_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,111 +39,94 @@ 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
}

// 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
}

// 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
}

// 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
}

// 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 {
Expand All @@ -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 {
Expand All @@ -187,65 +167,41 @@ 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
}

// 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
}

// 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
Expand Down