Skip to content

Commit c6f0a0d

Browse files
committed
feat: Added corefunc_str_constant.
1 parent f33108f commit c6f0a0d

17 files changed

+469
-8
lines changed

bats/tfschema.bats.sh

+14-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
[ "$status" -eq 0 ]
88
[[ ${lines[0]} == "corefunc_env_ensure" ]]
99
[[ ${lines[1]} == "corefunc_str_camel" ]]
10-
[[ ${lines[2]} == "corefunc_str_kebab" ]]
11-
[[ ${lines[3]} == "corefunc_str_pascal" ]]
12-
[[ ${lines[4]} == "corefunc_str_snake" ]]
13-
[[ ${lines[5]} == "corefunc_str_truncate_label" ]]
10+
[[ ${lines[2]} == "corefunc_str_constant" ]]
11+
[[ ${lines[3]} == "corefunc_str_kebab" ]]
12+
[[ ${lines[4]} == "corefunc_str_pascal" ]]
13+
[[ ${lines[5]} == "corefunc_str_snake" ]]
14+
[[ ${lines[6]} == "corefunc_str_truncate_label" ]]
1415

1516
}
1617

@@ -33,6 +34,15 @@
3334
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
3435
}
3536

37+
@test "corefunc_str_constant: attrs" {
38+
run bash -c "tfschema data show -format=json corefunc_str_constant | jq -Mrc '.attributes[]'"
39+
40+
[ "$status" -eq 0 ]
41+
[[ ${lines[0]} == '{"name":"id","type":"number","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
42+
[[ ${lines[1]} == '{"name":"string","type":"string","required":true,"optional":false,"computed":false,"sensitive":false}' ]]
43+
[[ ${lines[2]} == '{"name":"value","type":"string","required":false,"optional":false,"computed":true,"sensitive":false}' ]]
44+
}
45+
3646
@test "corefunc_str_kebab: attrs" {
3747
run bash -c "tfschema data show -format=json corefunc_str_kebab | jq -Mrc '.attributes[]'"
3848

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
EnvEnsureDataSource,
105105
StrCamelDataSource,
106+
StrConstantDataSource,
106107
StrKebabDataSource,
107108
StrPascalDataSource,
108109
StrSnakeDataSource,

corefuncprovider/str_camel_data_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (d strCamelDataSource) Create(
147147
}
148148

149149
// Read refreshes the Terraform state with the latest data.
150-
func (d *strCamelDataSource) Read(
150+
func (d *strCamelDataSource) Read( // lint:no_dupe
151151
ctx context.Context,
152152
_ datasource.ReadRequest, // lint:allow_large_memory
153153
resp *datasource.ReadResponse,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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 // lint:no_dupe
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 = &strConstantDataSource{}
34+
_ datasource.DataSourceWithConfigure = &strConstantDataSource{}
35+
)
36+
37+
// strConstantDataSource is the data source implementation.
38+
type (
39+
strConstantDataSource struct{}
40+
41+
// strConstantDataSourceModel maps the data source schema data.
42+
strConstantDataSourceModel struct {
43+
ID types.Int64 `tfsdk:"id"`
44+
String types.String `tfsdk:"string"`
45+
Value types.String `tfsdk:"value"`
46+
}
47+
)
48+
49+
// StrConstantDataSource is a method that exposes its paired Go function as a
50+
// Terraform Data Source.
51+
func StrConstantDataSource() datasource.DataSource { // lint:allow_return_interface
52+
return &strConstantDataSource{}
53+
}
54+
55+
// Metadata returns the data source type name.
56+
func (d *strConstantDataSource) Metadata(
57+
ctx context.Context,
58+
req datasource.MetadataRequest,
59+
resp *datasource.MetadataResponse,
60+
) {
61+
tflog.Info(ctx, "Starting StrConstant DataSource Metadata method.")
62+
63+
resp.TypeName = req.ProviderTypeName + "_str_constant"
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 StrConstant DataSource Metadata method.")
69+
}
70+
71+
// Schema defines the schema for the data source.
72+
func (d *strConstantDataSource) Schema(
73+
ctx context.Context,
74+
_ datasource.SchemaRequest,
75+
resp *datasource.SchemaResponse,
76+
) {
77+
tflog.Info(ctx, "Starting StrConstant DataSource Schema method.")
78+
79+
resp.Schema = schema.Schema{
80+
MarkdownDescription: strings.TrimSpace(dedent.Dedent(`
81+
Converts a string to ` + "`" + `CONSTANT_CASE` + "`" + `, removing any non-alphanumeric characters.
82+
Also known as ` + "`" + `SCREAMING_SNAKE_CASE` + "`" + `.
83+
84+
Maps to the [` + "`" + `caps.ToScreamingSnake()` + "`" +
85+
`](https://pkg.go.dev/github.com/chanced/caps#ToScreamingSnake)
86+
Go method, which can be used in ` + Terratest + `.
87+
`)),
88+
Attributes: map[string]schema.Attribute{
89+
"id": schema.Int64Attribute{
90+
Description: "Not used. Required by the " + TPF + ".",
91+
Computed: true,
92+
},
93+
"string": schema.StringAttribute{
94+
Description: "The string to convert to `CONSTANT_CASE`.",
95+
Required: true,
96+
},
97+
"value": schema.StringAttribute{
98+
Description: "The value of the string.",
99+
Computed: true,
100+
},
101+
},
102+
}
103+
104+
tflog.Info(ctx, "Ending StrConstant DataSource Schema method.")
105+
}
106+
107+
// Configure adds the provider configured client to the data source.
108+
func (d *strConstantDataSource) Configure(
109+
ctx context.Context,
110+
req datasource.ConfigureRequest,
111+
_ *datasource.ConfigureResponse,
112+
) {
113+
tflog.Info(ctx, "Starting StrConstant DataSource Configure method.")
114+
115+
if req.ProviderData == nil {
116+
return
117+
}
118+
119+
tflog.Info(ctx, "Ending StrConstant DataSource Configure method.")
120+
}
121+
122+
func (d strConstantDataSource) Create(
123+
ctx context.Context,
124+
req resource.CreateRequest, // lint:allow_large_memory
125+
resp *resource.CreateResponse,
126+
) {
127+
tflog.Info(ctx, "Starting StrConstant DataSource Create method.")
128+
129+
var plan strConstantDataSourceModel
130+
131+
diags := req.Plan.Get(ctx, &plan)
132+
resp.Diagnostics.Append(diags...)
133+
134+
if resp.Diagnostics.HasError() {
135+
return
136+
}
137+
138+
tflog.Info(ctx, "Ending StrConstant DataSource Create method.")
139+
}
140+
141+
// Read refreshes the Terraform state with the latest data.
142+
func (d *strConstantDataSource) Read( // lint:no_dupe
143+
ctx context.Context,
144+
_ datasource.ReadRequest, // lint:allow_large_memory
145+
resp *datasource.ReadResponse,
146+
) {
147+
tflog.Info(ctx, "Starting StrConstant DataSource Read method.")
148+
149+
var state strConstantDataSourceModel
150+
diags := resp.State.Get(ctx, &state)
151+
resp.Diagnostics.Append(diags...)
152+
153+
state.ID = types.Int64Value(1)
154+
155+
state.Value = types.StringValue(
156+
caps.ToScreamingSnake(
157+
state.String.ValueString(),
158+
),
159+
)
160+
161+
diags = resp.State.Set(ctx, &state)
162+
resp.Diagnostics.Append(diags...)
163+
164+
if resp.Diagnostics.HasError() {
165+
return
166+
}
167+
168+
tflog.Info(ctx, "Ending StrConstant DataSource Read method.")
169+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
data "corefunc_str_constant" "constant" {
2+
string = "{{ .Input }}"
3+
}
4+
#=> {{ .Expected }}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
"os"
22+
"strings"
23+
"testing"
24+
"text/template"
25+
26+
"github.com/northwood-labs/terraform-provider-corefunc/testfixtures"
27+
28+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
29+
)
30+
31+
func TestAccStrConstantDataSource(t *testing.T) {
32+
funcName := traceFuncName()
33+
34+
for name, tc := range testfixtures.StrConstantTestTable {
35+
fmt.Printf(
36+
"=== RUN %s/%s\n",
37+
strings.TrimSpace(funcName),
38+
strings.TrimSpace(name),
39+
)
40+
41+
buf := new(bytes.Buffer)
42+
tmpl := template.Must(
43+
template.ParseFiles("str_constant_data_source_fixture.tftpl"),
44+
)
45+
46+
err := tmpl.Execute(buf, tc)
47+
if err != nil {
48+
log.Fatalln(err)
49+
}
50+
51+
if os.Getenv("PROVIDER_DEBUG") != "" {
52+
fmt.Fprintln(os.Stderr, buf.String())
53+
}
54+
55+
resource.Test(t, resource.TestCase{
56+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
57+
Steps: []resource.TestStep{
58+
{
59+
Config: providerConfig + buf.String(),
60+
Check: resource.ComposeAggregateTestCheckFunc(
61+
resource.TestCheckResourceAttr("data.corefunc_str_constant.constant", "value", tc.Expected),
62+
),
63+
},
64+
},
65+
})
66+
}
67+
}

corefuncprovider/str_kebab_data_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (d strKebabDataSource) Create(
137137
}
138138

139139
// Read refreshes the Terraform state with the latest data.
140-
func (d *strKebabDataSource) Read(
140+
func (d *strKebabDataSource) Read( // lint:no_dupe
141141
ctx context.Context,
142142
_ datasource.ReadRequest, // lint:allow_large_memory
143143
resp *datasource.ReadResponse,

corefuncprovider/str_pascal_data_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ func (d strPascalDataSource) Create(
147147
}
148148

149149
// Read refreshes the Terraform state with the latest data.
150-
func (d *strPascalDataSource) Read(
150+
func (d *strPascalDataSource) Read( // lint:no_dupe
151151
ctx context.Context,
152152
_ datasource.ReadRequest, // lint:allow_large_memory
153153
resp *datasource.ReadResponse,

corefuncprovider/str_snake_data_source.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (d strSnakeDataSource) Create(
137137
}
138138

139139
// Read refreshes the Terraform state with the latest data.
140-
func (d *strSnakeDataSource) Read(
140+
func (d *strSnakeDataSource) Read( // lint:no_dupe
141141
ctx context.Context,
142142
_ datasource.ReadRequest, // lint:allow_large_memory
143143
resp *datasource.ReadResponse,

docs/data-sources/str_constant.md

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<!--
2+
---
3+
page_title: "corefunc_str_constant Data Source - corefunc"
4+
subcategory: ""
5+
description: |-
6+
Converts a string to CONSTANT_CASE, removing any non-alphanumeric characters.
7+
Also known as SCREAMING_SNAKE_CASE.
8+
Maps to the caps.ToScreamingSnake() https://pkg.go.dev/github.com/chanced/caps#ToScreamingSnake
9+
Go method, which can be used in Terratest https://terratest.gruntwork.io.
10+
---
11+
-->
12+
13+
# corefunc_str_constant (Data Source)
14+
15+
Converts a string to `CONSTANT_CASE`, removing any non-alphanumeric characters.
16+
Also known as `SCREAMING_SNAKE_CASE`.
17+
18+
Maps to the [`caps.ToScreamingSnake()`](https://pkg.go.dev/github.com/chanced/caps#ToScreamingSnake)
19+
Go method, which can be used in [Terratest](https://terratest.gruntwork.io).
20+
21+
## Example Usage
22+
23+
```terraform
24+
data "corefunc_str_snake" "v322" {
25+
string = "v3.2.2"
26+
}
27+
28+
#=> V3_2_2
29+
```
30+
31+
```terraform
32+
data "corefunc_str_snake" "test_from_camel" {
33+
string = "TestFromCamel"
34+
}
35+
36+
#=> TEST_FROM_CAMEL
37+
```
38+
39+
```terraform
40+
data "corefunc_str_snake" "test_with_number" {
41+
string = "test with number -123.456"
42+
}
43+
44+
#=> TEST_WITH_NUMBER_123_456
45+
```
46+
47+
```terraform
48+
data "corefunc_str_snake" "this_is_an_example" {
49+
string = "This is [an] {example}$${id32}."
50+
}
51+
52+
#=> THIS_IS_AN_EXAMPLE_ID_32
53+
```
54+
55+
<!-- schema generated by tfplugindocs -->
56+
## Schema
57+
58+
### Required
59+
60+
* `string` (String) The string to convert to `CONSTANT_CASE`.
61+
62+
### Read-Only
63+
64+
* `id` (Number) Not used. Required by the [Terraform Plugin Framework](https://developer.hashicorp.com/terraform/plugin/framework).
65+
* `value` (String) The value of the string.
66+
67+
<!-- 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,5 @@
1+
data "corefunc_str_snake" "test_from_camel" {
2+
string = "TestFromCamel"
3+
}
4+
5+
#=> TEST_FROM_CAMEL
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
data "corefunc_str_snake" "test_with_number" {
2+
string = "test with number -123.456"
3+
}
4+
5+
#=> TEST_WITH_NUMBER_123_456

0 commit comments

Comments
 (0)