Skip to content

Commit a31fc38

Browse files
committed
extract attribute package
1 parent b6b3471 commit a31fc38

File tree

4 files changed

+77
-72
lines changed

4 files changed

+77
-72
lines changed

attribute/attribute.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package attribute
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
7+
"github.com/hashicorp/terraform-plugin-go/tftypes"
8+
)
9+
10+
// AttributeType defines an interface for describing a kind of attribute.
11+
// AttributeTypes are collections of constraints and behaviors such that they
12+
// can be reused on multiple attributes easily.
13+
type AttributeType interface {
14+
// TerraformType returns the tftypes.Type that should be used to
15+
// represent this type. This constrains what user input will be
16+
// accepted and what kind of data can be set in state. The framework
17+
// will use this to translate the AttributeType to something Terraform
18+
// can understand.
19+
TerraformType(context.Context) tftypes.Type
20+
21+
// Validate returns any warnings or errors about the value that is
22+
// being used to populate the AttributeType. It is generally used to
23+
// check the data format and ensure that it complies with the
24+
// requirements of the AttributeType.
25+
//
26+
// TODO: don't use tfprotov6.Diagnostic, use our type
27+
Validate(context.Context, tftypes.Value) []*tfprotov6.Diagnostic
28+
29+
// Description returns a practitioner-friendly explanation of the type
30+
// and the constraints of the data it accepts and returns. It will be
31+
// combined with the Description associated with the Attribute.
32+
Description(context.Context, StringKind) string
33+
34+
// ValueFromTerraform returns an AttributeValue given a tftypes.Value.
35+
// This is meant to convert the tftypes.Value into a more convenient Go
36+
// type for the provider to consume the data with.
37+
ValueFromTerraform(context.Context, tftypes.Value) (AttributeValue, error)
38+
}
39+
40+
// StringKind represents a kind of string formatting.
41+
type StringKind uint8
42+
43+
// NestingMode represents a specific way a group of attributes can be nested.
44+
type NestingMode uint8
45+
46+
// AttributeValue defines an interface for describing data associated with an
47+
// attribute. AttributeValues allow provider developers to specify data in a
48+
// convenient format, and have it transparently be converted to formats
49+
// Terraform understands.
50+
type AttributeValue interface {
51+
// ToTerraformValue returns the data contained in the AttributeValue as
52+
// a Go type that tftypes.NewValue will accept.
53+
ToTerraformValue(context.Context) (interface{}, error)
54+
55+
// Equal must return true if the AttributeValue is considered
56+
// semantically equal to the AttributeValue passed as an argument.
57+
Equal(AttributeValue) bool
58+
}

schema.go

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package tf
22

33
import (
4-
"context"
5-
6-
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
7-
"github.com/hashicorp/terraform-plugin-go/tftypes"
4+
"github.com/hashicorp/terraform-plugin-framework/attribute"
85
)
96

107
const (
11-
NestingModeSingle NestingMode = 0
12-
NestingModeList NestingMode = 1
13-
NestingModeSet NestingMode = 2
14-
NestingModeMap NestingMode = 3
8+
NestingModeSingle attribute.NestingMode = 0
9+
NestingModeList attribute.NestingMode = 1
10+
NestingModeSet attribute.NestingMode = 2
11+
NestingModeMap attribute.NestingMode = 3
1512
)
1613

