Skip to content

Commit

Permalink
types: Deprecate Null, Unknown, and Value fields
Browse files Browse the repository at this point in the history
Reference: #447

When the framework type system was originally being developed, the value types were introduced with exported fields which also served as the internal details of whether a value was null, unknown, or a known value of a friendlier Go type. It was known that there was the potential for issues, but the simplified developer experience seemed to outweigh the potential for developer issues. Fast forward a few months, this decision appears to have two consequences that the framework maintainers hear about across various forums.

One issue is that the value types directly expose their internal implementation details and support the three states of a Terraform type value: being null, unknown, or a known value. Only one state should ever be set, but provider developers can make a value that is any combination of those states. This makes the framework behavior potentially indeterminate from the provider developer perspective whether, for example, a null AND unknown value becomes null OR unknown as it works its way through the framework.

```go
type ThingResourceModel struct{
  Computed types.String `tfsdk:"computed"`
}

func (r ThingResource) Create(ctx context.Context, req resource.CreateResource, resp *resource.CreateResponse) {
  var data ThingResourceModel

  resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

  tflog.Trace(ctx, "Data Values", map[string]any{
    // Unknown value: types.String{Null: false, Unknown: true, Value: ""}
    "computed": plan.Computed,
  })

  // Maybe some external API responses here, but showing hardcoded updates for
  // brevity. This will make the value invalid by enabling Null without
  // disabling Unknown.
  data.Computed.Null = true

  tflog.Trace(ctx, "Data Values", map[string]any{
    // Invalid value: types.String{Null: true, Unknown: true, Value: ""}
    "computed": data.Computed,
  })

  // The invalid value will be either null or unknown, depending on the
  // type implementation. If unknown, Terraform will error, since unknown
  // values are never allowed in state.
  resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
```

Another issue is that the default (zero-value) state for an "unset" value type turns into a known value, which is confusing since these values explicitly support being null. This causes Terraform errors which would surface to practitioners (especially when untested) that provider developers then have to troubleshoot the error message containing Terraform's type system details, potentially discover the reason why it is happening by looking at the framework type source code, then figure out a workable solution. It's not intuitive.

```go
type ThingResourceModel struct{
  // let's assume this is left unconfigured (null in config and plan)
  Optional types.String `tfsdk:"optional"`
}

func (r ThingResource) Create(ctx context.Context, req resource.CreateResource, resp *resource.CreateResponse) {
  // Providers can opt to use a single variable that is updated based on an
  // external response, however that logic can be more difficult sometimes,
  // so it can be easier to split them. Showing the split way to exemplify
  // the "unset" problem.
  var plan, state ThingResourceModel

  resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)

  tflog.Trace(ctx, "Plan Values", map[string]any{
    // Null value: types.String{Null: true, Unknown: false, Value: ""}
    "optional": plan.Optional,
  })

  // Maybe some external API responses here, but intentionally not
  // doing any state.Optional setting, which might happen if the
  // external response for that data was null for example.

  tflog.Trace(ctx, "State Values", map[string]any{
    // Zero-value: types.String{Null: false, Unknown: false, Value: ""}
    "optional": state.Optional,
  })

  // The state zero-value will later cause Terraform to error, such as:
  // Error: Provider produced inconsistent result after apply
  // ... expected cty.NullVal(cty.String), got cty.StringVal("")
  // Since the plan value said it would be null.
  resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
```

This deprecation of the fields in preference of functions and methods aims to unexport the internal details and treat the value types as immutable once they are created. When provider developers switch over to the new model, any errant changes to the deprecated exported fields will have no effect. A future version will remove the exported fields entirely and switch the zero-value implementation of these values to being null, instead of a known zero-value of the underlying type.
  • Loading branch information
bflad committed Oct 20, 2022
1 parent 2be6665 commit b868035
Show file tree
Hide file tree
Showing 52 changed files with 3,314 additions and 534 deletions.
59 changes: 59 additions & 0 deletions .changelog/pending.txt
@@ -0,0 +1,59 @@
```release-note:note
types: The `Bool` type `Null`, `Unknown`, and `Value` fields have been deprecated in preference of the `BoolNull()`, `BoolUnknown()`, and `BoolValue()` creation functions and `IsNull()`, `IsUnknown()`, and `ValueBool()` methods. The fields will be removed in a future release.
```

```release-note:note
types: The `Float64` type `Null`, `Unknown`, and `Value` fields have been deprecated in preference of the `Float64Null()`, `Float64Unknown()`, and `Float64Value()` creation functions and `IsNull()`, `IsUnknown()`, and `ValueFloat64()` methods. The fields will be removed in a future release.
```

