Skip to content

Commit 194dde1

Browse files
authored
feat: Implemented support for str_iterative_replace. (#31)
* feat: str_iterative_replace. * refactor: Implemented a new type. Refactored all other code to match.
1 parent 14356ab commit 194dde1

16 files changed

+615
-10
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,7 @@ issues:
837837

838838
- linters:
839839
- tagliatelle
840+
- gofumpt
840841
source: lint:allow_format
841842

842843
- text: (is unused)

bats/str_iterative_replace.bats.sh

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/env bats
2+
# https://bats-core.readthedocs.io/en/stable/writing-tests.html
3+
4+
@test "corefunc_str_iterative_replace: attrs" {
5+
run bash -c "tfschema data show -format=json corefunc_str_iterative_replace | jq -Mrc '.attributes[]'"
6+
7+
[ "$status" -eq 0 ]
8+
[[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
9+
[[ ${lines[1]} == '{"name":"replacements","type":"list(object({new=string,old=string}))","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
10+
[[ ${lines[2]} == '{"name":"string","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
11+
[[ ${lines[3]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
12+
}

bats/tfschema_listing.bats.sh

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
[[ ${lines[0]} == "corefunc_env_ensure" ]]
99
[[ ${lines[1]} == "corefunc_str_camel" ]]
1010
[[ ${lines[2]} == "corefunc_str_constant" ]]
11-
[[ ${lines[3]} == "corefunc_str_kebab" ]]
12-
[[ ${lines[4]} == "corefunc_str_pascal" ]]
13-
[[ ${lines[5]} == "corefunc_str_snake" ]]
14-
[[ ${lines[6]} == "corefunc_str_truncate_label" ]]
15-
11+
[[ ${lines[3]} == "corefunc_str_iterative_replace" ]]
12+
[[ ${lines[4]} == "corefunc_str_kebab" ]]
13+
[[ ${lines[5]} == "corefunc_str_pascal" ]]
14+
[[ ${lines[6]} == "corefunc_str_snake" ]]
15+
[[ ${lines[7]} == "corefunc_str_truncate_label" ]]
1616
}

corefunc/env_ensure.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ variable is not set, or if its value doesn't match the expected patttern.
3232
3333
----
3434
35-
* name (string): The name of the environment variable to check.
35+
- name (string): The name of the environment variable to check.
3636
37-
* pattern (*regexp.Regexp): The result of a call to `regexp.Compile()` or `regexp.MustCompile()`.
37+
- pattern (*regexp.Regexp): The result of a call to `regexp.Compile()` or
38+
`regexp.MustCompile()`.
3839
*/
3940
func EnvEnsure(name string, pattern ...*regexp.Regexp) error {
4041
if os.Getenv(name) == "" {

corefunc/str_iterative_replace.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefunc
16+
17+
import (
18+
"strings"
19+
20+
"github.com/northwood-labs/terraform-provider-corefunc/corefunc/types"
21+
)
22+
23+
/*
24+
StrIterativeReplace iterates over a list of replacements. This allows you to
25+
accept a list of replacements of unknown length from users, and apply them all
26+
in sequence. It is a wrapper around `strings.ReplaceAll()`.
27+
28+
----
29+
30+
- str (string): The string to which we apply the replacements.
31+
32+
- replacements ([]types.Replacement): A list of replacements to apply to
33+
the string, in sequence.
34+
*/
35+
func StrIterativeReplace(str string, replacements []types.Replacement) string {
36+
s := str
37+
38+
for i := range replacements {
39+
replacement := replacements[i]
40+
s = strings.ReplaceAll(s, replacement.Old, replacement.New)
41+
}
42+
43+
return s
44+
}
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefunc
16+
17+
import (
18+
"fmt"
19+
"testing"
20+
21+
"github.com/northwood-labs/terraform-provider-corefunc/corefunc/types"
22+
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"
23+
)
24+
25+
func ExampleStrIterativeReplace() {
26+
output := StrIterativeReplace(
27+
"This is a string for testing replacements. New Relic. Set-up.",
28+
[]types.Replacement{
29+
{Old: ".", New: ""},
30+
{Old: " ", New: "_"},
31+
{Old: "-", New: "_"},
32+
{Old: "New_Relic", New: "datadog"},
33+
{Old: "This", New: "this"},
34+
{Old: "Set_up", New: "setup"},
35+
},
36+
)
37+
38+
fmt.Println(output)
39+
40+
// Output:
41+
// this_is_a_string_for_testing_replacements_datadog_setup
42+
}
43+
44+
func TestStrIterativeReplace(t *testing.T) {
45+
for name, tc := range testfixtures.StrIterativeReplaceTestTable {
46+
t.Run(name, func(t *testing.T) {
47+
output := StrIterativeReplace(tc.Input, tc.Replacements)
48+
49+
if output != tc.Expected {
50+
t.Errorf("Expected %s, got %s", tc.Expected, output)
51+
}
52+
})
53+
}
54+
}
55+
56+
func BenchmarkStrIterativeReplace(b *testing.B) {
57+
b.ReportAllocs()
58+
for name, tc := range testfixtures.StrIterativeReplaceTestTable {
59+
b.Run(name, func(b *testing.B) {
60+
b.ResetTimer()
61+
for i := 0; i < b.N; i++ {
62+
_ = StrIterativeReplace(tc.Input, tc.Replacements)
63+
}
64+
})
65+
}
66+
}
67+
68+
func BenchmarkStrIterativeReplaceParallel(b *testing.B) {
69+
b.ReportAllocs()
70+
for name, tc := range testfixtures.StrIterativeReplaceTestTable {
71+
b.Run(name, func(b *testing.B) {
72+
b.ResetTimer()
73+
b.RunParallel(func(pb *testing.PB) {
74+
for pb.Next() {
75+
_ = StrIterativeReplace(tc.Input, tc.Replacements)
76+
}
77+
})
78+
})
79+
}
80+
}
81+
82+
func FuzzStrIterativeReplace(f *testing.F) {
83+
for _, tc := range testfixtures.StrIterativeReplaceTestTable {
84+
f.Add(tc.Input)
85+
f.Add(tc.Expected)
86+
87+
for _, r := range tc.Replacements {
88+
f.Add(r.Old)
89+
f.Add(r.New)
90+
}
91+
}
92+
93+
f.Fuzz(
94+
func(t *testing.T, in string) {
95+
_ = StrIterativeReplace(in, []types.Replacement{
96+
{Old: in, New: in},
97+
})
98+
},
99+
)
100+
}

corefunc/truncate_label.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ string.
3030
3131
----
3232
33-
* maxLength (int64): The maximum allowed length of the combined label.
33+
- maxLength (int64): The maximum allowed length of the combined label.
3434
35-
* prefix (string): The prefix to prepend to the label.
35+
- prefix (string): The prefix to prepend to the label.
3636
37-
* label (string): The label itself.
37+
- label (string): The label itself.
3838
3939
----
4040
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package types
16+
17+
// Replacement simplifies the creation of a `map[string]string` with validation.
18+
type Replacement struct {
19+
Old string `tfsdk:"old"`
20+
New string `tfsdk:"new"`
21+
}

corefuncprovider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
104104
EnvEnsureDataSource,
105105
StrCamelDataSource,
106106
StrConstantDataSource,
107+
StrIterativeReplaceDataSource,
107108
StrKebabDataSource,
108109
StrPascalDataSource,
109110
StrSnakeDataSource,

0 commit comments

Comments
 (0)