Skip to content

Commit

Permalink
internal/fwschemadata: Initial package (hashicorp#451)
Browse files Browse the repository at this point in the history
Reference: hashicorp#366

This change creates the initial `internal/fwschemadata` package, which will hold the shared implementation details of schema-based data, such as the existing `tfsdk.Config`, `tfsdk.Plan`, and `tfsdk.State` types and potentially other implementations in the future. This refactoring will be gradual over time to reduce review burden and should not introduce provider developer changes until potentially at the end of the refactoring.

The main interaction point will be the `Data` type, which currently contains the schema and terraform-plugin-go value information. This type may be expanded in the future to support diagnostic message customization (e.g. denoting the data is a "configuration") and eventually could support value migration to a framework native type (e.g. `types.Object`).

The initial functionality refactored is:

- Fetch and reflect entire value (`Get()` method)
- Path existence (`PathExists()` method)
- Path matching (`PathMatches()` method)
- terraform-plugin-go value extraction at a given terraform-plugin-go path (`TerraformValueAtTerraformPath()` method)

The unit testing is meant to be more exhaustive than what was previously performed in the `tfsdk` package.
  • Loading branch information
bflad authored Aug 12, 2022
1 parent eea9faa commit 3a30a6d
Show file tree
Hide file tree
Showing 16 changed files with 8,223 additions and 2,910 deletions.
20 changes: 20 additions & 0 deletions internal/fwschemadata/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package fwschemadata

import (
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// Data is the shared storage implementation for schema-based values, such as
// configuration, plan, and state.
type Data struct {
// Schema contains the data structure and types for the value.
Schema fwschema.Schema

// TerraformValue contains the terraform-plugin-go value implementation.
//
// TODO: In the future this may be migrated to attr.Value, or more
// succinctly, types.Object.
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/172
TerraformValue tftypes.Value
}
13 changes: 13 additions & 0 deletions internal/fwschemadata/data_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fwschemadata

import (
"context"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/reflect"
)

// Get populates the struct passed as `target` with the entire state.
func (d Data) Get(ctx context.Context, target any) diag.Diagnostics {
return reflect.Into(ctx, d.Schema.Type(), d.TerraformValue, target, reflect.Options{})
}
Loading

0 comments on commit 3a30a6d

Please sign in to comment.