```release-note:note
types: The `Int64` type `Null`, `Unknown`, and `Value` fields have been deprecated in preference of the `Int64Null()`, `Int64Unknown()`, and `Int64Value()` creation functions and `IsNull()`, `IsUnknown()`, and `ValueInt64()` methods. The fields will be removed in a future release.
```

```release-note:note
types: The `Number` type `Null`, `Unknown`, and `Value` fields have been deprecated in preference of the `NumberNull()`, `NumberUnknown()`, and `NumberValue()` creation functions and `IsNull()`, `IsUnknown()`, and `ValueBigFloat()` methods. The fields will be removed in a future release.
```

```release-note:note
types: The `String` type `Null`, `Unknown`, and `Value` fields have been deprecated in preference of the `StringNull()`, `StringUnknown()`, and `StringValue()` creation functions and `IsNull()`, `IsUnknown()`, and `ValueString()` methods. The fields will be removed in a future release.
```

```release-note:enhancement
types: Added `BoolNull()`, `BoolUnknown()`, `BoolValue()` functions, which create immutable `Bool` values
```

```release-note:enhancement
types: Added `Float64Null()`, `Float64Unknown()`, `Float64Value()` functions, which create immutable `Float64` values
```

```release-note:enhancement
types: Added `Int64Null()`, `Int64Unknown()`, `Int64Value()` functions, which create immutable `Int64` values
```

```release-note:enhancement
types: Added `NumberNull()`, `NumberUnknown()`, `NumberValue()` functions, which create immutable `Number` values
```

```release-note:enhancement
types: Added `StringNull()`, `StringUnknown()`, `StringValue()` functions, which create immutable `String` values
```

```release-note:enhancement
types: Added `Bool` type `ValueBool()` method, which returns the `bool` of the known value or `false` if null or unknown
```

```release-note:enhancement
types: Added `Float64` type `ValueFloat64()` method, which returns the `float64` of the known value or `0.0` if null or unknown
```

```release-note:enhancement
types: Added `Int64` type `ValueInt64()` method, which returns the `int64` of the known value or `0` if null or unknown
```

```release-note:enhancement
types: Added `Number` type `ValueBigFloat()` method, which returns the `*big.Float` of the known value or `nil` if null or unknown
```

```release-note:enhancement
types: Added `String` type `ValueString()` method, which returns the `string` of the known value or `""` if null or unknown
```
4 changes: 2 additions & 2 deletions internal/fwserver/attribute_plan_modification_test.go
Expand Up @@ -65,7 +65,7 @@ func TestAttributeModifyPlan(t *testing.T) {
AttributeState: types.String{Value: "TESTATTRONE"},
},
expectedResp: ModifyAttributePlanResponse{
AttributePlan: types.String{Value: "MODIFIED_TWO"},
AttributePlan: types.StringValue("MODIFIED_TWO"),
Private: testEmptyProviderData,
},
},
Expand Down Expand Up @@ -704,7 +704,7 @@ func TestAttributeModifyPlan(t *testing.T) {
},
},
expectedResp: ModifyAttributePlanResponse{
AttributePlan: types.String{Value: "TESTATTRTWO"},
AttributePlan: types.StringValue("TESTATTRTWO"),
RequiresReplace: path.Paths{
path.Root("test"),
},
Expand Down
4 changes: 2 additions & 2 deletions internal/fwserver/block_plan_modification_test.go
Expand Up @@ -1741,7 +1741,7 @@ func TestBlockModifyPlan(t *testing.T) {
"nested_attr": types.StringType,
},
Attrs: map[string]attr.Value{
"nested_attr": types.String{Value: "MODIFIED_TWO"},
"nested_attr": types.StringValue("MODIFIED_TWO"),
},
},
},
Expand Down Expand Up @@ -1836,7 +1836,7 @@ func TestBlockModifyPlan(t *testing.T) {
"nested_attr": types.StringType,
},
Attrs: map[string]attr.Value{
"nested_attr": types.String{Value: "TESTATTRTWO"},
"nested_attr": types.StringValue("TESTATTRTWO"),
},
},
},
Expand Down
36 changes: 18 additions & 18 deletions internal/fwserver/server_applyresourcechange_test.go
Expand Up @@ -138,8 +138,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.Config value: %s", data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.Config value: %s", data.TestRequired.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down Expand Up @@ -184,8 +184,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if data.TestRequired.Value != "test-plannedstate-value" {
resp.Diagnostics.AddError("unexpected req.Plan value: %s", data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-plannedstate-value" {
resp.Diagnostics.AddError("unexpected req.Plan value: %s", data.TestRequired.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down Expand Up @@ -229,8 +229,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...)

if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value)
if metadata.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down Expand Up @@ -451,8 +451,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if data.TestRequired.Value != "test-priorstate-value" {
resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-priorstate-value" {
resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.ValueString())
}
},
UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -487,8 +487,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestProviderMetaAttribute.ValueString())
}
},
UpdateMethod: func(_ context.Context, _ resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -699,8 +699,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-new-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-new-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -756,8 +756,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if data.TestComputed.Value != "test-plannedstate-value" {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value)
if data.TestComputed.ValueString() != "test-plannedstate-value" {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.ValueString())
}
},
},
Expand Down Expand Up @@ -813,8 +813,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if data.TestRequired.Value != "test-old-value" {
resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-old-value" {
resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -871,8 +871,8 @@ func TestServerApplyResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.ValueString())
}
},
},
Expand Down
4 changes: 2 additions & 2 deletions internal/fwserver/server_configureprovider_test.go
Expand Up @@ -68,8 +68,8 @@ func TestServerConfigureProvider(t *testing.T) {
return
}

