Skip to content

Commit

Permalink
feat: Implemented support for str_iterative_replace. (#31)
Browse files Browse the repository at this point in the history
* feat: str_iterative_replace.
* refactor: Implemented a new type. Refactored all other code to match.
  • Loading branch information
skyzyx authored Oct 20, 2023
1 parent 14356ab commit 194dde1
Show file tree
Hide file tree
Showing 16 changed files with 615 additions and 10 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,7 @@ issues:

- linters:
- tagliatelle
- gofumpt
source: lint:allow_format

- text: (is unused)
Expand Down
12 changes: 12 additions & 0 deletions bats/str_iterative_replace.bats.sh
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}' ]]
}
10 changes: 5 additions & 5 deletions bats/tfschema_listing.bats.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
[[ ${lines[0]} == "corefunc_env_ensure" ]]
[[ ${lines[1]} == "corefunc_str_camel" ]]
[[ ${lines[2]} == "corefunc_str_constant" ]]
[[ ${lines[3]} == "corefunc_str_kebab" ]]
[[ ${lines[4]} == "corefunc_str_pascal" ]]
[[ ${lines[5]} == "corefunc_str_snake" ]]
[[ ${lines[6]} == "corefunc_str_truncate_label" ]]

[[ ${lines[3]} == "corefunc_str_iterative_replace" ]]
[[ ${lines[4]} == "corefunc_str_kebab" ]]
[[ ${lines[5]} == "corefunc_str_pascal" ]]
[[ ${lines[6]} == "corefunc_str_snake" ]]
[[ ${lines[7]} == "corefunc_str_truncate_label" ]]
}
5 changes: 3 additions & 2 deletions corefunc/env_ensure.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ variable is not set, or if its value doesn't match the expected patttern.
----
* name (string): The name of the environment variable to check.
- name (string): The name of the environment variable to check.
* pattern (*regexp.Regexp): The result of a call to `regexp.Compile()` or `regexp.MustCompile()`.
- pattern (*regexp.Regexp): The result of a call to `regexp.Compile()` or
`regexp.MustCompile()`.
*/
func EnvEnsure(name string, pattern ...*regexp.Regexp) error {
if os.Getenv(name) == "" {
Expand Down
44 changes: 44 additions & 0 deletions corefunc/str_iterative_replace.go
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
}
100 changes: 100 additions & 0 deletions corefunc/str_iterative_replace_test.go
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},
})
},
)
}
6 changes: 3 additions & 3 deletions corefunc/truncate_label.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ string.
----
* maxLength (int64): The maximum allowed length of the combined label.
- maxLength (int64): The maximum allowed length of the combined label.
* prefix (string): The prefix to prepend to the label.
- prefix (string): The prefix to prepend to the label.
* label (string): The label itself.
- label (string): The label itself.
----
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"`
}
1 change: 1 addition & 0 deletions corefuncprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
EnvEnsureDataSource,
StrCamelDataSource,
StrConstantDataSource,
StrIterativeReplaceDataSource,
StrKebabDataSource,
StrPascalDataSource,
StrSnakeDataSource,
Expand Down
Loading

0 comments on commit 194dde1

Please sign in to comment.