From 6a720d087c95e7b85971cf517ce5c3457cb351c6 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Sun, 12 Apr 2015 15:17:26 +0100 Subject: [PATCH] length added to built-in functions --- config/interpolate_funcs.go | 23 +++++++++ config/interpolate_funcs_test.go | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/config/interpolate_funcs.go b/config/interpolate_funcs.go index 353c45500..af1d2b0a4 100644 --- a/config/interpolate_funcs.go +++ b/config/interpolate_funcs.go @@ -23,6 +23,7 @@ func init() { "element": interpolationFuncElement(), "replace": interpolationFuncReplace(), "split": interpolationFuncSplit(), + "length": interpolationFuncLength(), // Concat is a little useless now since we supported embeddded // interpolations but we keep it around for backwards compat reasons. @@ -132,6 +133,28 @@ func interpolationFuncReplace() ast.Function { } } +func interpolationFuncLength() ast.Function { + return ast.Function{ + ArgTypes: []ast.Type{ast.TypeString}, + ReturnType: ast.TypeInt, + Variadic: false, + Callback: func(args []interface{}) (interface{}, error) { + if !strings.Contains(args[0].(string), InterpSplitDelim) { + return len(args[0].(string)), nil + } + + var list []string + for _, arg := range args { + parts := strings.Split(arg.(string), InterpSplitDelim) + for _, part := range parts { + list = append(list, part) + } + } + return len(list), nil + }, + } +} + // interpolationFuncSplit implements the "split" function that allows // strings to split into multi-variable values func interpolationFuncSplit() ast.Function { diff --git a/config/interpolate_funcs_test.go b/config/interpolate_funcs_test.go index 2061e6ad8..309c4f71f 100644 --- a/config/interpolate_funcs_test.go +++ b/config/interpolate_funcs_test.go @@ -183,6 +183,66 @@ func TestInterpolateFuncReplace(t *testing.T) { }) } +func TestInterpolateFuncLength(t *testing.T) { + testFunction(t, testFunctionConfig{ + Cases: []testFunctionCase{ + // Raw strings + { + `${length("")}`, + "0", + false, + }, + { + `${length("a")}`, + "1", + false, + }, + { + `${length(" ")}`, + "1", + false, + }, + { + `${length(" a ,")}`, + "4", + false, + }, + { + `${length("aaa")}`, + "3", + false, + }, + + // Lists + { + `${length(split(",", "a"))}`, + "1", + false, + }, + { + `${length(split(",", "foo,"))}`, + "2", + false, + }, + { + `${length(split(",", ",foo,"))}`, + "3", + false, + }, + { + `${length(split(",", "foo,bar"))}`, + "2", + false, + }, + { + `${length(split(".", "one.two.three.four.five"))}`, + "5", + false, + }, + }, + }) +} + func TestInterpolateFuncSplit(t *testing.T) { testFunction(t, testFunctionConfig{ Cases: []testFunctionCase{ @@ -198,6 +258,35 @@ func TestInterpolateFuncSplit(t *testing.T) { false, }, + { + `${split(",", ",,,")}`, + fmt.Sprintf( + "%s%s%s", + InterpSplitDelim, + InterpSplitDelim, + InterpSplitDelim), + false, + }, + + { + `${split(",", "foo,")}`, + fmt.Sprintf( + "%s%s", + "foo", + InterpSplitDelim), + false, + }, + + { + `${split(",", ",foo,")}`, + fmt.Sprintf( + "%s%s%s", + InterpSplitDelim, + "foo", + InterpSplitDelim), + false, + }, + { `${split(".", "foo.bar.baz")}`, fmt.Sprintf(