Skip to content

Commit 074bdbb

Browse files
committed
set empty objects in NewListResult as well as update Set method on tfsdk types to allow tftype values
1 parent 3a3e409 commit 074bdbb

File tree

5 files changed

+52
-6
lines changed

5 files changed

+52
-6
lines changed

internal/fwschemadata/data_set.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,17 @@ import (
1010
"github.com/hashicorp/terraform-plugin-framework/diag"
1111
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
1212
"github.com/hashicorp/terraform-plugin-framework/path"
13+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1314
)
1415

1516
// Set replaces the entire value. The value should be a struct whose fields
1617
// have one of the attr.Value types. Each field must have the tfsdk field tag.
1718
func (d *Data) Set(ctx context.Context, val any) diag.Diagnostics {
19+
if v, ok := val.(tftypes.Value); ok {
20+
d.TerraformValue = v
21+
return nil
22+
}
23+
1824
attrValue, diags := reflect.FromValue(ctx, d.Schema.Type(), val, path.Empty())
1925

2026
if diags.HasError() {

internal/fwschemadata/data_set_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,39 @@ func TestDataSet(t *testing.T) {
139139
),
140140
}),
141141
},
142+
"write-tftypes-values": {
143+
data: fwschemadata.Data{
144+
TerraformValue: tftypes.NewValue(tftypes.Object{
145+
AttributeTypes: map[string]tftypes.Type{
146+
"name": tftypes.String,
147+
},
148+
}, map[string]tftypes.Value{
149+
"name": tftypes.NewValue(tftypes.String, "oldvalue"),
150+
}),
151+
Schema: testschema.Schema{
152+
Attributes: map[string]fwschema.Attribute{
153+
"name": testschema.Attribute{
154+
Type: types.StringType,
155+
Required: true,
156+
},
157+
},
158+
},
159+
},
160+
val: tftypes.NewValue(tftypes.Object{
161+
AttributeTypes: map[string]tftypes.Type{
162+
"name": tftypes.String,
163+
},
164+
}, map[string]tftypes.Value{
165+
"name": tftypes.NewValue(tftypes.String, "newvalue"),
166+
}),
167+
expected: tftypes.NewValue(tftypes.Object{
168+
AttributeTypes: map[string]tftypes.Type{
169+
"name": tftypes.String,
170+
},
171+
}, map[string]tftypes.Value{
172+
"name": tftypes.NewValue(tftypes.String, "newvalue"),
173+
}),
174+
},
142175
"overwrite": {
143176
data: fwschemadata.Data{
144177
TerraformValue: tftypes.Value{},

internal/proto5server/server_listresource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestServerListResource(t *testing.T) {
105105
continue
106106
}
107107

108-
result := req.NewListResult()
108+
result := req.NewListResult(ctx)
109109
result.DisplayName = name
110110

111111
diags = result.Identity.Set(ctx, resources[name].ThingResourceIdentity)

internal/proto6server/server_listresource_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestServerListResource(t *testing.T) {
105105
continue
106106
}
107107

108-
result := req.NewListResult()
108+
result := req.NewListResult(ctx)
109109
result.DisplayName = name
110110

111111
diags = result.Identity.Set(ctx, resources[name].ThingResourceIdentity)
@@ -131,7 +131,7 @@ func TestServerListResource(t *testing.T) {
131131
}
132132

133133
r.ListMethod = func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) {
134-
result := req.NewListResult()
134+
result := req.NewListResult(ctx)
135135
result.DisplayName = "plateau"
136136

137137
diags := result.Identity.Set(ctx, resources["plateau"].ThingResourceIdentity)

list/list_resource.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
1212
"github.com/hashicorp/terraform-plugin-framework/resource"
1313
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
14+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1415
)
1516

1617
// ListResource represents an implementation of listing instances of a managed resource
@@ -109,9 +110,15 @@ type ListRequest struct {
109110

110111
// NewListResult creates a new [ListResult] with convenient defaults
111112
// for each field.
112-
func (r ListRequest) NewListResult() ListResult {
113-
identity := &tfsdk.ResourceIdentity{Schema: r.ResourceIdentitySchema}
114-
resource := &tfsdk.Resource{Schema: r.ResourceSchema}
113+
func (r ListRequest) NewListResult(ctx context.Context) ListResult {
114+
identity := &tfsdk.ResourceIdentity{
115+
Raw: tftypes.NewValue(r.ResourceIdentitySchema.Type().TerraformType(ctx), nil),
116+
Schema: r.ResourceIdentitySchema,
117+
}
118+
resource := &tfsdk.Resource{
119+
Raw: tftypes.NewValue(r.ResourceSchema.Type().TerraformType(ctx), nil),
120+
Schema: r.ResourceSchema,
121+
}
115122

116123
return ListResult{
117124
DisplayName: "",

0 commit comments

Comments
 (0)