if got.Value != "test-value" {
resp.Diagnostics.AddError("Incorrect req.Config", "expected test-value, got "+got.Value)
if got.ValueString() != "test-value" {
resp.Diagnostics.AddError("Incorrect req.Config", "expected test-value, got "+got.ValueString())
}
},
},
Expand Down
12 changes: 6 additions & 6 deletions internal/fwserver/server_createresource_test.go
Expand Up @@ -114,8 +114,8 @@ func TestServerCreateResource(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down Expand Up @@ -153,8 +153,8 @@ func TestServerCreateResource(t *testing.T) {

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if data.TestRequired.Value != "test-plannedstate-value" {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-plannedstate-value" {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestRequired.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down Expand Up @@ -192,8 +192,8 @@ func TestServerCreateResource(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &metadata)...)

if metadata.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.Value)
if metadata.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+metadata.TestProviderMetaAttribute.ValueString())
}

// Prevent missing resource state error diagnostic
Expand Down
8 changes: 4 additions & 4 deletions internal/fwserver/server_deleteresource_test.go
Expand Up @@ -109,8 +109,8 @@ func TestServerDeleteResource(t *testing.T) {

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if data.TestRequired.Value != "test-priorstate-value" {
resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-priorstate-value" {
resp.Diagnostics.AddError("unexpected req.State value: %s", data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -138,8 +138,8 @@ func TestServerDeleteResource(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", data.TestProviderMetaAttribute.ValueString())
}
},
},
Expand Down
36 changes: 18 additions & 18 deletions internal/fwserver/server_planresourcechange_test.go
Expand Up @@ -810,8 +810,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -902,8 +902,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if !data.TestComputed.Unknown {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value)
if !data.TestComputed.IsUnknown() {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.ValueString())
}
},
},
Expand Down Expand Up @@ -947,8 +947,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.ValueString())
}
},
},
Expand Down Expand Up @@ -1211,8 +1211,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -1291,8 +1291,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.State.Get(ctx, &data)...)

if data.TestRequired.Value != "test-state-value" {
resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-state-value" {
resp.Diagnostics.AddError("Unexpected req.State Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -1330,8 +1330,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.ValueString())
}
},
},
Expand Down Expand Up @@ -1918,8 +1918,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

if data.TestRequired.Value != "test-new-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.Value)
if data.TestRequired.ValueString() != "test-new-value" {
resp.Diagnostics.AddError("Unexpected req.Config Value", "Got: "+data.TestRequired.ValueString())
}
},
},
Expand Down Expand Up @@ -1968,8 +1968,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

if !data.TestComputed.Unknown {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.Value)
if !data.TestComputed.IsUnknown() {
resp.Diagnostics.AddError("Unexpected req.Plan Value", "Got: "+data.TestComputed.ValueString())
}
},
},
Expand Down Expand Up @@ -2019,8 +2019,8 @@ func TestServerPlanResourceChange(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &data)...)

if data.TestProviderMetaAttribute.Value != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.Value)
if data.TestProviderMetaAttribute.ValueString() != "test-provider-meta-value" {
resp.Diagnostics.AddError("Unexpected req.ProviderMeta Value", "Got: "+data.TestProviderMetaAttribute.ValueString())
}
},
},
Expand Down
8 changes: 4 additions & 4 deletions internal/fwserver/server_readdatasource_test.go
Expand Up @@ -90,8 +90,8 @@ func TestServerReadDataSource(t *testing.T) {

resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)

if config.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value)
if config.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.ValueString())
}
},
},
Expand All @@ -116,8 +116,8 @@ func TestServerReadDataSource(t *testing.T) {

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...)

if config.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value)
if config.TestRequired.ValueString() != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.ValueString())
}
},
},
Expand Down

0 comments on commit b868035

Please sign in to comment.