Skip to content

Commit cccc5d0

Browse files
sl1pm4tapparentlymart
authored andcommitted
Add lower / upper interpolation functions
1 parent 77847b1 commit cccc5d0

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

config/interpolate_funcs.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ func init() {
2929
"index": interpolationFuncIndex(),
3030
"join": interpolationFuncJoin(),
3131
"length": interpolationFuncLength(),
32+
"lower": interpolationFuncLower(),
3233
"replace": interpolationFuncReplace(),
3334
"split": interpolationFuncSplit(),
3435
"base64encode": interpolationFuncBase64Encode(),
3536
"base64decode": interpolationFuncBase64Decode(),
37+
"upper": interpolationFuncUpper(),
3638
}
3739
}
3840

@@ -442,3 +444,29 @@ func interpolationFuncBase64Decode() ast.Function {
442444
},
443445
}
444446
}
447+
448+
// interpolationFuncLower implements the "lower" function that does
449+
// string lower casing.
450+
func interpolationFuncLower() ast.Function {
451+
return ast.Function{
452+
ArgTypes: []ast.Type{ast.TypeString},
453+
ReturnType: ast.TypeString,
454+
Callback: func(args []interface{}) (interface{}, error) {
455+
toLower := args[0].(string)
456+
return strings.ToLower(toLower), nil
457+
},
458+
}
459+
}
460+
461+
// interpolationFuncUpper implements the "upper" function that does
462+
// string upper casing.
463+
func interpolationFuncUpper() ast.Function {
464+
return ast.Function{
465+
ArgTypes: []ast.Type{ast.TypeString},
466+
ReturnType: ast.TypeString,
467+
Callback: func(args []interface{}) (interface{}, error) {
468+
toUpper := args[0].(string)
469+
return strings.ToUpper(toUpper), nil
470+
},
471+
}
472+
}

config/interpolate_funcs_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,54 @@ func TestInterpolateFuncBase64Decode(t *testing.T) {
644644
})
645645
}
646646

647+
func TestInterpolateFuncLower(t *testing.T) {
648+
testFunction(t, testFunctionConfig{
649+
Cases: []testFunctionCase{
650+
{
651+
`${lower("HELLO")}`,
652+
"hello",
653+
false,
654+
},
655+
656+
{
657+
`${lower("")}`,
658+
"",
659+
false,
660+
},
661+
662+
{
663+
`${lower()}`,
664+
nil,
665+
true,
666+
},
667+
},
668+
})
669+
}
670+
671+
func TestInterpolateFuncUpper(t *testing.T) {
672+
testFunction(t, testFunctionConfig{
673+
Cases: []testFunctionCase{
674+
{
675+
`${upper("hello")}`,
676+
"HELLO",
677+
false,
678+
},
679+
680+
{
681+
`${upper("")}`,
682+
"",
683+
false,
684+
},
685+
686+
{
687+
`${upper()}`,
688+
nil,
689+
true,
690+
},
691+
},
692+
})
693+
}
694+
647695
type testFunctionConfig struct {
648696
Cases []testFunctionCase
649697
Vars map[string]ast.Variable

website/source/docs/configuration/interpolation.html.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ The supported built-in functions are:
131131
variable. The `map` parameter should be another variable, such
132132
as `var.amis`.
133133

134+
* `lower(string)` - returns a copy of the string with all Unicode letters mapped to their lower case.
135+
134136
* `replace(string, search, replace)` - Does a search and replace on the
135137
given string. All instances of `search` are replaced with the value
136138
of `replace`. If `search` is wrapped in forward slashes, it is treated
@@ -147,6 +149,8 @@ The supported built-in functions are:
147149
`a_resource_param = ["${split(",", var.CSV_STRING)}"]`.
148150
Example: `split(",", module.amod.server_ids)`
149151

152+
* `upper(string)` - returns a copy of the string with all Unicode letters mapped to their upper case.
153+
150154
## Templates
151155

152156
Long strings can be managed using templates. [Templates](/docs/providers/template/index.html) are [resources](/docs/configuration/resources.html) defined by a filename and some variables to use during interpolation. They have a computed `rendered` attribute containing the result.

0 commit comments

Comments
 (0)