[breaking] do not return first element of 1-sized slices as it, return a slice

- it is more consistent and does not rely of a "hidden" assomption.
 - for consistency, it is also a better practice to always  the same type when waiting to receive a slice ; the 1 element case should not break anything
This commit is contained in:
Adrien Marquès 2020-03-02 22:24:36 +01:00
parent 7e7eb3ac29
commit 5741ec597b
Signed by: xdrm-brackets
GPG Key ID: D75243CA236D825E
1 changed files with 2 additions and 17 deletions

View File

@ -66,27 +66,12 @@ func parseParameter(data interface{}) (interface{}, error) {
/* (1) []string -> recursive */ /* (1) []string -> recursive */
case reflect.Slice: case reflect.Slice:
// 1. Return nothing if empty // 1. ignore empty
if dvalue.Len() == 0 { if dvalue.Len() == 0 {
return data, nil return data, nil
} }
// 2. only return first element if alone // 2. parse each element recursively
if dvalue.Len() == 1 {
element := dvalue.Index(0)
// try to parse if a string (containing json)
if element.Kind() == reflect.String {
return parseParameter(element.String())
}
// already typed
return element.Interface(), nil
}
// 3. Return all elements if more than 1
result := make([]interface{}, dvalue.Len()) result := make([]interface{}, dvalue.Len())
for i, l := 0, dvalue.Len(); i < l; i++ { for i, l := 0, dvalue.Len(); i < l; i++ {