Skip to content

Commit

Permalink
types: Migrate type implementations into basetypes subpackage (#567)
Browse files Browse the repository at this point in the history
Reference: #91

Aliasing and function shadowing in the original `types` package should prevent most provider developer changes. The main exception is the newer type-specific `Typable` and `Valuable` interfaces were moved without a type alias. This should help developers find the necessary interfaces for custom types next to the base type implementations.

The underlying implementation of the primitive types (`BoolType`, `Float64Type`, `Int64Type`, `NumberType`, and `StringType`) are now fully exported types instead of the unexported `primitive` type which was difficult to extend.

The underlying value type creation functions were prefixed with New and the value types themselves were renamed to include Value at the end. This should prevent rough edges with the `String` value type since it conflicted with the `String()` method and could not be directly embedded easily.
  • Loading branch information
bflad committed Dec 12, 2022
1 parent 23973ba commit 7afa862
Show file tree
Hide file tree
Showing 167 changed files with 3,677 additions and 3,442 deletions.
11 changes: 11 additions & 0 deletions .changelog/567.txt
@@ -0,0 +1,11 @@
```release-note:feature
types/basetypes: New package which contains embeddable types for custom types
```

```release-note:breaking-change
types: The type-specific `Typable` and `Valuable` interfaces have been moved into the underlying `basetypes` package.
```

```release-note:note
types: Framework type implementations have been moved into the underlying `basetypes` package. Value creation functions and type aliases have been created in the `types` package that should prevent any breaking changes.
```
5 changes: 3 additions & 2 deletions datasource/schema/bool_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -29,9 +30,9 @@ var (
// .example_attribute
type BoolAttribute struct {
// CustomType enables the use of a custom attribute type in place of the
// default types.BoolType. When retrieving data, the types.BoolValuable
// default basetypes.BoolType. When retrieving data, the basetypes.BoolValuable
// associated with this custom type must be used in place of types.Bool.
CustomType types.BoolTypable
CustomType basetypes.BoolTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/bool_attribute_test.go
Expand Up @@ -29,25 +29,25 @@ func TestBoolAttributeApplyTerraform5AttributePathStep(t *testing.T) {
attribute: schema.BoolAttribute{},
step: tftypes.AttributeName("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to types.BoolType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to basetypes.BoolType"),
},
"ElementKeyInt": {
attribute: schema.BoolAttribute{},
step: tftypes.ElementKeyInt(1),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to types.BoolType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to basetypes.BoolType"),
},
"ElementKeyString": {
attribute: schema.BoolAttribute{},
step: tftypes.ElementKeyString("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to types.BoolType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to basetypes.BoolType"),
},
"ElementKeyValue": {
attribute: schema.BoolAttribute{},
step: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to types.BoolType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to basetypes.BoolType"),
},
}

Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/float64_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -32,9 +33,9 @@ var (
// .example_attribute
type Float64Attribute struct {
// CustomType enables the use of a custom attribute type in place of the
// default types.Float64Type. When retrieving data, the types.Float64Valuable
// default basetypes.Float64Type. When retrieving data, the basetypes.Float64Valuable
// associated with this custom type must be used in place of types.Float64.
CustomType types.Float64Typable
CustomType basetypes.Float64Typable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/float64_attribute_test.go
Expand Up @@ -28,25 +28,25 @@ func TestFloat64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
attribute: schema.Float64Attribute{},
step: tftypes.AttributeName("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to types.Float64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to basetypes.Float64Type"),
},
"ElementKeyInt": {
attribute: schema.Float64Attribute{},
step: tftypes.ElementKeyInt(1),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to types.Float64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to basetypes.Float64Type"),
},
"ElementKeyString": {
attribute: schema.Float64Attribute{},
step: tftypes.ElementKeyString("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to types.Float64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to basetypes.Float64Type"),
},
"ElementKeyValue": {
attribute: schema.Float64Attribute{},
step: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to types.Float64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to basetypes.Float64Type"),
},
}

Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/int64_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -32,9 +33,9 @@ var (
// .example_attribute
type Int64Attribute struct {
// CustomType enables the use of a custom attribute type in place of the
// default types.Int64Type. When retrieving data, the types.Int64Valuable
// default basetypes.Int64Type. When retrieving data, the basetypes.Int64Valuable
// associated with this custom type must be used in place of types.Int64.
CustomType types.Int64Typable
CustomType basetypes.Int64Typable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/int64_attribute_test.go
Expand Up @@ -28,25 +28,25 @@ func TestInt64AttributeApplyTerraform5AttributePathStep(t *testing.T) {
attribute: schema.Int64Attribute{},
step: tftypes.AttributeName("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to types.Int64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to basetypes.Int64Type"),
},
"ElementKeyInt": {
attribute: schema.Int64Attribute{},
step: tftypes.ElementKeyInt(1),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to types.Int64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to basetypes.Int64Type"),
},
"ElementKeyString": {
attribute: schema.Int64Attribute{},
step: tftypes.ElementKeyString("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to types.Int64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to basetypes.Int64Type"),
},
"ElementKeyValue": {
attribute: schema.Int64Attribute{},
step: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to types.Int64Type"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to basetypes.Int64Type"),
},
}

Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/list_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -40,9 +41,9 @@ type ListAttribute struct {
ElementType attr.Type

// CustomType enables the use of a custom attribute type in place of the
// default types.ListType. When retrieving data, the types.ListValuable
// default basetypes.ListType. When retrieving data, the basetypes.ListValuable
// associated with this custom type must be used in place of types.List.
CustomType types.ListTypable
CustomType basetypes.ListTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/list_nested_attribute.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -51,9 +52,9 @@ type ListNestedAttribute struct {

// CustomType enables the use of a custom attribute type in place of the
// default types.ListType of types.ObjectType. When retrieving data, the
// types.ListValuable associated with this custom type must be used in
// basetypes.ListValuable associated with this custom type must be used in
// place of types.List.
CustomType types.ListTypable
CustomType basetypes.ListTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/list_nested_block.go
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -55,9 +56,9 @@ type ListNestedBlock struct {

// CustomType enables the use of a custom attribute type in place of the
// default types.ListType of types.ObjectType. When retrieving data, the
// types.ListValuable associated with this custom type must be used in
// basetypes.ListValuable associated with this custom type must be used in
// place of types.List.
CustomType types.ListTypable
CustomType basetypes.ListTypable

// Description is used in various tooling, like the language server, to
// give practitioners more information about what this attribute is,
Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/map_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -43,9 +44,9 @@ type MapAttribute struct {
ElementType attr.Type

// CustomType enables the use of a custom attribute type in place of the
// default types.MapType. When retrieving data, the types.MapValuable
// default basetypes.MapType. When retrieving data, the basetypes.MapValuable
// associated with this custom type must be used in place of types.Map.
CustomType types.MapTypable
CustomType basetypes.MapTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/map_nested_attribute.go
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)

// Ensure the implementation satisifies the desired interfaces.
Expand Down Expand Up @@ -52,9 +53,9 @@ type MapNestedAttribute struct {

// CustomType enables the use of a custom attribute type in place of the
// default types.MapType of types.ObjectType. When retrieving data, the
// types.MapValuable associated with this custom type must be used in
// basetypes.MapValuable associated with this custom type must be used in
// place of types.Map.
CustomType types.MapTypable
CustomType basetypes.MapTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/nested_attribute_object.go
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand All @@ -27,9 +27,9 @@ type NestedAttributeObject struct {
Attributes map[string]Attribute

// CustomType enables the use of a custom attribute type in place of the
// default types.ObjectType. When retrieving data, the types.ObjectValuable
// default basetypes.ObjectType. When retrieving data, the basetypes.ObjectValuable
// associated with this custom type must be used in place of types.Object.
CustomType types.ObjectTypable
CustomType basetypes.ObjectTypable

// Validators define value validation functionality for the attribute. All
// elements of the slice of AttributeValidator are run, regardless of any
Expand Down Expand Up @@ -70,7 +70,7 @@ func (o NestedAttributeObject) ObjectValidators() []validator.Object {
}

// Type returns the framework type of the NestedAttributeObject.
func (o NestedAttributeObject) Type() types.ObjectTypable {
func (o NestedAttributeObject) Type() basetypes.ObjectTypable {
if o.CustomType != nil {
return o.CustomType
}
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/nested_block_object.go
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -34,9 +34,9 @@ type NestedBlockObject struct {
Blocks map[string]Block

// CustomType enables the use of a custom attribute type in place of the
// default types.ObjectType. When retrieving data, the types.ObjectValuable
// default basetypes.ObjectType. When retrieving data, the basetypes.ObjectValuable
// associated with this custom type must be used in place of types.Object.
CustomType types.ObjectTypable
CustomType basetypes.ObjectTypable

// Validators define value validation functionality for the attribute. All
// elements of the slice of AttributeValidator are run, regardless of any
Expand Down Expand Up @@ -82,7 +82,7 @@ func (o NestedBlockObject) ObjectValidators() []validator.Object {
}

// Type returns the framework type of the NestedBlockObject.
func (o NestedBlockObject) Type() types.ObjectTypable {
func (o NestedBlockObject) Type() basetypes.ObjectTypable {
if o.CustomType != nil {
return o.CustomType
}
Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/number_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -33,9 +34,9 @@ var (
// .example_attribute
type NumberAttribute struct {
// CustomType enables the use of a custom attribute type in place of the
// default types.NumberType. When retrieving data, the types.NumberValuable
// default basetypes.NumberType. When retrieving data, the basetypes.NumberValuable
// associated with this custom type must be used in place of types.Number.
CustomType types.NumberTypable
CustomType basetypes.NumberTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down
8 changes: 4 additions & 4 deletions datasource/schema/number_attribute_test.go
Expand Up @@ -29,25 +29,25 @@ func TestNumberAttributeApplyTerraform5AttributePathStep(t *testing.T) {
attribute: schema.NumberAttribute{},
step: tftypes.AttributeName("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to types.NumberType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.AttributeName to basetypes.NumberType"),
},
"ElementKeyInt": {
attribute: schema.NumberAttribute{},
step: tftypes.ElementKeyInt(1),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to types.NumberType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyInt to basetypes.NumberType"),
},
"ElementKeyString": {
attribute: schema.NumberAttribute{},
step: tftypes.ElementKeyString("test"),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to types.NumberType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyString to basetypes.NumberType"),
},
"ElementKeyValue": {
attribute: schema.NumberAttribute{},
step: tftypes.ElementKeyValue(tftypes.NewValue(tftypes.String, "test")),
expected: nil,
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to types.NumberType"),
expectedError: fmt.Errorf("cannot apply AttributePathStep tftypes.ElementKeyValue to basetypes.NumberType"),
},
}

Expand Down
5 changes: 3 additions & 2 deletions datasource/schema/object_attribute.go
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema/fwxschema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

Expand Down Expand Up @@ -42,9 +43,9 @@ type ObjectAttribute struct {
AttributeTypes map[string]attr.Type

// CustomType enables the use of a custom attribute type in place of the
// default types.ObjectType. When retrieving data, the types.ObjectValuable
// default basetypes.ObjectType. When retrieving data, the basetypes.ObjectValuable
// associated with this custom type must be used in place of types.Object.
CustomType types.ObjectTypable
CustomType basetypes.ObjectTypable

// Required indicates whether the practitioner must enter a value for
// this attribute or not. Required and Optional cannot both be true,
Expand Down

0 comments on commit 7afa862

Please sign in to comment.