lang/funcs: Add more `trim*` functions (#23016)
* lang/funcs: Add `trim*` functions
This commit is contained in:
parent
04c5547766
commit
91100c003c
|
@ -123,7 +123,7 @@ var SplitFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// ChompFunc constructions a function that removes newline characters at the end of a string.
|
||||
// ChompFunc constructs a function that removes newline characters at the end of a string.
|
||||
var ChompFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
|
@ -138,7 +138,7 @@ var ChompFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// IndentFunc constructions a function that adds a given number of spaces to the
|
||||
// IndentFunc constructs a function that adds a given number of spaces to the
|
||||
// beginnings of all but the first line in a given multi-line string.
|
||||
var IndentFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -163,7 +163,7 @@ var IndentFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// ReplaceFunc constructions a function that searches a given string for another
|
||||
// ReplaceFunc constructs a function that searches a given string for another
|
||||
// given substring, and replaces each occurence with a given replacement string.
|
||||
var ReplaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -201,7 +201,7 @@ var ReplaceFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// TitleFunc constructions a function that converts the first letter of each word
|
||||
// TitleFunc constructs a function that converts the first letter of each word
|
||||
// in the given string to uppercase.
|
||||
var TitleFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -216,7 +216,7 @@ var TitleFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// TrimSpaceFunc constructions a function that removes any space characters from
|
||||
// TrimSpaceFunc constructs a function that removes any space characters from
|
||||
// the start and end of the given string.
|
||||
var TrimSpaceFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
|
@ -231,6 +231,69 @@ var TrimSpaceFunc = function.New(&function.Spec{
|
|||
},
|
||||
})
|
||||
|
||||
// TrimFunc constructs a function that removes the specified characters from
|
||||
// the start and end of the given string.
|
||||
var TrimFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "cutset",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.Trim(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimPrefixFunc constructs a function that removes the specified characters from
|
||||
// the start the given string.
|
||||
var TrimPrefixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "prefix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
prefix := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimPrefix(str, prefix)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// TrimSuffixFunc constructs a function that removes the specified characters from
|
||||
// the end of the given string.
|
||||
var TrimSuffixFunc = function.New(&function.Spec{
|
||||
Params: []function.Parameter{
|
||||
{
|
||||
Name: "str",
|
||||
Type: cty.String,
|
||||
},
|
||||
{
|
||||
Name: "suffix",
|
||||
Type: cty.String,
|
||||
},
|
||||
},
|
||||
Type: function.StaticReturnType(cty.String),
|
||||
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
|
||||
str := args[0].AsString()
|
||||
cutset := args[1].AsString()
|
||||
return cty.StringVal(strings.TrimSuffix(str, cutset)), nil
|
||||
},
|
||||
})
|
||||
|
||||
// Join concatenates together the string elements of one or more lists with a
|
||||
// given separator.
|
||||
func Join(sep cty.Value, lists ...cty.Value) (cty.Value, error) {
|
||||
|
@ -278,3 +341,18 @@ func Title(str cty.Value) (cty.Value, error) {
|
|||
func TrimSpace(str cty.Value) (cty.Value, error) {
|
||||
return TrimSpaceFunc.Call([]cty.Value{str})
|
||||
}
|
||||
|
||||
// Trim removes the specified characters from the start and end of the given string.
|
||||
func Trim(str, cutset cty.Value) (cty.Value, error) {
|
||||
return TrimFunc.Call([]cty.Value{str, cutset})
|
||||
}
|
||||
|
||||
// TrimPrefix removes the specified prefix from the start of the given string.
|
||||
func TrimPrefix(str, prefix cty.Value) (cty.Value, error) {
|
||||
return TrimPrefixFunc.Call([]cty.Value{str, prefix})
|
||||
}
|
||||
|
||||
// TrimSuffix removes the specified suffix from the end of the given string.
|
||||
func TrimSuffix(str, suffix cty.Value) (cty.Value, error) {
|
||||
return TrimSuffixFunc.Call([]cty.Value{str, suffix})
|
||||
}
|
||||
|
|
|
@ -506,3 +506,126 @@ func TestTrimSpace(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrim(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Cutset cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("!? test ?!"),
|
||||
cty.StringVal("?!"),
|
||||
cty.StringVal(" test "),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("test"),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trim(%#v, %#v)", test.String, test.Cutset), func(t *testing.T) {
|
||||
got, err := Trim(test.String, test.Cutset)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimPrefix(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Prefix cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("helloworld"),
|
||||
cty.StringVal("hello"),
|
||||
cty.StringVal("world"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("..test..."),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trimprefix(%#v, %#v)", test.String, test.Prefix), func(t *testing.T) {
|
||||
got, err := TrimPrefix(test.String, test.Prefix)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrimSuffix(t *testing.T) {
|
||||
tests := []struct {
|
||||
String cty.Value
|
||||
Suffix cty.Value
|
||||
Want cty.Value
|
||||
Err bool
|
||||
}{
|
||||
{
|
||||
cty.StringVal("helloworld"),
|
||||
cty.StringVal("world"),
|
||||
cty.StringVal("hello"),
|
||||
false,
|
||||
},
|
||||
{
|
||||
cty.StringVal("...test..."),
|
||||
cty.StringVal("."),
|
||||
cty.StringVal("...test.."),
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(fmt.Sprintf("trimright(%#v, %#v)", test.String, test.Suffix), func(t *testing.T) {
|
||||
got, err := TrimSuffix(test.String, test.Suffix)
|
||||
|
||||
if test.Err {
|
||||
if err == nil {
|
||||
t.Fatal("succeeded; want error")
|
||||
}
|
||||
return
|
||||
} else if err != nil {
|
||||
t.Fatalf("unexpected error: %s", err)
|
||||
}
|
||||
|
||||
if !got.RawEquals(test.Want) {
|
||||
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
---
|
||||
layout: "functions"
|
||||
page_title: "trim - Functions - Configuration Language"
|
||||
sidebar_current: "docs-funcs-string-trim"
|
||||
description: |-
|
||||
The trim function removes the specified characters from the start and end of
|
||||
a given string.
|
||||
---
|
||||
|
||||
# `trim` Function
|
||||
|
||||
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
||||
earlier, see
|
||||
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
||||
|
||||
`trim` removes the specified characters from the start and end of the given
|
||||
string.
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
> trim("?!hello?!", "!?")
|
||||
hello
|
||||
```
|
||||
|
||||
## Related Functions
|
||||
|
||||
* [`trimprefix`](./trimprefix.html) removes a word from the start of a string.
|
||||
* [`trimsuffix`](./trimsuffix.html) removes a word from the end of a string.
|
||||
* [`trimspace`](./trimspace.html) removes all types of whitespace from
|
||||
both the start and the end of a string.
|
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
layout: "functions"
|
||||
page_title: "trimprefix - Functions - Configuration Language"
|
||||
sidebar_current: "docs-funcs-string-trimprefix"
|
||||
description: |-
|
||||
The trimprefix function removes the specified prefix from the start of a
|
||||
given string.
|
||||
---
|
||||
|
||||
# `trimprefix` Function
|
||||
|
||||
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
||||
earlier, see
|
||||
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
||||
|
||||
`trimprefix` removes the specified prefix from the start of the given string.
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
> trimprefix("helloworld", "hello")
|
||||
world
|
||||
```
|
||||
|
||||
## Related Functions
|
||||
|
||||
* [`trim`](./trim.html) removes characters at the start and end of a string.
|
||||
* [`trimsuffix`](./trimsuffix.html) removes a word from the end of a string.
|
||||
* [`trimspace`](./trimspace.html) removes all types of whitespace from
|
||||
both the start and the end of a string.
|
|
@ -0,0 +1,30 @@
|
|||
---
|
||||
layout: "functions"
|
||||
page_title: "trimsuffix - Functions - Configuration Language"
|
||||
sidebar_current: "docs-funcs-string-trimsuffix"
|
||||
description: |-
|
||||
The trimsuffix function removes the specified suffix from the end of a
|
||||
given string.
|
||||
---
|
||||
|
||||
# `trimsuffix` Function
|
||||
|
||||
-> **Note:** This page is about Terraform 0.12 and later. For Terraform 0.11 and
|
||||
earlier, see
|
||||
[0.11 Configuration Language: Interpolation Syntax](../../configuration-0-11/interpolation.html).
|
||||
|
||||
`trimsuffix` removes the specified suffix from the end of the given string.
|
||||
|
||||
## Examples
|
||||
|
||||
```
|
||||
> trimsuffix("helloworld", "world")
|
||||
hello
|
||||
```
|
||||
|
||||
## Related Functions
|
||||
|
||||
* [`trim`](./trim.html) removes characters at the start and end of a string.
|
||||
* [`trimprefix`](./trimprefix.html) removes a word from the start of a string.
|
||||
* [`trimspace`](./trimspace.html) removes all types of whitespace from
|
||||
both the start and the end of a string.
|
|
@ -111,6 +111,18 @@
|
|||
<a href="/docs/configuration/functions/title.html">title</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/trim.html">trim</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/trimprefix.html">trimprefix</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/trimsuffix.html">trimsuffix</a>
|
||||
</li>
|
||||
|
||||
<li>
|
||||
<a href="/docs/configuration/functions/trimspace.html">trimspace</a>
|
||||
</li>
|
||||
|
|
Loading…
Reference in New Issue