Skip to content

Commit

Permalink
feat: Finish implementing str_iterative_replace().
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Mar 20, 2024
1 parent 0570546 commit 8481fbe
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion corefuncprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (p *coreFuncProvider) Functions(ctx context.Context) []func() function.Func
StrCamelFunction,
StrConstantFunction,
StrKebabFunction,
// StrIterativeReplaceFunction,
StrIterativeReplaceFunction,
StrLeftpadFunction,
StrPascalFunction,
StrSnakeFunction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/lithammer/dedent"
"github.com/northwood-labs/terraform-provider-corefunc/corefunc"
cftypes "github.com/northwood-labs/terraform-provider-corefunc/corefunc/types"
)

// Ensure the implementation satisfies the expected interfaces.
Expand Down Expand Up @@ -85,8 +86,10 @@ func (f *strIterativeReplaceFunction) Definition(
` key. ` + "`" + `old` + "`" + ` represents the existing string to be replaced, and ` + "`" +
`new` + "`" + ` represents the replacement string.
`)),
ElementType: types.MapType{},
// https://discuss.hashicorp.com/t/provider-functions-equivalent-to-schema-listnestedattribute/63585
// https://discuss.hashicorp.com/t/provider-functions-equivalent-to-schema-listnestedattribute/63585
ElementType: types.MapType{
ElemType: types.StringType,
},
},
},
Return: function.StringReturn{},
Expand All @@ -98,17 +101,32 @@ func (f *strIterativeReplaceFunction) Definition(
func (f *strIterativeReplaceFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
tflog.Debug(ctx, "Starting StrIterativeReplace Function Run method.")

var str string
err := req.Arguments.Get(ctx, &str)
var (
str string
replacements []map[string]string
)

err := req.Arguments.Get(ctx, &str, &replacements)

resp.Error = function.ConcatFuncErrors(err)
if resp.Error != nil {
return
}

// Remap!
repls := make([]cftypes.Replacement, len(replacements))

for i := range replacements {
r := replacements[i]
repls[i] = cftypes.Replacement{
Old: r["old"],
New: r["new"],
}
}

value := corefunc.StrIterativeReplace(
str,
// opts,
repls,
)

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
)

func TestAccStrIterativeReplaceFunction(t *testing.T) {
t.Parallel()
t.Parallel()

funcName := traceFuncName()

Expand Down Expand Up @@ -68,7 +68,7 @@ func TestAccStrIterativeReplaceFunction(t *testing.T) {
{
Config: providerConfig + buf.String(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckOutput("@TODO", tc.Expected),
resource.TestCheckOutput("replacements", tc.Expected),
),
// ExpectError: tc.expectedError,
},
Expand Down
31 changes: 31 additions & 0 deletions terratest/functions/str_replace.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
output "str_iterative_replace_fn" {
value = provider::corefunc::str_iterative_replace(
"This is a string for testing replacements. New Relic. Set-up.",
[
{
old = "."
new = ""
},
{
old = " "
new = "_"
},
{
old = "-"
new = "_"
},
{
old = "New_Relic"
new = "datadog"
},
{
old = "This"
new = "this"
},
{
old = "Set_up"
new = "setup"
},
]
)
}

0 comments on commit 8481fbe

Please sign in to comment.