-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented support for
str_iterative_replace
. (#31)
* feat: str_iterative_replace. * refactor: Implemented a new type. Refactored all other code to match.
- Loading branch information
Showing
16 changed files
with
615 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -837,6 +837,7 @@ issues: | |
|
||
- linters: | ||
- tagliatelle | ||
- gofumpt | ||
source: lint:allow_format | ||
|
||
- text: (is unused) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env bats | ||
# https://bats-core.readthedocs.io/en/stable/writing-tests.html | ||
|
||
@test "corefunc_str_iterative_replace: attrs" { | ||
run bash -c "tfschema data show -format=json corefunc_str_iterative_replace | jq -Mrc '.attributes[]'" | ||
|
||
[ "$status" -eq 0 ] | ||
[[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]] | ||
[[ ${lines[1]} == '{"name":"replacements","type":"list(object({new=string,old=string}))","required":true,"optional":false,"computed":false,"sensitive":false}' ]] | ||
[[ ${lines[2]} == '{"name":"string","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]] | ||
[[ ${lines[3]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 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 corefunc | ||
|
||
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()`. | ||
---- | ||
- str (string): The string to which we apply the replacements. | ||
- replacements ([]types.Replacement): A list of replacements to apply to | ||
the string, in sequence. | ||
*/ | ||
func StrIterativeReplace(str string, replacements []types.Replacement) string { | ||
s := str | ||
|
||
for i := range replacements { | ||
replacement := replacements[i] | ||
s = strings.ReplaceAll(s, replacement.Old, replacement.New) | ||
} | ||
|
||
return s | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// 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 corefunc | ||
|
||
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.", | ||
[]types.Replacement{ | ||
{Old: ".", New: ""}, | ||
{Old: " ", New: "_"}, | ||
{Old: "-", New: "_"}, | ||
{Old: "New_Relic", New: "datadog"}, | ||
{Old: "This", New: "this"}, | ||
{Old: "Set_up", New: "setup"}, | ||
}, | ||
) | ||
|
||
fmt.Println(output) | ||
|
||
// Output: | ||
// this_is_a_string_for_testing_replacements_datadog_setup | ||
} | ||
|
||
func TestStrIterativeReplace(t *testing.T) { | ||
for name, tc := range testfixtures.StrIterativeReplaceTestTable { | ||
t.Run(name, func(t *testing.T) { | ||
output := StrIterativeReplace(tc.Input, tc.Replacements) | ||
|
||
if output != tc.Expected { | ||
t.Errorf("Expected %s, got %s", tc.Expected, output) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkStrIterativeReplace(b *testing.B) { | ||
b.ReportAllocs() | ||
for name, tc := range testfixtures.StrIterativeReplaceTestTable { | ||
b.Run(name, func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
_ = StrIterativeReplace(tc.Input, tc.Replacements) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func BenchmarkStrIterativeReplaceParallel(b *testing.B) { | ||
b.ReportAllocs() | ||
for name, tc := range testfixtures.StrIterativeReplaceTestTable { | ||
b.Run(name, func(b *testing.B) { | ||
b.ResetTimer() | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = StrIterativeReplace(tc.Input, tc.Replacements) | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func FuzzStrIterativeReplace(f *testing.F) { | ||
for _, tc := range testfixtures.StrIterativeReplaceTestTable { | ||
f.Add(tc.Input) | ||
f.Add(tc.Expected) | ||
|
||
for _, r := range tc.Replacements { | ||
f.Add(r.Old) | ||
f.Add(r.New) | ||
} | ||
} | ||
|
||
f.Fuzz( | ||
func(t *testing.T, in string) { | ||
_ = StrIterativeReplace(in, []types.Replacement{ | ||
{Old: in, New: in}, | ||
}) | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.