1714
// Schema is used to define the shape of practitioner-provider information,
@@ -39,7 +36,7 @@ type Attribute struct {
3936
// want to use one of the types in the types package.
4037
//
4138
// If Type is set, Attributes cannot be.
42-
Type AttributeType
39+
Type attribute.AttributeType
4340

4441
// Attributes can have their own, nested attributes. This nested map of
4542
// attributes behaves exactly like the map of attributes on the Schema
@@ -54,7 +51,7 @@ type Attribute struct {
5451
// AttributesNestingMode controls the various ways these sub-groups of
5552
// attributes can behave. It can only be used with Attributes, and must
5653
// not be set if Type is set.
57-
AttributesNestingMode NestingMode
54+
AttributesNestingMode attribute.NestingMode
5855

5956
// Description is used in various tooling, like the documentation
6057
// generator and the language server, to give practitioners more
@@ -66,7 +63,7 @@ type Attribute struct {
6663
// Description uses. It should be Markdown or PlainText.
6764
//
6865
// TODO: come up with a better interface for this, this is weird.
69-
DescriptionKind StringKind
66+
DescriptionKind attribute.StringKind
7067

7168
// Required indicates whether the practitioner must enter a value for
7269
// this attribute or not. Required and Optional cannot both be true,
@@ -97,53 +94,3 @@ type Attribute struct {
9794
// instructing them on what upgrade steps to take.
9895
DeprecationMessage string
9996
}
100-
101-
// AttributeType defines an interface for describing a kind of attribute.
102-
// AttributeTypes are collections of constraints and behaviors such that they
103-
// can be reused on multiple attributes easily.
104-
type AttributeType interface {
105-
// TerraformType returns the tftypes.Type that should be used to
106-
// represent this type. This constrains what user input will be
107-
// accepted and what kind of data can be set in state. The framework
108-
// will use this to translate the AttributeType to something Terraform
109-
// can understand.
110-
TerraformType(context.Context) tftypes.Type
111-
112-
// Validate returns any warnings or errors about the value that is
113-
// being used to populate the AttributeType. It is generally used to
114-
// check the data format and ensure that it complies with the
115-
// requirements of the AttributeType.
116-
//
117-
// TODO: don't use tfprotov6.Diagnostic, use our type
118-
Validate(context.Context, tftypes.Value) []*tfprotov6.Diagnostic
119-
120-
// Description returns a practitioner-friendly explanation of the type
121-
// and the constraints of the data it accepts and returns. It will be
122-
// combined with the Description associated with the Attribute.
123-
Description(context.Context, StringKind) string
124-
125-
// ValueFromTerraform returns an AttributeValue given a tftypes.Value.
126-
// This is meant to convert the tftypes.Value into a more convenient Go
127-
// type for the provider to consume the data with.
128-
ValueFromTerraform(context.Context, tftypes.Value) (AttributeValue, error)
129-
}
130-
131-
// StringKind represents a kind of string formatting.
132-
type StringKind uint8
133-
134-
// NestingMode represents a specific way a group of attributes can be nested.
135-
type NestingMode uint8
136-
137-
// AttributeValue defines an interface for describing data associated with an
138-
// attribute. AttributeValues allow provider developers to specify data in a
139-
// convenient format, and have it transparently be converted to formats
140-
// Terraform understands.
141-
type AttributeValue interface {
142-
// ToTerraformValue returns the data contained in the AttributeValue as
143-
// a Go type that tftypes.NewValue will accept.
144-
ToTerraformValue(context.Context) (interface{}, error)
145-
146-
// Equal must return true if the AttributeValue is considered
147-
// semantically equal to the AttributeValue passed as an argument.
148-
Equal(AttributeValue) bool
149-
}

types/list.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package types
33
import (
44
"context"
55

6-
tf "github.com/hashicorp/terraform-plugin-framework"
6+
"github.com/hashicorp/terraform-plugin-framework/attribute"
77
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
88
"github.com/hashicorp/terraform-plugin-go/tftypes"
99
)
1010

1111
type ListType struct {
12-
ElemType tf.AttributeType
12+
ElemType attribute.AttributeType
1313
}
1414

1515
// TerraformType returns the tftypes.Type that should be used to
@@ -36,14 +36,14 @@ func (l ListType) Validate(_ context.Context, _ tftypes.Value) []*tfprotov6.Diag
3636
// Description returns a practitioner-friendly explanation of the type
3737
// and the constraints of the data it accepts and returns. It will be
3838
// combined with the Description associated with the Attribute.
39-
func (l ListType) Description(_ context.Context, _ tf.StringKind) string {
39+
func (l ListType) Description(_ context.Context, _ attribute.StringKind) string {
4040
return ""
4141
}
4242

4343
// ValueFromTerraform returns an AttributeValue given a tftypes.Value.
4444
// This is meant to convert the tftypes.Value into a more convenient Go
4545
// type for the provider to consume the data with.
46-
func (l ListType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (tf.AttributeValue, error) {
46+
func (l ListType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (attribute.AttributeValue, error) {
4747
if !in.IsKnown() {
4848
return List{
4949
Unknown: true,
@@ -59,7 +59,7 @@ func (l ListType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (tf.
5959
if err != nil {
6060
return nil, err
6161
}
62-
elems := make([]tf.AttributeValue, 0, len(val))
62+
elems := make([]attribute.AttributeValue, 0, len(val))
6363
for _, elem := range val {
6464
av, err := l.ElemType.ValueFromTerraform(ctx, elem)
6565
if err != nil {
@@ -76,7 +76,7 @@ func (l ListType) ValueFromTerraform(ctx context.Context, in tftypes.Value) (tf.
7676
type List struct {
7777
Unknown bool
7878
Null bool
79-
Elems []tf.AttributeValue
79+
Elems []attribute.AttributeValue
8080
ElemType tftypes.Type
8181
}
8282

@@ -106,7 +106,7 @@ func (l List) ToTerraformValue(ctx context.Context) (interface{}, error) {
106106

107107
// Equal must return true if the AttributeValue is considered
108108
// semantically equal to the AttributeValue passed as an argument.
109-
func (l List) Equal(o tf.AttributeValue) bool {
109+
func (l List) Equal(o attribute.AttributeValue) bool {
110110
other, ok := o.(List)
111111
if !ok {
112112
return false

types/string.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package types
33
import (
44
"context"
55

6-
tf "github.com/hashicorp/terraform-plugin-framework"
6+
"github.com/hashicorp/terraform-plugin-framework/attribute"
77
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
88
"github.com/hashicorp/terraform-plugin-go/tftypes"
99
)
@@ -32,14 +32,14 @@ func (s StringType) Validate(_ context.Context, _ tftypes.Value) []*tfprotov6.Di
3232
// Description returns a practitioner-friendly explanation of the type
3333
// and the constraints of the data it accepts and returns. It will be
3434
// combined with the Description associated with the Attribute.
35-
func (s StringType) Description(_ context.Context, _ tf.StringKind) string {
35+
func (s StringType) Description(_ context.Context, _ attribute.StringKind) string {
3636
return ""
3737
}
3838

3939
// ValueFromTerraform returns an AttributeValue given a tftypes.Value.
4040
// This is meant to convert the tftypes.Value into a more convenient Go
4141
// type for the provider to consume the data with.
42-
func (s StringType) ValueFromTerraform(_ context.Context, in tftypes.Value) (tf.AttributeValue, error) {
42+
func (s StringType) ValueFromTerraform(_ context.Context, in tftypes.Value) (attribute.AttributeValue, error) {
4343
var val String
4444
if !in.IsKnown() {
4545
val.Unknown = true
@@ -73,7 +73,7 @@ func (s String) ToTerraformValue(_ context.Context) (interface{}, error) {
7373

7474
// Equal must return true if the AttributeValue is considered
7575
// semantically equal to the AttributeValue passed as an argument.
76-
func (s String) Equal(other tf.AttributeValue) bool {
76+
func (s String) Equal(other attribute.AttributeValue) bool {
7777
o, ok := other.(String)
7878
if !ok {
7979
return false

0 commit comments

Comments
 (0)