length added to built-in functions

This commit is contained in:
Radek Simko 2015-04-12 15:17:26 +01:00
parent 852e7a3554
commit 6a720d087c
2 changed files with 112 additions and 0 deletions

View File

@ -23,6 +23,7 @@ func init() {
"element": interpolationFuncElement(), "element": interpolationFuncElement(),
"replace": interpolationFuncReplace(), "replace": interpolationFuncReplace(),
"split": interpolationFuncSplit(), "split": interpolationFuncSplit(),
"length": interpolationFuncLength(),
// Concat is a little useless now since we supported embeddded // Concat is a little useless now since we supported embeddded
// interpolations but we keep it around for backwards compat reasons. // 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 // interpolationFuncSplit implements the "split" function that allows
// strings to split into multi-variable values // strings to split into multi-variable values
func interpolationFuncSplit() ast.Function { func interpolationFuncSplit() ast.Function {

View File

@ -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) { func TestInterpolateFuncSplit(t *testing.T) {
testFunction(t, testFunctionConfig{ testFunction(t, testFunctionConfig{
Cases: []testFunctionCase{ Cases: []testFunctionCase{
@ -198,6 +258,35 @@ func TestInterpolateFuncSplit(t *testing.T) {
false, 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")}`, `${split(".", "foo.bar.baz")}`,
fmt.Sprintf( fmt.Sprintf(