Skip to content

Commit

Permalink
refactor: Implemented a new type. Refactored all other code to match.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Oct 20, 2023
1 parent 044e934 commit db4f47d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 43 deletions.
23 changes: 7 additions & 16 deletions corefunc/str_iterative_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 3 additions & 8 deletions corefunc/str_iterative_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "_"},
Expand Down Expand Up @@ -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},
})
},
Expand Down
21 changes: 21 additions & 0 deletions corefunc/types/str_iterative_replace_map.go
Original file line number Diff line number Diff line change
@@ -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"`
}
12 changes: 5 additions & 7 deletions corefuncprovider/str_iterative_replace_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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"`
}
)

Expand Down
20 changes: 8 additions & 12 deletions testfixtures/str_iterative_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
// <https://github.com/golang/go/wiki/TableDrivenTests>
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: "_"},
Expand Down

0 comments on commit db4f47d

Please sign in to comment.