Skip to content

Commit 064b41c

Browse files
committed
add cut function support
1 parent f42f07d commit 064b41c

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### 1.6.0 (Next)
2-
- Add `product` function.
2+
- Add `product` and `cut` functions.
33
- Optimize data source `id` where possible (may cause superficial plan changes to existing states).
44

55
### 1.5.1
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Return the separated strings:
2+
data "stdlib_cut" "foobarbaz" {
3+
param = "foobarbaz"
4+
separator = "bar"
5+
}
6+
# before => "foo", after => "baz", found = true
7+
8+
# Return the separated strings with absent separator:
9+
data "stdlib_cut" "pizza" {
10+
param = "foobarbaz"
11+
separator = "pizza"
12+
}
13+
# before => "foobarbaz", after => "", found = false

stdlib/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func (*stdlibProvider) DataSources(_ context.Context) []func() datasource.DataSo
7979
slicefunc.NewProductDataSource,
8080
slicefunc.NewReplaceDataSource,
8181
slicefunc.NewSortListDataSource,
82+
stringfunc.NewCutDataSource,
8283
stringfunc.NewLastCharDataSource,
8384
}
8485
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package stringfunc_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
7+
8+
provider "github.com/mschuchard/terraform-provider-stdlib/stdlib"
9+
)
10+
11+
func TestAccCut(test *testing.T) {
12+
// invoke test
13+
resource.ParallelTest(test, resource.TestCase{
14+
ProtoV6ProviderFactories: provider.TestAccProtoV6ProviderFactories,
15+
Steps: []resource.TestStep{
16+
// test basic string cut
17+
{
18+
Config: `data "stdlib_cut" "test" {
19+
param = "foobarbaz"
20+
separator = "bar"
21+
}`,
22+
Check: resource.ComposeAggregateTestCheckFunc(
23+
// verify input param is stored correctly
24+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "param", "foobarbaz"),
25+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "separator", "bar"),
26+
// verify before, after, and found are stored correctly
27+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "before", "foo"),
28+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "after", "baz"),
29+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "found", "true"),
30+
// verify id stored correctly
31+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "id", "foobarbaz"),
32+
),
33+
},
34+
// test basic string cut absent separator
35+
{
36+
Config: `data "stdlib_cut" "test" {
37+
param = "foobarbaz"
38+
separator = "pizza"
39+
}`,
40+
Check: resource.ComposeAggregateTestCheckFunc(
41+
// verify input param is stored correctly
42+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "param", "foobarbaz"),
43+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "separator", "pizza"),
44+
// verify before, after, and found are stored correctly
45+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "before", "foobarbaz"),
46+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "after", ""),
47+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "found", "false"),
48+
// verify id stored correctly
49+
resource.TestCheckResourceAttr("data.stdlib_cut.test", "id", "foobarbaz"),
50+
),
51+
},
52+
},
53+
})
54+
}

0 commit comments

Comments
 (0)