Skip to content

[DRAFT] Transcribe proposed new state handling from Terraform Core #1156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions datasource/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Schema struct {
DeprecationMessage string
}

func (s Schema) EmptyValue(ctx context.Context) tftypes.Value {
return fwschema.EmptySchemaValue(ctx, s)
}

// ApplyTerraform5AttributePathStep applies the given AttributePathStep to the
// schema.
func (s Schema) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (any, error) {
Expand Down
4 changes: 4 additions & 0 deletions ephemeral/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ type Schema struct {
DeprecationMessage string
}

func (s Schema) EmptyValue(ctx context.Context) tftypes.Value {
return fwschema.EmptySchemaValue(ctx, s)
}

// ApplyTerraform5AttributePathStep applies the given AttributePathStep to the
// schema.
func (s Schema) ApplyTerraform5AttributePathStep(step tftypes.AttributePathStep) (any, error) {
Expand Down
3 changes: 2 additions & 1 deletion internal/fwschema/nested_block_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ package fwschema
import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// NestedBlockObject represents the Object inside a Block.
Expand Down
16 changes: 16 additions & 0 deletions internal/fwschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ type Schema interface {
// AttributeTypeAtPath should return the framework type of the Attribute at
// the given Terraform path or return an error.
TypeAtTerraformPath(context.Context, *tftypes.AttributePath) (attr.Type, error)

// EmptyValue should return an empty tftypes.Value of the schema's TerraformType
EmptyValue(context.Context) tftypes.Value
}

// SchemaApplyTerraform5AttributePathStep is a helper function to perform base
Expand Down Expand Up @@ -195,6 +198,19 @@ func SchemaTypeAtPath(ctx context.Context, s Schema, p path.Path) (attr.Type, di
return attrType, diags
}

func EmptySchemaValue(ctx context.Context, s Schema) tftypes.Value {
vals := make(map[string]tftypes.Value)
for name, attr := range s.GetAttributes() {
attr.GetType()
vals[name] = tftypes.NewValue(attr.GetType().TerraformType(ctx), nil)
}
for name, block := range s.GetBlocks() {
vals[name] = tftypes.NewValue(block.Type().TerraformType(ctx), nil)
}

return tftypes.NewValue(s.Type().TerraformType(ctx), vals)
}

// SchemaTypeAtTerraformPath is a helper function to perform base type handling
// using the tftypes.AttributePathStepper interface.
func SchemaTypeAtTerraformPath(ctx context.Context, s Schema, p *tftypes.AttributePath) (attr.Type, error) {
Expand Down
Loading
Loading