Skip to content

Commit f42f07d

Browse files
committed
add cut function prototype
1 parent 1344f95 commit f42f07d

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

stdlib/string/cut_data_source.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package stringfunc
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57

68
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
79
"github.com/hashicorp/terraform-plugin-framework/datasource"
810
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
911
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1012
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"github.com/hashicorp/terraform-plugin-log/tflog"
1114

1215
util "github.com/mschuchard/terraform-provider-stdlib/internal"
1316
)
@@ -52,7 +55,7 @@ func (*cutDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp
5255
},
5356
"separator": schema.StringAttribute{
5457
Description: "The separator for cutting the input string.",
55-
Optional: true,
58+
Required: true,
5659
Validators: []validator.String{
5760
stringvalidator.LengthAtLeast(1),
5861
},
@@ -82,4 +85,30 @@ func (*cutDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp
8285
if resp.Diagnostics.HasError() {
8386
return
8487
}
88+
89+
// initialize input string param and separator
90+
inputString := state.Param.ValueString()
91+
separator := state.Separator.ValueString()
92+
93+
// determine string cut
94+
before, after, found := strings.Cut(inputString, separator)
95+
96+
// provide debug logging
97+
ctx = tflog.SetField(ctx, "stdlib_cut_before", before)
98+
ctx = tflog.SetField(ctx, "stdlib_cut_after", after)
99+
ctx = tflog.SetField(ctx, "stdlib_cut_found", found)
100+
tflog.Debug(ctx, fmt.Sprintf("Input string parameter \"%s\" with separator \"%s\" has before \"%s\", after \"%s\", and found \"%t\"", inputString, separator, before, after, found))
101+
102+
// store returned values in state
103+
state.ID = types.StringValue(inputString)
104+
state.Before = types.StringValue(before)
105+
state.After = types.StringValue(after)
106+
state.Found = types.BoolValue(found)
107+
108+
// set state
109+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
110+
if resp.Diagnostics.HasError() {
111+
return
112+
}
113+
tflog.Info(ctx, "Determined string cut", map[string]any{"success": true})
85114
}

0 commit comments

Comments
 (0)