Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

config: Add length to built-in functions #1495

Merged
merged 1 commit into from
Apr 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions config/interpolate_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
89 changes: 89 additions & 0 deletions config/interpolate_funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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(
Expand Down