Use built-in len function for wf exec

This commit is contained in:
Denis Arh
2021-03-30 14:58:04 +02:00
parent 0e787d4772
commit 723c361171
3 changed files with 35 additions and 5 deletions
+14
View File
@@ -12,6 +12,7 @@ func GenericFunctions() []gval.Language {
gval.Function("coalesce", coalesce),
gval.Function("isEmpty", isEmpty),
gval.Function("isNil", isNil),
gval.Function("length", length),
}
}
@@ -25,6 +26,19 @@ func coalesce(aa ...interface{}) interface{} {
return nil
}
func length(i interface{}) int {
if isEmpty(i) {
return 0
}
switch reflect.TypeOf(i).Kind() {
case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Map, reflect.String:
return reflect.ValueOf(i).Len()
}
return 0
}
func isNil(i interface{}) bool {
if i == nil {
return true
+21
View File
@@ -87,3 +87,24 @@ func Test_empty(t *testing.T) {
req.Equal(tst.expect, isEmpty(tst.value))
}
}
func Test_length(t *testing.T) {
var (
req = require.New(t)
tcc = []struct {
len int
value interface{}
}{
{0, []string{}},
{0, map[string]string{}},
{3, "foo"},
{0, make(chan string)},
{0, 34234},
}
)
for _, tst := range tcc {
req.Equal(tst.len, length(tst.value))
}
}
-5
View File
@@ -15,7 +15,6 @@ func StringFunctions() []gval.Language {
gval.Function("trim", strings.TrimSpace),
gval.Function("trimLeft", strings.TrimLeft),
gval.Function("trimRight", strings.TrimRight),
gval.Function("length", length),
gval.Function("toLower", strings.ToLower),
gval.Function("toUpper", strings.ToUpper),
gval.Function("shortest", shortest),
@@ -59,10 +58,6 @@ func longest(f string, aa ...string) string {
return f
}
func length(s string) int {
return len(s)
}
// title works similarly as strings.ToTitle, with the expception
// of uppercasing only the first word in line
func title(s string) string {