Skip to content

Commit 9a9a2ea

Browse files
authored
Fix Type implementations for terraform-plugin-framework 1.3+ (#72)
Reference: hashicorp/terraform-plugin-framework#793 Reference: https://developer.hashicorp.com/terraform/plugin/framework/handling-data/custom-types#developing-custom-types Verified with terraform-provider-corner.
1 parent 51b807d commit 9a9a2ea

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'datasource/timeouts: Prevented `Value Conversion Error` with terraform-plugin-framework
3+
1.3.0 and later'
4+
time: 2023-07-06T17:28:42.871268-04:00
5+
custom:
6+
Issue: "72"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: BUG FIXES
2+
body: 'resource/timeouts: Prevented `Value Conversion Error` with terraform-plugin-framework
3+
1.3.0 and later'
4+
time: 2023-07-06T17:29:07.283097-04:00
5+
custom:
6+
Issue: "72"

datasource/timeouts/timeouts.go

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/attr"
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1415
"github.com/hashicorp/terraform-plugin-go/tftypes"
1516
"github.com/hashicorp/terraform-plugin-log/tflog"
1617
)
1718

19+
var (
20+
_ basetypes.ObjectTypable = Type{}
21+
_ basetypes.ObjectValuable = Value{}
22+
)
23+
1824
// Type is an attribute type that represents timeouts.
1925
type Type struct {
20-
types.ObjectType
26+
basetypes.ObjectType
27+
}
28+
29+
// String returns a human-readable representation of the type.
30+
func (t Type) String() string {
31+
return "timeouts.Type"
32+
}
33+
34+
// ValueFromObject returns a Value given a basetypes.ObjectValue.
35+
func (t Type) ValueFromObject(_ context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
36+
value := Value{
37+
Object: in,
38+
}
39+
40+
return value, nil
2141
}
2242

2343
// ValueFromTerraform returns a Value given a tftypes.Value.
@@ -39,7 +59,13 @@ func (t Type) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Va
3959
}, err
4060
}
4161

42-
// Equal returns true if `candidate` is also an Type and has the same
62+
// ValueType returns the associated Value type for debugging.
63+
func (t Type) ValueType(context.Context) attr.Value {
64+
// It does not need to be a fully valid implementation of the type.
65+
return Value{}
66+
}
67+
68+
// Equal returns true if `candidate` is also a Type and has the same
4369
// AttributeTypes.
4470
func (t Type) Equal(candidate attr.Type) bool {
4571
other, ok := candidate.(Type)
@@ -67,6 +93,11 @@ func (t Value) Equal(c attr.Value) bool {
6793
return t.Object.Equal(other.Object)
6894
}
6995

96+
// ToObjectValue returns the underlying ObjectValue.
97+
func (v Value) ToObjectValue(_ context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
98+
return v.Object, nil
99+
}
100+
70101
// Type returns a Type with the same attribute types as `t`.
71102
func (t Value) Type(ctx context.Context) attr.Type {
72103
return Type{

resource/timeouts/timeouts.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,33 @@ import (
1111
"github.com/hashicorp/terraform-plugin-framework/attr"
1212
"github.com/hashicorp/terraform-plugin-framework/diag"
1313
"github.com/hashicorp/terraform-plugin-framework/types"
14+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1415
"github.com/hashicorp/terraform-plugin-go/tftypes"
1516
"github.com/hashicorp/terraform-plugin-log/tflog"
1617
)
1718

19+
var (
20+
_ basetypes.ObjectTypable = Type{}
21+
_ basetypes.ObjectValuable = Value{}
22+
)
23+
1824
// Type is an attribute type that represents timeouts.
1925
type Type struct {
20-
types.ObjectType
26+
basetypes.ObjectType
27+
}
28+
29+
// String returns a human-readable representation of the type.
30+
func (t Type) String() string {
31+
return "timeouts.Type"
32+
}
33+
34+
// ValueFromObject returns a Value given a basetypes.ObjectValue.
35+
func (t Type) ValueFromObject(_ context.Context, in basetypes.ObjectValue) (basetypes.ObjectValuable, diag.Diagnostics) {
36+
value := Value{
37+
Object: in,
38+
}
39+
40+
return value, nil
2141
}
2242

2343
// ValueFromTerraform returns a Value given a tftypes.Value.
@@ -39,6 +59,12 @@ func (t Type) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attr.Va
3959
}, err
4060
}
4161

62+
// ValueType returns the associated Value type for debugging.
63+
func (t Type) ValueType(context.Context) attr.Value {
64+
// It does not need to be a fully valid implementation of the type.
65+
return Value{}
66+
}
67+
4268
// Equal returns true if `candidate` is also a Type and has the same
4369
// AttributeTypes.
4470
func (t Type) Equal(candidate attr.Type) bool {
@@ -67,6 +93,11 @@ func (t Value) Equal(c attr.Value) bool {
6793
return t.Object.Equal(other.Object)
6894
}
6995

96+
// ToObjectValue returns the underlying ObjectValue.
97+
func (v Value) ToObjectValue(_ context.Context) (basetypes.ObjectValue, diag.Diagnostics) {
98+
return v.Object, nil
99+
}
100+
70101
// Type returns a Type with the same attribute types as `t`.
71102
func (t Value) Type(ctx context.Context) attr.Type {
72103
return Type{

0 commit comments

Comments
 (0)