Skip to content

Commit

Permalink
Merge pull request #1495 from TimeIncOSS/length-func
Browse files Browse the repository at this point in the history
config: Add length to built-in functions
  • Loading branch information
phinze committed Apr 15, 2015
2 parents 11280a0 + 6a720d0 commit 5874fa0
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
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

0 comments on commit 5874fa0

Please sign in to comment.