diff options
| author | Dave Henderson <dhenderson@gmail.com> | 2019-10-15 00:04:45 -0400 |
|---|---|---|
| committer | Dave Henderson <dhenderson@gmail.com> | 2019-10-15 00:04:45 -0400 |
| commit | 882696f858a764ea9354763370e10aa15d31d1b4 (patch) | |
| tree | 8ec5ffd08a100d617939f576d532d59803e73248 /coll | |
| parent | 4e8d13bc227b8ffdc4ce5232555cb683212b0c30 (diff) | |
Slight performance improvement & refactoring
Signed-off-by: Dave Henderson <dhenderson@gmail.com>
Diffstat (limited to 'coll')
| -rw-r--r-- | coll/coll.go | 32 |
1 files changed, 9 insertions, 23 deletions
diff --git a/coll/coll.go b/coll/coll.go index 8e41ed91..dbbbea8c 100644 --- a/coll/coll.go +++ b/coll/coll.go @@ -11,7 +11,7 @@ import ( "sort" "github.com/hairyhenderson/gomplate/conv" - "github.com/pkg/errors" + iconv "github.com/hairyhenderson/gomplate/internal/conv" ) // Slice creates a slice from a bunch of arguments @@ -19,21 +19,6 @@ func Slice(args ...interface{}) []interface{} { return args } -func interfaceSlice(slice interface{}) ([]interface{}, error) { - s := reflect.ValueOf(slice) - kind := s.Kind() - switch kind { - case reflect.Slice, reflect.Array: - ret := make([]interface{}, s.Len()) - for i := 0; i < s.Len(); i++ { - ret[i] = s.Index(i).Interface() - } - return ret, nil - default: - return nil, errors.Errorf("expected an array or slice, but got a %T", s) - } -} - // Has determines whether or not a given object has a property with the given key func Has(in interface{}, key interface{}) bool { av := reflect.ValueOf(in) @@ -120,7 +105,7 @@ func Values(in ...map[string]interface{}) ([]interface{}, error) { // Append v to the end of list. No matter what type of input slice or array list is, a new []interface{} is always returned. func Append(v interface{}, list interface{}) ([]interface{}, error) { - l, err := interfaceSlice(list) + l, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } @@ -130,7 +115,7 @@ func Append(v interface{}, list interface{}) ([]interface{}, error) { // Prepend v to the beginning of list. No matter what type of input slice or array list is, a new []interface{} is always returned. func Prepend(v interface{}, list interface{}) ([]interface{}, error) { - l, err := interfaceSlice(list) + l, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } @@ -140,7 +125,7 @@ func Prepend(v interface{}, list interface{}) ([]interface{}, error) { // Uniq finds the unique values within list. No matter what type of input slice or array list is, a new []interface{} is always returned. func Uniq(list interface{}) ([]interface{}, error) { - l, err := interfaceSlice(list) + l, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } @@ -156,7 +141,7 @@ func Uniq(list interface{}) ([]interface{}, error) { // Reverse the list. No matter what type of input slice or array list is, a new []interface{} is always returned. func Reverse(list interface{}) ([]interface{}, error) { - l, err := interfaceSlice(list) + l, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } @@ -224,7 +209,7 @@ func Sort(key string, list interface{}) (out []interface{}, err error) { return nil, nil } - ia, err := interfaceSlice(list) + ia, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } @@ -296,14 +281,15 @@ func sameTypes(a []interface{}) bool { // Flatten a nested array or slice to at most 'depth' levels. Use depth of -1 // to completely flatten the input. // Returns a new slice without modifying the input. -func Flatten(list interface{}, depth int) (out []interface{}, err error) { - l, err := interfaceSlice(list) +func Flatten(list interface{}, depth int) ([]interface{}, error) { + l, err := iconv.InterfaceSlice(list) if err != nil { return nil, err } if depth == 0 { return l, nil } + out := make([]interface{}, 0, len(l)*2) for _, v := range l { s := reflect.ValueOf(v) kind := s.Kind() |
