Skip to content

Commit b763957

Browse files
committed
feat: Added corefunc_str_snake.
1 parent 0a3d40c commit b763957

File tree

11 files changed

+468
-0
lines changed

11 files changed

+468
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,9 @@ See the `docs/` directory for user-facing documentation.
2121

2222
* [TF Registry Documentation](https://registry.terraform.io/providers/northwood-labs/corefunc/) (not published yet; see the `docs/` directory in the interim)
2323
* [Go Package Documentation](https://pkg.go.dev/github.com/northwood-labs/terraform-provider-corefunc)
24+
25+
## Third-Party Libraries
26+
27+
Don't reimplement things that already work well. This project leans on the following libraries:
28+
29+
* [chanced/caps](https://github.com/chanced/caps) — Handles the case manipulation.

corefuncprovider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ func (p *coreFuncProvider) DataSources(ctx context.Context) []func() datasource.
103103
return []func() datasource.DataSource{
104104
TruncateLabelDataSource,
105105
EnvEnsureDataSource,
106+
StrSnakeDataSource,
106107
}
107108
}
108109

+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefuncprovider
16+
17+
import (
18+
"context"
19+
"fmt"
20+
"strings"
21+
22+
"github.com/chanced/caps"
23+
"github.com/hashicorp/terraform-plugin-framework/datasource"
24+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
25+
"github.com/hashicorp/terraform-plugin-framework/resource"
26+
"github.com/hashicorp/terraform-plugin-framework/types"
27+
"github.com/hashicorp/terraform-plugin-log/tflog"
28+
"github.com/lithammer/dedent"
29+
)
30+
31+
// Ensure the implementation satisfies the expected interfaces.
32+
var (
33+
_ datasource.DataSource = &strSnakeDataSource{}
34+
_ datasource.DataSourceWithConfigure = &strSnakeDataSource{}
35+
)
36+
37+
// strSnakeDataSource is the data source implementation.
38+
type (
39+
strSnakeDataSource struct{}
40+
41+
// strSnakeDataSourceModel maps the data source schema data.
42+
strSnakeDataSourceModel struct {
43+
ID types.Int64 `tfsdk:"id"`
44+
String types.String `tfsdk:"string"`
45+
Value types.String `tfsdk:"value"`
46+
}
47+
)
48+
49+
// StrSnakeDataSource is a method that exposes its paired Go function as a
50+
// Terraform Data Source.
51+
func StrSnakeDataSource() datasource.DataSource { // lint:allow_return_interface
52+
return &strSnakeDataSource{}
53+
}
54+
55+
// Metadata returns the data source type name.
56+
func (d *strSnakeDataSource) Metadata(
57+
ctx context.Context,
58+
req datasource.MetadataRequest,
59+
resp *datasource.MetadataResponse,
60+
) {
61+
tflog.Info(ctx, "Starting StrSnake DataSource Metadata method.")
62+
63+
resp.TypeName = req.ProviderTypeName + "_str_snake"
64+
65+
tflog.Debug(ctx, fmt.Sprintf("req.ProviderTypeName = %s", req.ProviderTypeName))
66+
tflog.Debug(ctx, fmt.Sprintf("resp.TypeName = %s", resp.TypeName))
67+
68+
tflog.Info(ctx, "Ending StrSnake DataSource Metadata method.")
69+
}
70+
71+
// Schema defines the schema for the data source.
72+
func (d *strSnakeDataSource) Schema(
73+
ctx context.Context,
74+
_ datasource.SchemaRequest,
75+
resp *datasource.SchemaResponse,
76+
) {
77+
tflog.Info(ctx, "Starting StrSnake DataSource Schema method.")
78+
79+
resp.Schema = schema.Schema{
80+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
81+
Converts a string to ` + "`" + `snake_case` + "`" + `, removing any non-alphanumeric characters.
82+
83+
Maps to the [` + "`" + `caps.ToSnake()` + "`" + `](https://pkg.go.dev/github.com/chanced/caps#ToSnake)
84+
Go method, which can be used in ` + Terratest + `.
85+
`)),
86+
Attributes: map[string]schema.Attribute{
87+
"id": schema.Int64Attribute{
88+
Description: "Not used. Required by the " + TPF + ".",
89+
Computed: true,
90+
},
91+
"string": schema.StringAttribute{
92+
Description: "The string to convert to `snake_case`.",
93+
Required: true,
94+
},
95+
"value": schema.StringAttribute{
96+
Description: "The value of the environment variable, if it exists.",
97+
Computed: true,
98+
},
99+
},
100+
}
101+
102+
tflog.Info(ctx, "Ending StrSnake DataSource Schema method.")
103+
}
104+
105+
// Configure adds the provider configured client to the data source.
106+
func (d *strSnakeDataSource) Configure(
107+
ctx context.Context,
108+
req datasource.ConfigureRequest,
109+
_ *datasource.ConfigureResponse,
110+
) {
111+
tflog.Info(ctx, "Starting StrSnake DataSource Configure method.")
112+
113+
if req.ProviderData == nil {
114+
return
115+
}
116+
117+
tflog.Info(ctx, "Ending StrSnake DataSource Configure method.")
118+
}
119+
120+
func (d strSnakeDataSource) Create(
121+
ctx context.Context,
122+
req resource.CreateRequest, // lint:allow_large_memory
123+
resp *resource.CreateResponse,
124+
) {
125+
tflog.Info(ctx, "Starting StrSnake DataSource Create method.")
126+
127+
var plan strSnakeDataSourceModel
128+
129+
diags := req.Plan.Get(ctx, &plan)
130+
resp.Diagnostics.Append(diags...)
131+
132+
if resp.Diagnostics.HasError() {
133+
return
134+
}
135+
136+
tflog.Info(ctx, "Ending StrSnake DataSource Create method.")
137+
}
138+
139+
// Read refreshes the Terraform state with the latest data.
140+
func (d *strSnakeDataSource) Read(
141+
ctx context.Context,
142+
_ datasource.ReadRequest, // lint:allow_large_memory
143+
resp *datasource.ReadResponse,
144+
) {
145+
tflog.Info(ctx, "Starting StrSnake DataSource Read method.")
146+
147+
var state strSnakeDataSourceModel
148+
diags := resp.State.Get(ctx, &state)
149+
resp.Diagnostics.Append(diags...)
150+
151+
state.ID = types.Int64Value(1)
152+
153+
state.Value = types.StringValue(
154+
caps.ToSnake(
155+
state.String.ValueString(),
156+
),
157+
)
158+
159+
diags = resp.State.Set(ctx, &state)
160+
resp.Diagnostics.Append(diags...)
161+
162+
if resp.Diagnostics.HasError() {
163+
return
164+
}
165+
166+
tflog.Info(ctx, "Ending StrSnake DataSource Read method.")
167+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
data "corefunc_str_snake" "snake" {
2+
string = "{{ .Input }}"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright 2023, Ryan Parman
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package corefuncprovider
16+
17+
import (
18+
"bytes"
19+
"fmt"
20+
"log"
21+
"strings"
22+
"testing"
23+
"text/template"
24+
25+
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"
26+
27+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
28+
)
29+
30+
func TestAccStrSnakeDataSource(t *testing.T) {
31+
funcName := traceFuncName()
32+
33+
for name, tc := range testfixtures.StrSnakeTestTable {
34+
fmt.Printf(
35+
"=== RUN %s/%s\n",
36+
strings.TrimSpace(funcName),
37+
strings.TrimSpace(name),
38+
)
39+
40+
buf := new(bytes.Buffer)
41+
tmpl := template.Must(
42+
template.ParseFiles("str_snake_data_source_fixture.tftpl"),
43+
)
44+
45+
err := tmpl.Execute(buf, tc)
46+
if err != nil {
47+
log.Fatalln(err)
48+
}
49+
50+
// fmt.Fprintln(os.Stderr, buf.String())
51+
52+
resource.Test(t, resource.TestCase{
53+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
54+
Steps: []resource.TestStep{
55+
{
56+
Config: providerConfig + buf.String(),
57+
Check: resource.ComposeAggregateTestCheckFunc(
58+
resource.TestCheckResourceAttr("data.corefunc_str_snake.snake", "value", tc.Expected),
59+
),
60+
},
61+
},
62+
})
63+
}
64+
}

docs/data-sources/str_snake.md

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<!--
2+
---
3+
page_title: "corefunc_str_snake Data Source - corefunc"
4+
subcategory: ""
5+
description: |-
6+
Converts a string to snake_case, removing any non-alphanumeric characters.
7+
Maps to the caps.ToSnake() https://pkg.go.dev/github.com/chanced/caps#ToSnake
8+
Go method, which can be used in Terratest https://terratest.gruntwork.io.
9+
---
10+
-->
11+
12+
# corefunc_str_snake (Data Source)
13+
14+
Converts a string to `snake_case`, removing any non-alphanumeric characters.
15+
16+
Maps to the [`caps.ToSnake()`](https://pkg.go.dev/github.com/chanced/caps#ToSnake)
17+
Go method, which can be used in [Terratest](https://terratest.gruntwork.io).
18+
19+
## Example Usage
20+
21+
```terraform
22+
data "corefunc_str_snake" "v322" {
23+
string = "v3.2.2"
24+
}
25+
26+
output "value" {
27+
value = data.corefunc_str_snake.v322.value
28+
}
29+
30+
#=> v3_2_2
31+
32+
#-----------------------------------------------------------------------
33+
34+
data "corefunc_str_snake" "test_from_camel" {
35+
string = "TestFromCamel"
36+
}
37+
38+
output "value_from_camel" {
39+
value = data.corefunc_str_snake.test_from_camel.value
40+
}
41+
42+
#=> test_from_camel
43+
44+
#-----------------------------------------------------------------------
45+
46+
data "corefunc_str_snake" "this_is_an_example" {
47+
string = "This is [an] {example}$${id32}."
48+
}
49+
50+
output "value_is_an_example" {
51+
value = data.corefunc_str_snake.this_is_an_example.value
52+
}
53+
54+
#=> this_is_an_example_id_32
55+
56+
#-----------------------------------------------------------------------
57+
58+
data "corefunc_str_snake" "test_with_number" {
59+
string = "test with number -123.456"
60+
}
61+
62+
output "value_with_number" {
63+
value = data.corefunc_str_snake.test_with_number.value
64+
}
65+
66+
#=> test_with_number_123_456
67+
```
68+
69+
<!-- schema generated by tfplugindocs -->
70+
## Schema
71+
72+
### Required
73+
74+
* `string` (String) The string to convert to `snake_case`.
75+
76+
### Read-Only
77+
78+
* `id` (Number) Not used. Required by the [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework).
79+
* `value` (String) The value of the environment variable, if it exists.
80+
81+
<!-- Preview the provider docs with the Terraform registry provider docs preview tool: https://registry.terraform.io/tools/doc-preview -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
data "corefunc_str_snake" "v322" {
2+
string = "v3.2.2"
3+
}
4+
5+
output "value" {
6+
value = data.corefunc_str_snake.v322.value
7+
}
8+
9+
#=> v3_2_2
10+
11+
#-----------------------------------------------------------------------
12+
13+
data "corefunc_str_snake" "test_from_camel" {
14+
string = "TestFromCamel"
15+
}
16+
17+
output "value_from_camel" {
18+
value = data.corefunc_str_snake.test_from_camel.value
19+
}
20+
21+
#=> test_from_camel
22+
23+
#-----------------------------------------------------------------------
24+
25+
data "corefunc_str_snake" "this_is_an_example" {
26+
string = "This is [an] {example}$${id32}."
27+
}
28+
29+
output "value_is_an_example" {
30+
value = data.corefunc_str_snake.this_is_an_example.value
31+
}
32+
33+
#=> this_is_an_example_id_32
34+
35+
#-----------------------------------------------------------------------
36+
37+
data "corefunc_str_snake" "test_with_number" {
38+
string = "test with number -123.456"
39+
}
40+
41+
output "value_with_number" {
42+
value = data.corefunc_str_snake.test_with_number.value
43+
}
44+
45+
#=> test_with_number_123_456
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = "~> 1.1"
3+
4+
required_providers {
5+
corefunc = {
6+
source = "northwood-labs/corefunc"
7+
version = "~> 1.0"
8+
}
9+
}
10+
}
11+
12+
# There are no configuration options
13+
14+
provider "corefunc" {}

0 commit comments

Comments
 (0)