diff --git a/corefunc/str_iterative_replace.go b/corefunc/str_iterative_replace.go index 3d888154..7e26ddc0 100644 --- a/corefunc/str_iterative_replace.go +++ b/corefunc/str_iterative_replace.go @@ -14,34 +14,25 @@ package corefunc -import "strings" +import ( + "strings" + + "github.com/northwood-labs/terraform-provider-corefunc/corefunc/types" +) /* StrIterativeReplace iterates over a list of replacements. This allows you to accept a list of replacements of unknown length from users, and apply them all in sequence. It is a wrapper around `strings.ReplaceAll()`. -Unfortunately, we need to apply the `tfsdk` text every time we invoke this -anonymous struct. As it turns out, Go treats this as part of its signature. Need -to see if an interface will improve this syntactic mess. - - []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - } - ---- - str (string): The string to which we apply the replacements. - - replacements ([]TypeStrIterativeReplace): A list of replacements to apply to + - replacements ([]types.Replacement): A list of replacements to apply to the string, in sequence. */ -func StrIterativeReplace(str string, replacements []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` -}, -) string { // lint:allow_format +func StrIterativeReplace(str string, replacements []types.Replacement) string { s := str for i := range replacements { diff --git a/corefunc/str_iterative_replace_test.go b/corefunc/str_iterative_replace_test.go index b2fc3e04..028c9ccc 100644 --- a/corefunc/str_iterative_replace_test.go +++ b/corefunc/str_iterative_replace_test.go @@ -18,16 +18,14 @@ import ( "fmt" "testing" + "github.com/northwood-labs/terraform-provider-corefunc/corefunc/types" "github.com/northwood-labs/terraform-provider-corefunc/testfixtures" ) func ExampleStrIterativeReplace() { output := StrIterativeReplace( "This is a string for testing replacements. New Relic. Set-up.", - []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - }{ + []types.Replacement{ {Old: ".", New: ""}, {Old: " ", New: "_"}, {Old: "-", New: "_"}, @@ -94,10 +92,7 @@ func FuzzStrIterativeReplace(f *testing.F) { f.Fuzz( func(t *testing.T, in string) { - _ = StrIterativeReplace(in, []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - }{ + _ = StrIterativeReplace(in, []types.Replacement{ {Old: in, New: in}, }) }, diff --git a/corefunc/types/str_iterative_replace_map.go b/corefunc/types/str_iterative_replace_map.go new file mode 100644 index 00000000..43269e19 --- /dev/null +++ b/corefunc/types/str_iterative_replace_map.go @@ -0,0 +1,21 @@ +// Copyright 2023, Ryan Parman +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package types + +// Replacement simplifies the creation of a `map[string]string` with validation. +type Replacement struct { + Old string `tfsdk:"old"` + New string `tfsdk:"new"` +} diff --git a/corefuncprovider/str_iterative_replace_data_source.go b/corefuncprovider/str_iterative_replace_data_source.go index 581ed4fc..8c4b900f 100644 --- a/corefuncprovider/str_iterative_replace_data_source.go +++ b/corefuncprovider/str_iterative_replace_data_source.go @@ -27,6 +27,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. @@ -41,13 +42,10 @@ type ( // strIterativeReplaceDataSourceModel maps the data source schema data. strIterativeReplaceDataSourceModel struct { - ID types.Int64 `tfsdk:"id"` - String types.String `tfsdk:"string"` - Value types.String `tfsdk:"value"` - Replacements []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - } `tfsdk:"replacements"` + ID types.Int64 `tfsdk:"id"` + String types.String `tfsdk:"string"` + Value types.String `tfsdk:"value"` + Replacements []cfTypes.Replacement `tfsdk:"replacements"` } ) diff --git a/testfixtures/str_iterative_replace.go b/testfixtures/str_iterative_replace.go index 7fd1d210..a8a0d4bc 100644 --- a/testfixtures/str_iterative_replace.go +++ b/testfixtures/str_iterative_replace.go @@ -14,23 +14,19 @@ package testfixtures // lint:no_dupe -// StrIterativeReplaceTestTable is used by both the standard Go tests and also the -// Terraform acceptance tests. +import "github.com/northwood-labs/terraform-provider-corefunc/corefunc/types" + +// StrIterativeReplaceTestTable is used by both the standard Go tests and also +// the Terraform acceptance tests. // var StrIterativeReplaceTestTable = map[string]struct { // lint:no_dupe - Replacements []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - } - Input string - Expected string + Replacements []types.Replacement + Input string + Expected string }{ "replacements": { Input: "This is a string for testing replacements. New Relic. Set-up.", - Replacements: []struct { - Old string `tfsdk:"old"` - New string `tfsdk:"new"` - }{ + Replacements: []types.Replacement{ {Old: ".", New: ""}, {Old: " ", New: "_"}, {Old: "-", New: "_"},