Skip to content

Commit

Permalink
feat: Implemented leftpads as functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
skyzyx committed Mar 11, 2024
1 parent 6de9499 commit 238263e
Show file tree
Hide file tree
Showing 30 changed files with 635 additions and 229 deletions.
Binary file modified acc-coverage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
152 changes: 89 additions & 63 deletions acc-coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
110 changes: 110 additions & 0 deletions corefuncprovider/int_leftpad_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Copyright 2023-2024, Northwood Labs
// Copyright 2023-2024, Ryan Parman <[email protected]>
//
// 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 = &intLeftpadFunction{}

type (
// intLeftpadFunction is the function implementation.
intLeftpadFunction struct{}
)

// IntLeftpadFunction is a method that exposes its paired Go function as a
// Terraform Function.
// https://developer.hashicorp.com/terraform/plugin/framework/functions/implementation
func IntLeftpadFunction() function.Function { // lint:allow_return_interface
return &intLeftpadFunction{}
}

func (f *intLeftpadFunction) Metadata(
ctx context.Context,
req function.MetadataRequest,
resp *function.MetadataResponse,
) {
tflog.Debug(ctx, "Starting IntLeftpad Function Metadata method.")

resp.Name = "int_leftpad"

tflog.Debug(ctx, fmt.Sprintf("resp.Name = %s", resp.Name))

tflog.Debug(ctx, "Ending IntLeftpad Function Metadata method.")
}

