hashicorp / terraform-plugin-go

A low-level Go binding for the Terraform protocol for integrations to be built on top of.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tftypes: Consider Additional Errors for (Value).ApplyTerraform5AttributePathStep()

bflad opened this issue · comments

terraform-plugin-go version

v0.4.0

Use cases

Downstream consumers using tftypes.WalkAttributePath() against a tftypes.Value tend to alway receive the same tftypes.ErrInvalidStep error, regardless of the cause, which may include:

  • Stepping into a null value
  • Stepping into an unknown value
  • Stepping into a type incorrectly (e.g. attempting WithAttributeName() on a List value)

The ability to distinguish between these situations means that these consumers can further tailor their intended behaviors, without the need to then determine the underlying cause.

Attempted solutions

After receiving ErrInvalidStep, walk backwards in the path (via recursive (AttributePath).WithoutLastStep() until no ErrInvalidStep is received) to detect a parent value with IsUnknown().

Proposal

Create two new error values (one for stepping into a null value, one for stepping into an unknown value) and convert this logic to use them:

	if !val.IsKnown() || val.IsNull() {
		return nil, ErrInvalidStep
	}

e.g.

	if val.IsNull() {
		return nil, ErrInvalidStepNullValue
	}

	if !val.IsKnown() {
		return nil, ErrInvalidStepUnknownValue
	}

In the error implementation, they should still be appropriately detectable via errors.Is(tftypes.ErrInvalidStep) or this enhancement becomes a breaking change.

References

Breaking change 👍 ErrInvalidStep should be migrated to a new name so it is very clear that usage should be updated.