diff --git a/.licenses.cache.json b/.licenses.cache.json index c5982052..db07bec7 100644 --- a/.licenses.cache.json +++ b/.licenses.cache.json @@ -46,15 +46,15 @@ ], "Indirect": true, "DependsOn": [ - "golang.org/x/crypto@v0.3.0", "github.com/google/uuid@v1.1.1", - "github.com/shopspring/decimal@v1.2.0", - "github.com/huandu/xstrings@v1.3.3", "github.com/imdario/mergo@v0.3.11", - "github.com/mitchellh/copystructure@v1.0.0", "github.com/spf13/cast@v1.3.1", "github.com/Masterminds/goutils@v1.1.1", - "github.com/Masterminds/semver/v3@v3.2.0" + "github.com/Masterminds/semver/v3@v3.2.0", + "github.com/huandu/xstrings@v1.3.3", + "github.com/mitchellh/copystructure@v1.0.0", + "github.com/shopspring/decimal@v1.2.0", + "golang.org/x/crypto@v0.3.0" ], "Layer": {} }, @@ -158,8 +158,8 @@ ], "Indirect": true, "DependsOn": [ - "github.com/spf13/pflag@v1.0.5", - "github.com/inconshreveable/mousetrap@v1.1.0" + "github.com/inconshreveable/mousetrap@v1.1.0", + "github.com/spf13/pflag@v1.0.5" ], "Layer": {} }, @@ -216,16 +216,16 @@ ], "Indirect": true, "DependsOn": [ + "golang.org/x/crypto@v0.15.0", + "github.com/Masterminds/goutils@v1.1.1", + "github.com/huandu/xstrings@v1.3.2", "github.com/imdario/mergo@v0.3.15", "github.com/shopspring/decimal@v1.3.1", "github.com/spf13/cast@v1.5.0", - "github.com/Masterminds/goutils@v1.1.1", + "github.com/stretchr/testify@v1.8.4", "github.com/Masterminds/semver/v3@v3.1.1", "github.com/google/uuid@v1.3.1", - "github.com/huandu/xstrings@v1.3.2", - "github.com/mitchellh/copystructure@v1.2.0", - "github.com/stretchr/testify@v1.8.4", - "golang.org/x/crypto@v0.15.0" + "github.com/mitchellh/copystructure@v1.2.0" ], "Layer": {} }, @@ -342,8 +342,8 @@ ], "Indirect": true, "DependsOn": [ - "google.golang.org/protobuf@v1.31.0", - "github.com/google/go-cmp@v0.6.0" + "github.com/google/go-cmp@v0.6.0", + "google.golang.org/protobuf@v1.31.0" ], "Layer": {} }, @@ -374,9 +374,9 @@ "MIT" ], "DependsOn": [ - "golang.org/x/sys@v0.14.0", "github.com/stretchr/testify@v1.8.4", - "github.com/xo/terminfo@v0.0.0-20220910002029-abceb7e1c41e" + "github.com/xo/terminfo@v0.0.0-20220910002029-abceb7e1c41e", + "golang.org/x/sys@v0.14.0" ], "Layer": {} }, @@ -388,9 +388,9 @@ "MIT" ], "DependsOn": [ + "github.com/fatih/color@v1.16.0", "github.com/hexops/gotextdiff@v1.0.3", - "github.com/stretchr/testify@v1.8.4", - "github.com/fatih/color@v1.16.0" + "github.com/stretchr/testify@v1.8.4" ], "Layer": {} }, @@ -437,9 +437,9 @@ ], "Indirect": true, "DependsOn": [ - "github.com/google/go-cmp@v0.6.0", "github.com/vmihailenco/msgpack@v4.0.4+incompatible", - "golang.org/x/text@v0.14.0" + "golang.org/x/text@v0.14.0", + "github.com/google/go-cmp@v0.6.0" ], "Layer": {} }, @@ -452,10 +452,10 @@ ], "Indirect": true, "DependsOn": [ + "github.com/mattn/go-isatty@v0.0.20", "github.com/stretchr/testify@v1.8.4", "github.com/fatih/color@v1.16.0", - "github.com/mattn/go-colorable@v0.1.13", - "github.com/mattn/go-isatty@v0.0.20" + "github.com/mattn/go-colorable@v0.1.13" ], "Layer": {} }, @@ -481,13 +481,13 @@ ], "Indirect": true, "DependsOn": [ - "github.com/golang/protobuf@v1.5.3", + "google.golang.org/grpc@v1.59.0", + "github.com/hashicorp/yamux@v0.1.1", + "github.com/mitchellh/go-testing-interface@v1.14.1", "github.com/oklog/run@v1.1.0", "google.golang.org/protobuf@v1.31.0", - "github.com/hashicorp/yamux@v0.1.1", - "google.golang.org/grpc@v1.59.0", - "github.com/hashicorp/go-hclog@v1.5.0", - "github.com/mitchellh/go-testing-interface@v1.14.1" + "github.com/golang/protobuf@v1.5.3", + "github.com/hashicorp/go-hclog@v1.5.0" ], "Layer": {} }, @@ -520,15 +520,15 @@ ], "Indirect": true, "DependsOn": [ - "github.com/mitchellh/cli@v1.1.5", - "github.com/google/go-cmp@v0.6.0", - "github.com/hashicorp/go-checkpoint@v0.5.0", + "github.com/hashicorp/logutils@v1.0.0", "github.com/hashicorp/go-multierror@v1.1.1", "golang.org/x/mod@v0.14.0", "github.com/ProtonMail/go-crypto@v0.0.0-20230923063757-afb1ddc0824c", + "github.com/hashicorp/go-checkpoint@v0.5.0", "github.com/hashicorp/go-cleanhttp@v0.5.2", + "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/go-version@v1.6.0", - "github.com/hashicorp/logutils@v1.0.0" + "github.com/mitchellh/cli@v1.1.5" ], "Layer": {} }, @@ -542,13 +542,13 @@ "Indirect": true, "DependsOn": [ "github.com/agext/levenshtein@v1.2.3", - "github.com/google/go-cmp@v0.6.0", - "github.com/spf13/pflag@v1.0.5", - "github.com/zclconf/go-cty@v1.14.1", "github.com/davecgh/go-spew@v1.1.1", - "github.com/mitchellh/go-wordwrap@v1.0.1", + "github.com/spf13/pflag@v1.0.5", "github.com/apparentlymart/go-textseg/v15@v15.0.0", - "golang.org/x/crypto@v0.15.0" + "github.com/google/go-cmp@v0.6.0", + "golang.org/x/crypto@v0.15.0", + "github.com/mitchellh/go-wordwrap@v1.0.1", + "github.com/zclconf/go-cty@v1.14.1" ], "Layer": {} }, @@ -571,11 +571,11 @@ ], "Indirect": true, "DependsOn": [ - "github.com/hashicorp/go-version@v1.6.0", - "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/terraform-json@v0.18.0", - "github.com/hashicorp/hc-install@v0.6.1", - "github.com/zclconf/go-cty@v1.14.1" + "github.com/zclconf/go-cty@v1.14.1", + "github.com/google/go-cmp@v0.6.0", + "github.com/hashicorp/go-version@v1.6.0", + "github.com/hashicorp/hc-install@v0.6.1" ], "Layer": {} }, @@ -588,11 +588,11 @@ ], "Indirect": true, "DependsOn": [ + "github.com/davecgh/go-spew@v1.1.1", "github.com/zclconf/go-cty@v1.14.1", "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/go-version@v1.6.0", - "github.com/mitchellh/copystructure@v1.2.0", - "github.com/davecgh/go-spew@v1.1.1" + "github.com/mitchellh/copystructure@v1.2.0" ], "Layer": {} }, @@ -604,17 +604,17 @@ "MPL-2.0" ], "DependsOn": [ - "golang.org/x/text@v0.14.0", - "github.com/hashicorp/go-version@v1.6.0", - "github.com/hashicorp/terraform-exec@v0.19.0", "github.com/mattn/go-colorable@v0.1.13", - "github.com/russross/blackfriday@v1.6.0", "github.com/zclconf/go-cty@v1.14.1", - "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/terraform-json@v0.18.0", "github.com/mitchellh/cli@v1.1.5", - "golang.org/x/exp@v0.0.0-20230626212559-97b1e661b5df", - "github.com/hashicorp/hc-install@v0.6.1" + "github.com/hashicorp/terraform-exec@v0.19.0", + "github.com/hashicorp/go-version@v1.6.0", + "github.com/russross/blackfriday@v1.6.0", + "golang.org/x/text@v0.14.0", + "github.com/google/go-cmp@v0.6.0", + "github.com/hashicorp/hc-install@v0.6.1", + "golang.org/x/exp@v0.0.0-20230626212559-97b1e661b5df" ], "Layer": {} }, @@ -627,8 +627,8 @@ ], "DependsOn": [ "github.com/google/go-cmp@v0.6.0", - "github.com/hashicorp/terraform-plugin-go@v0.19.1", - "github.com/hashicorp/terraform-plugin-log@v0.9.0" + "github.com/hashicorp/terraform-plugin-log@v0.9.0", + "github.com/hashicorp/terraform-plugin-go@v0.19.1" ], "Layer": {} }, @@ -640,9 +640,9 @@ "MPL-2.0" ], "DependsOn": [ + "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/terraform-plugin-framework@v1.4.2", - "github.com/hashicorp/terraform-plugin-go@v0.19.1", - "github.com/google/go-cmp@v0.6.0" + "github.com/hashicorp/terraform-plugin-go@v0.19.1" ], "Layer": {} }, @@ -654,16 +654,16 @@ "MPL-2.0" ], "DependsOn": [ - "github.com/hashicorp/go-hclog@v1.5.0", - "github.com/hashicorp/terraform-registry-address@v0.2.3", - "google.golang.org/protobuf@v1.31.0", - "github.com/hashicorp/go-plugin@v1.6.0", + "github.com/hashicorp/terraform-plugin-log@v0.9.0", + "github.com/google/go-cmp@v0.6.0", "github.com/vmihailenco/msgpack/v5@v5.4.1", "google.golang.org/grpc@v1.59.0", - "github.com/google/go-cmp@v0.6.0", + "github.com/hashicorp/go-hclog@v1.5.0", + "github.com/hashicorp/go-plugin@v1.6.0", "github.com/hashicorp/go-uuid@v1.0.3", - "github.com/hashicorp/terraform-plugin-log@v0.9.0", - "github.com/mitchellh/go-testing-interface@v1.14.1" + "github.com/hashicorp/terraform-registry-address@v0.2.3", + "github.com/mitchellh/go-testing-interface@v1.14.1", + "google.golang.org/protobuf@v1.31.0" ], "Layer": {} }, @@ -675,9 +675,9 @@ "MPL-2.0" ], "DependsOn": [ - "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/go-hclog@v1.5.0", - "github.com/mitchellh/go-testing-interface@v1.14.1" + "github.com/mitchellh/go-testing-interface@v1.14.1", + "github.com/google/go-cmp@v0.6.0" ], "Layer": {} }, @@ -689,25 +689,25 @@ "MPL-2.0" ], "DependsOn": [ - "github.com/hashicorp/go-hclog@v1.5.0", - "github.com/hashicorp/go-version@v1.6.0", - "github.com/hashicorp/terraform-plugin-log@v0.9.0", - "github.com/google/go-cmp@v0.6.0", - "github.com/mitchellh/copystructure@v1.2.0", - "github.com/hashicorp/terraform-plugin-go@v0.19.1", - "github.com/hashicorp/go-plugin@v1.6.0", "github.com/mitchellh/go-testing-interface@v1.14.1", - "github.com/hashicorp/go-uuid@v1.0.3", - "golang.org/x/crypto@v0.15.0", - "github.com/hashicorp/hc-install@v0.6.1", + "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/hcl/v2@v2.19.1", - "github.com/hashicorp/terraform-exec@v0.19.0", + "github.com/hashicorp/terraform-plugin-log@v0.9.0", + "github.com/hashicorp/go-hclog@v1.5.0", + "github.com/hashicorp/go-uuid@v1.0.3", "github.com/hashicorp/terraform-json@v0.18.0", - "github.com/zclconf/go-cty@v1.14.1", - "github.com/mitchellh/mapstructure@v1.5.0", - "github.com/hashicorp/go-cty@v1.4.1-0.20200414143053-d3edf31b6320", + "github.com/hashicorp/go-plugin@v1.6.0", "github.com/hashicorp/logutils@v1.0.0", - "github.com/mitchellh/reflectwalk@v1.0.2" + "github.com/hashicorp/terraform-exec@v0.19.0", + "github.com/hashicorp/go-cty@v1.4.1-0.20200414143053-d3edf31b6320", + "github.com/hashicorp/go-version@v1.6.0", + "github.com/hashicorp/hc-install@v0.6.1", + "github.com/mitchellh/mapstructure@v1.5.0", + "github.com/hashicorp/terraform-plugin-go@v0.19.1", + "github.com/mitchellh/reflectwalk@v1.0.2", + "github.com/zclconf/go-cty@v1.14.1", + "github.com/mitchellh/copystructure@v1.2.0", + "golang.org/x/crypto@v0.15.0" ], "Layer": {} }, @@ -720,9 +720,9 @@ ], "Indirect": true, "DependsOn": [ + "golang.org/x/net@v0.18.0", "github.com/google/go-cmp@v0.6.0", - "github.com/hashicorp/terraform-svchost@v0.1.1", - "golang.org/x/net@v0.18.0" + "github.com/hashicorp/terraform-svchost@v0.1.1" ], "Layer": {} }, @@ -735,11 +735,11 @@ ], "Indirect": true, "DependsOn": [ - "github.com/google/go-cmp@v0.6.0", "github.com/hashicorp/go-cleanhttp@v0.5.2", - "github.com/hashicorp/go-version@v1.6.0", "github.com/zclconf/go-cty@v1.14.1", - "golang.org/x/net@v0.18.0" + "golang.org/x/net@v0.18.0", + "github.com/google/go-cmp@v0.6.0", + "github.com/hashicorp/go-version@v1.6.0" ], "Layer": {} }, @@ -840,12 +840,12 @@ ], "Indirect": true, "DependsOn": [ + "github.com/Masterminds/sprig/v3@v3.2.2", + "github.com/armon/go-radix@v1.0.0", "github.com/bgentry/speakeasy@v0.1.0", "github.com/fatih/color@v1.16.0", "github.com/mattn/go-isatty@v0.0.20", - "github.com/posener/complete@v1.2.3", - "github.com/Masterminds/sprig/v3@v3.2.2", - "github.com/armon/go-radix@v1.0.0" + "github.com/posener/complete@v1.2.3" ], "Layer": {} }, @@ -1177,9 +1177,9 @@ ], "Indirect": true, "DependsOn": [ - "google.golang.org/protobuf@v1.31.0", "github.com/golang/protobuf@v1.5.3", - "golang.org/x/text@v0.14.0" + "golang.org/x/text@v0.14.0", + "google.golang.org/protobuf@v1.31.0" ], "Layer": {} }, @@ -1206,12 +1206,12 @@ "Indirect": true, "DependsOn": [ "github.com/golang/protobuf@v1.5.3", - "golang.org/x/sys@v0.14.0", - "google.golang.org/genproto/googleapis/rpc@v0.0.0-20231120223509-83a465c0220f", "github.com/google/go-cmp@v0.6.0", - "golang.org/x/net@v0.18.0", + "google.golang.org/genproto/googleapis/rpc@v0.0.0-20231120223509-83a465c0220f", "github.com/google/uuid@v1.3.1", - "google.golang.org/protobuf@v1.31.0" + "golang.org/x/sys@v0.14.0", + "google.golang.org/protobuf@v1.31.0", + "golang.org/x/net@v0.18.0" ], "Layer": {} }, diff --git a/acc-coverage.png b/acc-coverage.png index 14b3fce0..8bf0e79b 100644 Binary files a/acc-coverage.png and b/acc-coverage.png differ diff --git a/acc-coverage.svg b/acc-coverage.svg index 881258d6..30e3fb7c 100644 --- a/acc-coverage.svg +++ b/acc-coverage.svg @@ -7,7 +7,7 @@ > - + - + env_ensure_data_source.go @@ -33,12 +33,12 @@ - + helpers.go @@ -46,12 +46,25 @@ - + int_leftpad_data_source.go + + + + + + + +provider.go @@ -59,12 +72,12 @@ - + str_camel_data_source.go @@ -72,12 +85,12 @@ - + str_constant_data_source.go @@ -85,12 +98,12 @@ - + str_iterative_replace_data_source.go @@ -98,12 +111,12 @@ - + str_kebab_data_source.go @@ -111,12 +124,25 @@ - + + +str_leftpad_data_source.go + + + + + + str_pascal_data_source.go @@ -124,12 +150,12 @@ - + str_snake_data_source.go @@ -137,12 +163,12 @@ - + truncate_label_data_source.go diff --git a/bats/int_leftpad.bats.sh b/bats/int_leftpad.bats.sh new file mode 100644 index 00000000..212def9b --- /dev/null +++ b/bats/int_leftpad.bats.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bats +# https://bats-core.readthedocs.io/en/stable/writing-tests.html + +@test "corefunc_int_leftpad: attrs" { + run bash -c "tfschema data show -format=json corefunc_int_leftpad | jq -Mrc '.attributes[]'" + + [ "$status" -eq 0 ] + [[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]] + [[ ${lines[1]} == '{"name":"num","type":"number","required":true,"optional":false,"computed":false,"sensitive":false}' ]] + [[ ${lines[2]} == '{"name":"pad_width","type":"number","required":true,"optional":false,"computed":false,"sensitive":false}' ]] + [[ ${lines[3]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]] +} diff --git a/bats/str_leftpad.bats.sh b/bats/str_leftpad.bats.sh new file mode 100644 index 00000000..01cb7760 --- /dev/null +++ b/bats/str_leftpad.bats.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bats +# https://bats-core.readthedocs.io/en/stable/writing-tests.html + +@test "corefunc_str_leftpad: attrs" { + run bash -c "tfschema data show -format=json corefunc_str_leftpad | jq -Mrc '.attributes[]'" + + [ "$status" -eq 0 ] + [[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]] + [[ ${lines[1]} == '{"name":"pad_char","type":"string","required":false,"optional":true,"computed":false,"sensitive":false}' ]] + [[ ${lines[2]} == '{"name":"pad_width","type":"number","required":true,"optional":false,"computed":false,"sensitive":false}' ]] + [[ ${lines[3]} == '{"name":"string","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]] + [[ ${lines[4]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]] +} diff --git a/bats/tfschema_listing.bats.sh b/bats/tfschema_listing.bats.sh index c4a19ffd..75f7a3fd 100644 --- a/bats/tfschema_listing.bats.sh +++ b/bats/tfschema_listing.bats.sh @@ -6,11 +6,13 @@ [ "$status" -eq 0 ] [[ ${lines[0]} == "corefunc_env_ensure" ]] - [[ ${lines[1]} == "corefunc_str_camel" ]] - [[ ${lines[2]} == "corefunc_str_constant" ]] - [[ ${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" ]] + [[ ${lines[1]} == "corefunc_int_leftpad" ]] + [[ ${lines[2]} == "corefunc_str_camel" ]] + [[ ${lines[3]} == "corefunc_str_constant" ]] + [[ ${lines[4]} == "corefunc_str_iterative_replace" ]] + [[ ${lines[5]} == "corefunc_str_kebab" ]] + [[ ${lines[6]} == "corefunc_str_leftpad" ]] + [[ ${lines[7]} == "corefunc_str_pascal" ]] + [[ ${lines[8]} == "corefunc_str_snake" ]] + [[ ${lines[9]} == "corefunc_str_truncate_label" ]] } diff --git a/corefunc/str_pad.go b/corefunc/str_pad.go index 3de4a794..93285c74 100644 --- a/corefunc/str_pad.go +++ b/corefunc/str_pad.go @@ -14,9 +14,7 @@ package corefunc -import ( - "strings" -) +import "strings" /* StrLeftPad pads a string on the left side with a given string until it reaches @@ -29,22 +27,23 @@ the desired width. - padWidth (int): The total width of the padded string. If `str` is equivalent length or longer, then no padding will be applied. - - padStr (string): The string to use as padding. If unspecified, will use a + - padChar (string): The string to use as padding. If unspecified, will use a single space character. */ -func StrLeftPad(str string, padWidth int, padStr ...rune) string { - if len(str) > padWidth { +func StrLeftPad(str string, padWidth int, padChar ...byte) string { + if len(str) >= padWidth { return str } - pStr := ' ' + var pStr byte + pStr = 0x20 if padWidth < 0 { padWidth = 0 } - if len(padStr) > 0 { - pStr = padStr[0] + if len(padChar) > 0 { + pStr = padChar[0] } return strings.Repeat(string(pStr), padWidth-len(str)) + str diff --git a/corefunc/str_pad_test.go b/corefunc/str_pad_test.go index a29f3784..b901d492 100644 --- a/corefunc/str_pad_test.go +++ b/corefunc/str_pad_test.go @@ -17,7 +17,6 @@ package corefunc import ( "fmt" "testing" - "unicode/utf8" "github.com/northwood-labs/terraform-provider-corefunc/testfixtures" ) @@ -37,7 +36,14 @@ func ExampleStrLeftPad() { func TestStrLeftPad(t *testing.T) { for name, tc := range testfixtures.StrLeftPadTestTable { t.Run(name, func(t *testing.T) { - output := StrLeftPad(tc.Input, tc.PadWidth, tc.PadStr) + output := "" + var emptyByte byte + + if tc.PadChar == emptyByte { + output = StrLeftPad(tc.Input, tc.PadWidth) + } else { + output = StrLeftPad(tc.Input, tc.PadWidth, tc.PadChar) + } if output != tc.Expected { t.Errorf("Expected %s, got %s", tc.Expected, output) @@ -53,7 +59,7 @@ func BenchmarkStrLeftPad(b *testing.B) { b.Run(name, func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - _ = StrLeftPad(tc.Input, tc.PadWidth, tc.PadStr) // lint:allow_unhandled + _ = StrLeftPad(tc.Input, tc.PadWidth, tc.PadChar) // lint:allow_unhandled } }) } @@ -67,7 +73,7 @@ func BenchmarkStrLeftPadParallel(b *testing.B) { b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - _ = StrLeftPad(tc.Input, tc.PadWidth, tc.PadStr) // lint:allow_unhandled + _ = StrLeftPad(tc.Input, tc.PadWidth, tc.PadChar) // lint:allow_unhandled } }) }) @@ -82,8 +88,7 @@ func FuzzStrLeftPad(f *testing.F) { f.Fuzz( func(t *testing.T, in string) { - r, _ := utf8.DecodeRuneInString(in) - _ = StrLeftPad(in, len(in), r) // lint:allow_unhandled + _ = StrLeftPad(in, len(in)) // lint:allow_unhandled }, ) } diff --git a/corefuncprovider/env_ensure_data_source.go b/corefuncprovider/env_ensure_data_source.go index 176204cf..8a4f76dd 100644 --- a/corefuncprovider/env_ensure_data_source.go +++ b/corefuncprovider/env_ensure_data_source.go @@ -72,7 +72,7 @@ func (d *envEnsureDataSource) Metadata( } // Schema defines the schema for the data source. -func (d *envEnsureDataSource) Schema( +func (d *envEnsureDataSource) Schema( // lint:no_dupe ctx context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse, diff --git a/corefuncprovider/int_leftpad_data_source.go b/corefuncprovider/int_leftpad_data_source.go new file mode 100644 index 00000000..86f9838a --- /dev/null +++ b/corefuncprovider/int_leftpad_data_source.go @@ -0,0 +1,171 @@ +// 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 corefuncprovider // lint:no_dupe + +import ( + "context" + "fmt" + "strings" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/lithammer/dedent" + "github.com/northwood-labs/terraform-provider-corefunc/corefunc" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &intLeftpadDataSource{} + _ datasource.DataSourceWithConfigure = &intLeftpadDataSource{} +) + +// intLeftpadDataSource is the data source implementation. +type ( + intLeftpadDataSource struct{} + + // intLeftpadDataSourceModel maps the data source schema data. + intLeftpadDataSourceModel struct { + Value types.String `tfsdk:"value"` + ID types.Int64 `tfsdk:"id"` + Num types.Int64 `tfsdk:"num"` + PadWidth types.Int64 `tfsdk:"pad_width"` + } +) + +// IntLeftpadDataSource is a method that exposes its paired Go function as a +// Terraform Data Source. +func IntLeftpadDataSource() datasource.DataSource { // lint:allow_return_interface + return &intLeftpadDataSource{} +} + +// Metadata returns the data source type name. +func (d *intLeftpadDataSource) Metadata( + ctx context.Context, + req datasource.MetadataRequest, + resp *datasource.MetadataResponse, +) { + tflog.Info(ctx, "Starting IntLeftpad DataSource Metadata method.") + + resp.TypeName = req.ProviderTypeName + "_int_leftpad" + + tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName)) + tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName)) + + tflog.Info(ctx, "Ending IntLeftpad DataSource Metadata method.") +} + +// Schema defines the schema for the data source. +func (d *intLeftpadDataSource) Schema( // lint:no_dupe + ctx context.Context, + _ datasource.SchemaRequest, + resp *datasource.SchemaResponse, +) { + tflog.Info(ctx, "Starting IntLeftpad DataSource Schema method.") + + resp.Schema = schema.Schema{ + MarkdownDescription: strings.TrimSpace(dedent.Dedent(` + Converts an integer to a string, and then pads it with zeroes on the left. + + Maps to the ` + linkPackage("IntLeftPad") + ` Go method, which can be used in ` + Terratest + `.`)), + Attributes: map[string]schema.Attribute{ + "id": schema.Int64Attribute{ + Description: "Not used. Required by the " + TPF + ".", + Computed: true, + }, + "num": schema.Int64Attribute{ + Description: "The integer to pad with zeroes.", + Required: true, + }, + "pad_width": schema.Int64Attribute{ + Description: "The max number of zeroes to pad the integer with.", + Required: true, + }, + "value": schema.StringAttribute{ + Description: "The value of the string.", + Computed: true, + }, + }, + } + + tflog.Info(ctx, "Ending IntLeftpad DataSource Schema method.") +} + +// Configure adds the provider configured client to the data source. +func (d *intLeftpadDataSource) Configure( + ctx context.Context, + req datasource.ConfigureRequest, + _ *datasource.ConfigureResponse, +) { + tflog.Info(ctx, "Starting IntLeftpad DataSource Configure method.") + + if req.ProviderData == nil { + return + } + + tflog.Info(ctx, "Ending IntLeftpad DataSource Configure method.") +} + +func (d *intLeftpadDataSource) Create( + ctx context.Context, + req resource.CreateRequest, // lint:allow_large_memory + resp *resource.CreateResponse, +) { + tflog.Info(ctx, "Starting IntLeftpad DataSource Create method.") + + var plan intLeftpadDataSourceModel + + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + tflog.Info(ctx, "Ending IntLeftpad DataSource Create method.") +} + +// Read refreshes the Terraform state with the latest data. +func (d *intLeftpadDataSource) Read( // lint:no_dupe + ctx context.Context, + _ datasource.ReadRequest, // lint:allow_large_memory + resp *datasource.ReadResponse, +) { + tflog.Info(ctx, "Starting IntLeftpad DataSource Read method.") + + var state intLeftpadDataSourceModel + diags := resp.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + + state.ID = types.Int64Value(1) + + state.Value = types.StringValue( + corefunc.IntLeftPad( + state.Num.ValueInt64(), + int(state.PadWidth.ValueInt64()), + ), + ) + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + tflog.Info(ctx, "Ending IntLeftpad DataSource Read method.") +} diff --git a/corefuncprovider/int_leftpad_data_source_fixture.tftpl b/corefuncprovider/int_leftpad_data_source_fixture.tftpl new file mode 100644 index 00000000..9cdb9355 --- /dev/null +++ b/corefuncprovider/int_leftpad_data_source_fixture.tftpl @@ -0,0 +1,5 @@ +data "corefunc_int_leftpad" "leftpad" { + num = {{ .Input }} + pad_width = {{ .PadWidth }} +} +#=> {{ .Expected }} diff --git a/corefuncprovider/int_leftpad_data_source_test.go b/corefuncprovider/int_leftpad_data_source_test.go new file mode 100644 index 00000000..028ccb5f --- /dev/null +++ b/corefuncprovider/int_leftpad_data_source_test.go @@ -0,0 +1,67 @@ +// 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 corefuncprovider // lint:no_dupe + +import ( + "bytes" + "fmt" + "log" + "os" + "strings" + "testing" + "text/template" + + "github.com/northwood-labs/terraform-provider-corefunc/testfixtures" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccIntLeftpadDataSource(t *testing.T) { + funcName := traceFuncName() + + for name, tc := range testfixtures.IntLeftPadTestTable { + fmt.Printf( + "=== RUN %s/%s\n", + strings.TrimSpace(funcName), + strings.TrimSpace(name), + ) + + buf := &bytes.Buffer{} + tmpl := template.Must( + template.ParseFiles("int_leftpad_data_source_fixture.tftpl"), + ) + + err := tmpl.Execute(buf, tc) + if err != nil { + log.Fatalln(err) + } + + if os.Getenv("PROVIDER_DEBUG") != "" { + fmt.Fprintln(os.Stderr, buf.String()) + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: providerConfig + buf.String(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.corefunc_int_leftpad.leftpad", "value", tc.Expected), + ), + }, + }, + }) + } +} diff --git a/corefuncprovider/provider.go b/corefuncprovider/provider.go index 2b6994b1..5af33f5e 100644 --- a/corefuncprovider/provider.go +++ b/corefuncprovider/provider.go @@ -102,10 +102,12 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource. return []func() datasource.DataSource{ EnvEnsureDataSource, + IntLeftpadDataSource, StrCamelDataSource, StrConstantDataSource, StrIterativeReplaceDataSource, StrKebabDataSource, + StrLeftpadDataSource, StrPascalDataSource, StrSnakeDataSource, TruncateLabelDataSource, diff --git a/corefuncprovider/str_leftpad_data_source.go b/corefuncprovider/str_leftpad_data_source.go new file mode 100644 index 00000000..974a26a7 --- /dev/null +++ b/corefuncprovider/str_leftpad_data_source.go @@ -0,0 +1,190 @@ +// 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 corefuncprovider // lint:no_dupe + +import ( + "context" + "fmt" + "strings" + + "github.com/hashicorp/terraform-plugin-framework/datasource" + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource" + "github.com/hashicorp/terraform-plugin-framework/types" + "github.com/hashicorp/terraform-plugin-log/tflog" + "github.com/lithammer/dedent" + "github.com/northwood-labs/terraform-provider-corefunc/corefunc" +) + +// Ensure the implementation satisfies the expected interfaces. +var ( + _ datasource.DataSource = &strLeftpadDataSource{} + _ datasource.DataSourceWithConfigure = &strLeftpadDataSource{} +) + +// strLeftpadDataSource is the data source implementation. +type ( + strLeftpadDataSource struct{} + + // strLeftpadDataSourceModel maps the data source schema data. + strLeftpadDataSourceModel struct { + PadChar types.String `tfsdk:"pad_char"` + String types.String `tfsdk:"string"` + Value types.String `tfsdk:"value"` + ID types.Int64 `tfsdk:"id"` + PadWidth types.Int64 `tfsdk:"pad_width"` + } +) + +// StrLeftpadDataSource is a method that exposes its paired Go function as a +// Terraform Data Source. +func StrLeftpadDataSource() datasource.DataSource { // lint:allow_return_interface + return &strLeftpadDataSource{} +} + +// Metadata returns the data source type name. +func (d *strLeftpadDataSource) Metadata( + ctx context.Context, + req datasource.MetadataRequest, + resp *datasource.MetadataResponse, +) { + tflog.Info(ctx, "Starting StrLeftpad DataSource Metadata method.") + + resp.TypeName = req.ProviderTypeName + "_str_leftpad" + + tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName)) + tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName)) + + tflog.Info(ctx, "Ending StrLeftpad DataSource Metadata method.") +} + +// Schema defines the schema for the data source. +func (d *strLeftpadDataSource) Schema( // lint:no_dupe + ctx context.Context, + _ datasource.SchemaRequest, + resp *datasource.SchemaResponse, +) { + tflog.Info(ctx, "Starting StrLeftpad DataSource Schema method.") + + resp.Schema = schema.Schema{ + MarkdownDescription: strings.TrimSpace(dedent.Dedent(` + Pads a string with additional characters on the left. + + Maps to the ` + linkPackage("StrLeftPad") + ` Go method, which can be used in ` + Terratest + `.`)), + Attributes: map[string]schema.Attribute{ + "id": schema.Int64Attribute{ + Description: "Not used. Required by the " + TPF + ".", + Computed: true, + }, + "pad_width": schema.Int64Attribute{ + Description: "The max number of padding characters to pad the string with.", + Required: true, + }, + "string": schema.StringAttribute{ + Description: "The string to pad with padding characters.", + Required: true, + }, + "pad_char": schema.StringAttribute{ + Description: "The padding character to use. Only supports a single byte. If more than one byte is provided, only the first byte will be used. The default value is a space character.", // lint:ignore_length + Optional: true, + }, + "value": schema.StringAttribute{ + Description: "The value of the string.", + Computed: true, + }, + }, + } + + tflog.Info(ctx, "Ending StrLeftpad DataSource Schema method.") +} + +// Configure adds the provider configured client to the data source. +func (d *strLeftpadDataSource) Configure( + ctx context.Context, + req datasource.ConfigureRequest, + _ *datasource.ConfigureResponse, +) { + tflog.Info(ctx, "Starting StrLeftpad DataSource Configure method.") + + if req.ProviderData == nil { + return + } + + tflog.Info(ctx, "Ending StrLeftpad DataSource Configure method.") +} + +func (d *strLeftpadDataSource) Create( + ctx context.Context, + req resource.CreateRequest, // lint:allow_large_memory + resp *resource.CreateResponse, +) { + tflog.Info(ctx, "Starting StrLeftpad DataSource Create method.") + + var plan strLeftpadDataSourceModel + + diags := req.Plan.Get(ctx, &plan) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + tflog.Info(ctx, "Ending StrLeftpad DataSource Create method.") +} + +// Read refreshes the Terraform state with the latest data. +func (d *strLeftpadDataSource) Read( // lint:no_dupe + ctx context.Context, + _ datasource.ReadRequest, // lint:allow_large_memory + resp *datasource.ReadResponse, +) { + tflog.Info(ctx, "Starting StrLeftpad DataSource Read method.") + + var state strLeftpadDataSourceModel + diags := resp.State.Get(ctx, &state) + resp.Diagnostics.Append(diags...) + + state.ID = types.Int64Value(1) + + // Grab the pad character, or use a space by default. + padChar := state.PadChar.ValueString() + if padChar == "" { + state.Value = types.StringValue( + corefunc.StrLeftPad( + state.String.ValueString(), + int(state.PadWidth.ValueInt64()), + ), + ) + } else { + padByte := []byte(padChar)[0] + + state.Value = types.StringValue( + corefunc.StrLeftPad( + state.String.ValueString(), + int(state.PadWidth.ValueInt64()), + padByte, + ), + ) + } + + diags = resp.State.Set(ctx, &state) + resp.Diagnostics.Append(diags...) + + if resp.Diagnostics.HasError() { + return + } + + tflog.Info(ctx, "Ending StrLeftpad DataSource Read method.") +} diff --git a/corefuncprovider/str_leftpad_data_source_fixture.tftpl b/corefuncprovider/str_leftpad_data_source_fixture.tftpl new file mode 100644 index 00000000..1b34f53c --- /dev/null +++ b/corefuncprovider/str_leftpad_data_source_fixture.tftpl @@ -0,0 +1,8 @@ +data "corefunc_str_leftpad" "leftpad" { + string = "{{ .Input }}" + pad_width = "{{ .PadWidth }}" +{{- with .PadChar }} + pad_char = "{{ . | printf "%c" }}" +{{- end }} +} +#=> {{ .Expected }} diff --git a/corefuncprovider/str_leftpad_data_source_test.go b/corefuncprovider/str_leftpad_data_source_test.go new file mode 100644 index 00000000..23d81f24 --- /dev/null +++ b/corefuncprovider/str_leftpad_data_source_test.go @@ -0,0 +1,67 @@ +// 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 corefuncprovider // lint:no_dupe + +import ( + "bytes" + "fmt" + "log" + "os" + "strings" + "testing" + "text/template" + + "github.com/northwood-labs/terraform-provider-corefunc/testfixtures" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccStrLeftpadDataSource(t *testing.T) { + funcName := traceFuncName() + + for name, tc := range testfixtures.StrLeftPadTestTable { + fmt.Printf( + "=== RUN %s/%s\n", + strings.TrimSpace(funcName), + strings.TrimSpace(name), + ) + + buf := &bytes.Buffer{} + tmpl := template.Must( + template.ParseFiles("str_leftpad_data_source_fixture.tftpl"), + ) + + err := tmpl.Execute(buf, tc) + if err != nil { + log.Fatalln(err) + } + + if os.Getenv("PROVIDER_DEBUG") != "" { + fmt.Fprintln(os.Stderr, buf.String()) + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: providerConfig + buf.String(), + Check: resource.ComposeAggregateTestCheckFunc( + resource.TestCheckResourceAttr("data.corefunc_str_leftpad.leftpad", "value", tc.Expected), + ), + }, + }, + }) + } +} diff --git a/docs/data-sources/int_leftpad.md b/docs/data-sources/int_leftpad.md new file mode 100644 index 00000000..4848424e --- /dev/null +++ b/docs/data-sources/int_leftpad.md @@ -0,0 +1,40 @@ + + +# corefunc_int_leftpad (Data Source) + +Converts an integer to a string, and then pads it with zeroes on the left. + +Maps to the [`corefunc.IntLeftPad()`](https://pkg.go.dev/github.com/northwood-labs/terraform-provider-corefunc/corefunc#IntLeftPad) Go method, which can be used in [Terratest](https://terratest.gruntwork.io). + +## Example Usage + +```terraform +data "corefunc_int_leftpad" "leftpad" { + num = 123 + pad_width = 5 +} +#=> "00123" +``` + + +## Schema + +### Required + +* `num` (Number) The integer to pad with zeroes. +* `pad_width` (Number) The max number of zeroes to pad the integer with. + +### Read-Only + +* `id` (Number) Not used. Required by the [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework). +* `value` (String) The value of the string. + + diff --git a/docs/data-sources/str_leftpad.md b/docs/data-sources/str_leftpad.md new file mode 100644 index 00000000..f7eda807 --- /dev/null +++ b/docs/data-sources/str_leftpad.md @@ -0,0 +1,45 @@ + + +# corefunc_str_leftpad (Data Source) + +Pads a string with additional characters on the left. + +Maps to the [`corefunc.StrLeftPad()`](https://pkg.go.dev/github.com/northwood-labs/terraform-provider-corefunc/corefunc#StrLeftPad) Go method, which can be used in [Terratest](https://terratest.gruntwork.io). + +## Example Usage + +```terraform +data "corefunc_str_leftpad" "leftpad" { + string = "abc" + pad_width = 5 + pad_char = "." +} +#=> ..abc +``` + + +## Schema + +### Required + +* `pad_width` (Number) The max number of padding characters to pad the string with. +* `string` (String) The string to pad with padding characters. + +### Optional + +* `pad_char` (String) The padding character to use. Only supports a single byte. If more than one byte is provided, only the first byte will be used. The default value is a space character. + +### Read-Only + +* `id` (Number) Not used. Required by the [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework). +* `value` (String) The value of the string. + + diff --git a/examples/data-sources/corefunc_int_leftpad/data-source.tf b/examples/data-sources/corefunc_int_leftpad/data-source.tf new file mode 100644 index 00000000..472f0fb2 --- /dev/null +++ b/examples/data-sources/corefunc_int_leftpad/data-source.tf @@ -0,0 +1,5 @@ +data "corefunc_int_leftpad" "leftpad" { + num = 123 + pad_width = 5 +} +#=> "00123" diff --git a/examples/data-sources/corefunc_int_leftpad/versions.tftpl b/examples/data-sources/corefunc_int_leftpad/versions.tftpl new file mode 100644 index 00000000..3c3fdda8 --- /dev/null +++ b/examples/data-sources/corefunc_int_leftpad/versions.tftpl @@ -0,0 +1,13 @@ +terraform { + required_version = "~> 1.1" + + required_providers { + corefunc = { + source = "northwood-labs/corefunc" + version = "~> 1.0" + } + } +} + +# There are no configuration options +provider "corefunc" {} diff --git a/examples/data-sources/corefunc_str_leftpad/data-source.tf b/examples/data-sources/corefunc_str_leftpad/data-source.tf new file mode 100644 index 00000000..53098f22 --- /dev/null +++ b/examples/data-sources/corefunc_str_leftpad/data-source.tf @@ -0,0 +1,6 @@ +data "corefunc_str_leftpad" "leftpad" { + string = "abc" + pad_width = 5 + pad_char = "." +} +#=> ..abc diff --git a/examples/data-sources/corefunc_str_leftpad/versions.tftpl b/examples/data-sources/corefunc_str_leftpad/versions.tftpl new file mode 100644 index 00000000..3c3fdda8 --- /dev/null +++ b/examples/data-sources/corefunc_str_leftpad/versions.tftpl @@ -0,0 +1,13 @@ +terraform { + required_version = "~> 1.1" + + required_providers { + corefunc = { + source = "northwood-labs/corefunc" + version = "~> 1.0" + } + } +} + +# There are no configuration options +provider "corefunc" {} diff --git a/examples/provider/lock-all.sh b/examples/provider/lock-all.sh index 2bfe3fd9..6ef99876 100644 --- a/examples/provider/lock-all.sh +++ b/examples/provider/lock-all.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash terraform providers lock \ -platform=darwin_amd64 \ -platform=darwin_arm64 \ diff --git a/examples/provider/lock-ext.sh b/examples/provider/lock-ext.sh index 3d44f472..3836bc82 100644 --- a/examples/provider/lock-ext.sh +++ b/examples/provider/lock-ext.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash terraform providers lock \ -platform=darwin_amd64 \ -platform=darwin_arm64 \ diff --git a/examples/provider/lock.sh b/examples/provider/lock.sh index c60aa138..9c3fac2c 100644 --- a/examples/provider/lock.sh +++ b/examples/provider/lock.sh @@ -1,3 +1,4 @@ +#!/usr/bin/env bash terraform providers lock \ -platform=darwin_amd64 \ -platform=darwin_arm64 \ diff --git a/generator/corefuncprovider/data_source.gotmpl b/generator/corefuncprovider/data_source.gotmpl index 310d427e..ae9039d2 100644 --- a/generator/corefuncprovider/data_source.gotmpl +++ b/generator/corefuncprovider/data_source.gotmpl @@ -129,7 +129,7 @@ func (d *{{ .CamelStrip }}DataSource) Configure( tflog.Info(ctx, "Ending {{ .PascalStrip }} DataSource Configure method.") } -func (d {{ .CamelStrip }}DataSource) Create( +func (d *{{ .CamelStrip }}DataSource) Create( ctx context.Context, req resource.CreateRequest, // lint:allow_large_memory resp *resource.CreateResponse, diff --git a/generator/corefuncprovider/data_source_test.gotmpl b/generator/corefuncprovider/data_source_test.gotmpl index d1f3d774..00de65de 100644 --- a/generator/corefuncprovider/data_source_test.gotmpl +++ b/generator/corefuncprovider/data_source_test.gotmpl @@ -38,7 +38,7 @@ func TestAcc{{ .PascalStrip }}DataSource(t *testing.T) { strings.TrimSpace(name), ) - buf := new(bytes.Buffer) + buf := &bytes.Buffer{} tmpl := template.Must( template.ParseFiles("{{ .SnakeStrip }}_data_source_fixture.tftpl"), ) diff --git a/templates/data-sources/int_leftpad.md.tmpl b/templates/data-sources/int_leftpad.md.tmpl new file mode 100644 index 00000000..a71c4c73 --- /dev/null +++ b/templates/data-sources/int_leftpad.md.tmpl @@ -0,0 +1,20 @@ + + +# {{ .Name }} ({{ .Type }}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/data-sources/corefunc_int_leftpad/data-source.tf" }} + +{{ .SchemaMarkdown | trimspace }} + + diff --git a/templates/data-sources/str_leftpad.md.tmpl b/templates/data-sources/str_leftpad.md.tmpl new file mode 100644 index 00000000..93577be9 --- /dev/null +++ b/templates/data-sources/str_leftpad.md.tmpl @@ -0,0 +1,20 @@ + + +# {{ .Name }} ({{ .Type }}) + +{{ .Description | trimspace }} + +## Example Usage + +{{ tffile "examples/data-sources/corefunc_str_leftpad/data-source.tf" }} + +{{ .SchemaMarkdown | trimspace }} + + diff --git a/testfixtures/str_pad.go b/testfixtures/str_pad.go index 53acaddf..fb4013d4 100644 --- a/testfixtures/str_pad.go +++ b/testfixtures/str_pad.go @@ -21,48 +21,83 @@ var StrLeftPadTestTable = map[string]struct { // lint:no_dupe Input string Expected string PadWidth int - PadStr rune + PadChar byte }{ "Xn1": { Input: "foo", PadWidth: -1, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "foo", }, "X0": { Input: "foo", PadWidth: 0, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "foo", }, "X1": { Input: "foo", PadWidth: 1, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "foo", }, "X2": { Input: "foo", PadWidth: 2, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "foo", }, "X3": { Input: "foo", PadWidth: 3, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "foo", }, "X4": { Input: "foo", PadWidth: 4, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: ".foo", }, "X5": { Input: "foo", PadWidth: 5, // lint:allow_raw_number - PadStr: '.', + PadChar: '.', Expected: "..foo", }, + "_Xn1": { + Input: "foo", + PadWidth: -1, // lint:allow_raw_number + Expected: "foo", + }, + "_X0": { + Input: "foo", + PadWidth: 0, // lint:allow_raw_number + Expected: "foo", + }, + "_X1": { + Input: "foo", + PadWidth: 1, // lint:allow_raw_number + Expected: "foo", + }, + "_X2": { + Input: "foo", + PadWidth: 2, // lint:allow_raw_number + Expected: "foo", + }, + "_X3": { + Input: "foo", + PadWidth: 3, // lint:allow_raw_number + Expected: "foo", + }, + "_X4": { + Input: "foo", + PadWidth: 4, // lint:allow_raw_number + Expected: " foo", + }, + "_X5": { + Input: "foo", + PadWidth: 5, // lint:allow_raw_number + Expected: " foo", + }, } diff --git a/unit-coverage.png b/unit-coverage.png index 37d88a75..5f9705a3 100644 Binary files a/unit-coverage.png and b/unit-coverage.png differ diff --git a/unit-coverage.svg b/unit-coverage.svg index 554c0580..50108c63 100644 --- a/unit-coverage.svg +++ b/unit-coverage.svg @@ -7,7 +7,7 @@ > - + - + env_ensure.go @@ -33,12 +33,12 @@ - + int_pad.go @@ -46,12 +46,12 @@ - + str_iterative_replace.go @@ -59,12 +59,12 @@ - + str_pad.go @@ -72,7 +72,7 @@ - +