// Definition defines the parameters and return type for the function.
func (f *intLeftpadFunction) Definition(
ctx context.Context,
req function.DefinitionRequest,
resp *function.DefinitionResponse,
) {
tflog.Debug(ctx, "Starting IntLeftpad Function Definition method.")

resp.Definition = function.Definition{
Summary: "Converts an integer to a string, and then pads it with zeroes on the left.",
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
Converts an integer to a string, and then pads it with zeroes on the left.
-> If the integer is NOT in base10 (decimal), it will be converted to base10 (decimal) _before_ being padded.
Maps to the ` + linkPackage("IntLeftPad") + ` Go method, which can be used in ` + Terratest + `.
`)),
Parameters: []function.Parameter{
function.Int64Parameter{
Name: "num",
MarkdownDescription: "The integer to pad with zeroes.",
},
function.Int64Parameter{
Name: "pad_width",
MarkdownDescription: "The max number of zeroes to pad the integer with.",
},
},
Return: function.StringReturn{},
}

tflog.Debug(ctx, "Ending IntLeftpad Function Definition method.")
}

func (f *intLeftpadFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
tflog.Debug(ctx, "Starting IntLeftpad Function Run method.")

var (
num int64
padWidth int64
)

err := req.Arguments.Get(ctx, &num, &padWidth)

resp.Error = function.ConcatFuncErrors(err)
if resp.Error != nil {
return
}

value := corefunc.IntLeftPad(num, int(padWidth))
resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, value))

tflog.Debug(ctx, "Ending IntLeftpad Function Run method.")
}
4 changes: 4 additions & 0 deletions corefuncprovider/int_leftpad_function_fixture.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
output "leftpad" {
value = provider::corefunc::int_leftpad({{ .Input }}, {{ .PadWidth }})
}
#=> {{ .Expected }}
77 changes: 77 additions & 0 deletions corefuncprovider/int_leftpad_function_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright 2023-2024, Northwood Labs
// Copyright 2023-2024, Ryan Parman <[email protected]>
//
// 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 TestAccIntLeftpadFunction(t *testing.T) {
t.Parallel()

funcName := traceFuncName()

for name, tc := range testfixtures.IntLeftPadTestTable { // lint:no_dupe
fmt.Printf(
"=== RUN %s/%s\n",
strings.TrimSpace(funcName),
strings.TrimSpace(name),
)

buf := &bytes.Buffer{}
tmpl := template.Must(
template.ParseFiles("int_leftpad_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("leftpad", tc.Expected),
),
},
},
})
}
}
2 changes: 2 additions & 0 deletions corefuncprovider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ func (p *coreFuncProvider) Functions(ctx context.Context) []func() function.Func
EnvEnsureFunction,
HomedirExpandFunction,
HomedirGetFunction,
IntLeftpadFunction,
RuntimeCpuarchFunction,
RuntimeGorootFunction,
RuntimeNumcpusFunction,
RuntimeOsFunction,
StrCamelFunction,
StrConstantFunction,
StrKebabFunction,
StrLeftpadFunction,
StrPascalFunction,
StrSnakeFunction,
}
Expand Down
119 changes: 119 additions & 0 deletions corefuncprovider/str_leftpad_function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// Copyright 2023-2024, Northwood Labs
// Copyright 2023-2024, Ryan Parman <[email protected]>
//
// 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 = &strLeftpadFunction{}

type (
// strLeftpadFunction is the function implementation.
strLeftpadFunction struct{}
)

// StrLeftpadFunction is a method that exposes its paired Go function as a
// Terraform Function.
// https://developer.hashicorp.com/terraform/plugin/framework/functions/implementation
func StrLeftpadFunction() function.Function { // lint:allow_return_interface
return &strLeftpadFunction{}
}

func (f *strLeftpadFunction) Metadata(
ctx context.Context,
req function.MetadataRequest,
resp *function.MetadataResponse,
) {
tflog.Debug(ctx, "Starting StrLeftpad Function Metadata method.")

resp.Name = "str_leftpad"

tflog.Debug(ctx, fmt.Sprintf("resp.Name = %s", resp.Name))

tflog.Debug(ctx, "Ending StrLeftpad Function Metadata method.")
}

// Definition defines the parameters and return type for the function.
func (f *strLeftpadFunction) Definition(
ctx context.Context,
req function.DefinitionRequest,
resp *function.DefinitionResponse,
) {
tflog.Debug(ctx, "Starting StrLeftpad Function Definition method.")

resp.Definition = function.Definition{
Summary: "Pads a string with additional characters on the left.",
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 + `.
`)),
Parameters: []function.Parameter{
function.StringParameter{
Name: "str",
MarkdownDescription: "The string to pad with padding characters.",
},
function.Int64Parameter{
Name: "pad_width",
MarkdownDescription: "The max number of padding characters to pad the string with.",
},
},
VariadicParameter: function.StringParameter{
Name: "pad_char",
MarkdownDescription: "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.",
},
Return: function.StringReturn{},
}

tflog.Debug(ctx, "Ending StrLeftpad Function Definition method.")
}

func (f *strLeftpadFunction) Run(ctx context.Context, req function.RunRequest, resp *function.RunResponse) {
tflog.Debug(ctx, "Starting StrLeftpad Function Run method.")

var (
str string
value string
padWidth int64
padChar []string
)

resp.Error = function.ConcatFuncErrors(req.Arguments.Get(ctx, &str, &padWidth, &padChar))
if resp.Error != nil {
return
}

if len(padChar) > 0 && padChar[0] != "" {
b := []byte(padChar[0])
value = corefunc.StrLeftPad(str, int(padWidth), b[0])
} else {
value = corefunc.StrLeftPad(str, int(padWidth))
}

resp.Error = function.ConcatFuncErrors(resp.Result.Set(ctx, value))

tflog.Debug(ctx, "Ending StrLeftpad Function Run method.")
}
9 changes: 9 additions & 0 deletions corefuncprovider/str_leftpad_function_fixture.tftpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{ $root := . }}
output "leftpad" {
{{- with .PadChar }}
value = provider::corefunc::str_leftpad("{{ $root.Input }}", {{ $root.PadWidth }}, "{{ . | printf "%c" }}")
{{- else }}
value = provider::corefunc::str_leftpad("{{ $root.Input }}", {{ $root.PadWidth }})
{{- end }}
}
#=> {{ .Expected }}
Loading

0 comments on commit 238263e

Please sign in to comment.