-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/toproto*: Convert null list/set block values to empty (#621)
Reference: #604 Reference: #620 This logic implements the inverse of what was added to `internal/fromproto*` where empty block values are converted to null. While Terraform implements some logic to automatically convert these for state responses from `ReadResource` and others, the same null values can cause errors with Terraform's plan validity logic. This fixes the potential new error caused by only introducing the `internal/fromproto*` logic, which was not released and why it does not contain a changelog entry in that regard, However, this change is considered an overall enhancement prior and including those changes since it means the provider developers will no longer be burdened with understanding Terraform's implementation details with null versus empty collection blocks when setting values for responses.
- Loading branch information
Showing
32 changed files
with
3,579 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:enhancement | ||
tfsdk: Automatically prevented Terraform `nested blocks must be empty to indicate no blocks` errors for responses containing `Plan` and `State` types | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
internal/fwschemadata/data_reify_null_collection_blocks.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package fwschemadata | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/diag" | ||
"github.com/hashicorp/terraform-plugin-framework/internal/fromtftypes" | ||
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema" | ||
"github.com/hashicorp/terraform-plugin-go/tftypes" | ||
) | ||
|
||
// ReifyNullCollectionBlocks converts list and set block null values to empty | ||
// values. This is the reverse conversion of NullifyCollectionBlocks. | ||
func (d *Data) ReifyNullCollectionBlocks(ctx context.Context) diag.Diagnostics { | ||
var diags diag.Diagnostics | ||
|
||
blockPathExpressions := fwschema.SchemaBlockPathExpressions(ctx, d.Schema) | ||
|
||
// Errors are handled as richer diag.Diagnostics instead. | ||
d.TerraformValue, _ = tftypes.Transform(d.TerraformValue, func(tfTypePath *tftypes.AttributePath, tfTypeValue tftypes.Value) (tftypes.Value, error) { | ||
// Only transform null values. | ||
if !tfTypeValue.IsNull() { | ||
return tfTypeValue, nil | ||
} | ||
|
||
fwPath, fwPathDiags := fromtftypes.AttributePath(ctx, tfTypePath, d.Schema) | ||
|
||
diags.Append(fwPathDiags...) | ||
|
||
// Do not transform if path cannot be converted. | ||
// Checking against fwPathDiags will capture all errors. | ||
if fwPathDiags.HasError() { | ||
return tfTypeValue, nil | ||
} | ||
|
||
// Do not transform if path is not a block. | ||
if !blockPathExpressions.Matches(fwPath) { | ||
return tfTypeValue, nil | ||
} | ||
|
||
// Transform to empty value. | ||
return tftypes.NewValue(tfTypeValue.Type(), []tftypes.Value{}), nil | ||
}) | ||
|
||
return diags | ||
} |
Oops, something went wrong.