-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implemented leftpads as functions.
- Loading branch information
Showing
30 changed files
with
635 additions
and
229 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
output "leftpad" { | ||
value = provider::corefunc::int_leftpad({{ .Input }}, {{ .PadWidth }}) | ||
} | ||
#=> {{ .Expected }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }} |
Oops, something went wrong.