diff --git a/acc-coverage.png b/acc-coverage.png index 2993cba2..412c8f48 100644 Binary files a/acc-coverage.png and b/acc-coverage.png differ diff --git a/acc-coverage.svg b/acc-coverage.svg index 4b775d60..45f5babf 100644 --- a/acc-coverage.svg +++ b/acc-coverage.svg @@ -7,7 +7,7 @@ > - + - + - + - + - + - + provider.go - + - + - + - + - + - + - + - + - + - + - + - + - + +// +// 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/function" + "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 _ function.Function = &strSnakeFunction{} + +type ( + // strSnakeFunction is the function implementation. + strSnakeFunction struct{} +) + +// StrSnakeFunction is a method that exposes its paired Go function as a +// Terraform Function. +func StrSnakeFunction() function.Function { // lint:allow_return_interface + return &strSnakeFunction{} +} + +func (f *strSnakeFunction) Metadata( + ctx context.Context, + req function.MetadataRequest, + resp *function.MetadataResponse, +) { + tflog.Debug(ctx, "Starting StrSnake Function Metadata method.") + + resp.Name = "str_snake" + + tflog.Debug(ctx, fmt.Sprintf("resp.Name = %s", resp.Name)) + + tflog.Debug(ctx, "Ending StrSnake Function Metadata method.") +} + +// Definition defines the parameters and return type for the function. +func (f *strSnakeFunction) Definition( + ctx context.Context, + req function.DefinitionRequest, + resp *function.DefinitionResponse, +) { + tflog.Debug(ctx, "Starting StrSnake Function Definition method.") + + resp.Definition = function.Definition{ + Summary: "Echo a string", + MarkdownDescription: strings.TrimSpace(dedent.Dedent(` + Converts a string to ` + "`" + `snake_case` + "`" + `, removing any non-alphanumeric characters. + + Maps to the ` + linkPackage("StrSnake") + ` Go method, which can be used in ` + Terratest + `. + `)), + Parameters: []function.Parameter{ + function.StringParameter{ + Name: "str", + MarkdownDescription: "The string to convert to `snake_case`.", + }, + }, + Return: function.StringReturn{}, + } + + tflog.Debug(ctx, "Ending StrSnake Function Definition method.") +} + +func (f *strSnakeFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) { + tflog.Debug(ctx, "Starting StrSnake Function Run method.") + + var str string + err := req.Arguments.Get(ctx, &str) + + resp.Error = function.ConcatFuncErrors(err) + if resp.Error != nil { + return + } + + value := corefunc.StrSnake( + str, + // opts, + ) + + // Function errors need to be wrapped and passed to resp.Error. + // function.NewArgumentFuncError(1, "") + // https://github.com/Yantrio/terraform-provider-tfutils/blob/main/internal/provider/cidrcontains.go + + resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, value)) + + tflog.Debug(ctx, "Ending StrSnake Function Run method.") +} diff --git a/corefuncprovider/str_snake_function_fixture.tftpl b/corefuncprovider/str_snake_function_fixture.tftpl new file mode 100644 index 00000000..f618abfa --- /dev/null +++ b/corefuncprovider/str_snake_function_fixture.tftpl @@ -0,0 +1,4 @@ +output "snake" { + value = provider::corefunc::str_snake("{{ .Input }}") +} +#=> {{ .Expected }} diff --git a/corefuncprovider/str_snake_function_test.go b/corefuncprovider/str_snake_function_test.go new file mode 100644 index 00000000..a25578e8 --- /dev/null +++ b/corefuncprovider/str_snake_function_test.go @@ -0,0 +1,76 @@ +// Copyright 2023-2024, Northwood Labs +// Copyright 2023-2024, 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/go-version" + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/tfversion" +) + +func TestAccStrSnakeFunction(t *testing.T) { + funcName := traceFuncName() + + for name, tc := range testfixtures.StrSnakeTestTable { // lint:no_dupe + fmt.Printf( + "=== RUN %s/%s\n", + strings.TrimSpace(funcName), + strings.TrimSpace(name), + ) + + buf := &bytes.Buffer{} + tmpl := template.Must( + template.ParseFiles("str_snake_function_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.UnitTest(t, resource.TestCase{ + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.SkipBelow( + version.Must(version.NewVersion("1.7.999")), + ), + }, + ProtoV6ProviderFactories: testAccProtoV6ProviderFactories, + Steps: []resource.TestStep{ + { + Config: providerConfig + buf.String(), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckOutput("snake", tc.Expected), + ), + // ExpectError: tc.expectedError, + }, + }, + }) + } +} diff --git a/go.mod b/go.mod index ead8f46e..af412ae5 100644 --- a/go.mod +++ b/go.mod @@ -11,6 +11,7 @@ require ( github.com/google/go-cmp v0.6.0 github.com/gookit/color v1.5.4 github.com/gtramontina/ooze v0.2.0 + github.com/hashicorp/go-version v1.6.0 github.com/hashicorp/terraform-plugin-docs v0.18.0 github.com/hashicorp/terraform-plugin-framework v1.6.1 github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 @@ -52,7 +53,6 @@ require ( github.com/hashicorp/go-multierror v1.1.1 // indirect github.com/hashicorp/go-plugin v1.6.0 // indirect github.com/hashicorp/go-uuid v1.0.3 // indirect - github.com/hashicorp/go-version v1.6.0 // indirect github.com/hashicorp/hc-install v0.6.3 // indirect github.com/hashicorp/hcl/v2 v2.20.0 // indirect github.com/hashicorp/logutils v1.0.0 // indirect diff --git a/unit-coverage.png b/unit-coverage.png index 12a71819..a30fbf1c 100644 Binary files a/unit-coverage.png and b/unit-coverage.png differ diff --git a/unit-coverage.svg b/unit-coverage.svg index b77c6204..fefaf655 100644 --- a/unit-coverage.svg +++ b/unit-coverage.svg @@ -7,20 +7,20 @@ > - + github.com/northwood-labs/terraform-provider-corefunc/corefunc - + - + env_ensure.go - + homedir.go - + - + - + str_pad.go @@ -111,13 +111,13 @@ - + url.go