Skip to content

Errantly Reflecting a List Into a Struct Provides Incorrect Error Details #729

Open
@bflad

Description

@bflad

Module version

v1.2.0

Relevant provider source code

The original issue occurred in a provider schema, although this is issue should theoretically be reproducible in data sources and resources as well.

Schema:

schema.Schema{
  Blocks: map[string]schema.Block{
    "list_block": schema.ListNestedBlock{
      NestedObject: schema.NestedBlockObject{
        Attributes: map[string]schema.Attribute{/* ... */},
      },
    },
  },
}

Models:

type ProviderModel struct {
  ListBlock types.List `tfsdk:"list_block"`
}

type ListBlockModel struct {
  // ...
}

Logic:

func (p ExampleCloudProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
  var data ProviderModel

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

  if resp.Diagnostics.HasError() {
    return
  }

  // this should be []ListBlockModel below
  var listBlocks ListBlockModel

  // omitting null/unknown checking for example brevity
  // error is raised here if list has elements
  resp.Diagnostics.Append(data.ListBlock.ElementsAs(ctx, &listBlocks, false)...)

  if resp.Diagnostics.HasError() {
    return
  }

  // ...
}

Terraform Configuration Files

list_block {
  # ...
}

Expected Behavior

The error message should indicate that it is necessary to reflect a list into a slice.

cannot reflect tftypes.List[tftypes.Object[...]] into a struct, must be a slice

Actual Behavior

The error message indicates that it is necessary to reflect a list into an object.

Error: Value Conversion Error

  {CONFIG SNIPPET}

An unexpected error was encountered trying to convert tftypes.Value into
provider.ProviderModel. This is always an error in the provider. Please
report the following to the provider developer:

cannot reflect tftypes.List[tftypes.Object[...]] into a struct, must be an object

It is likely this occurs with maps and sets as well, although I haven't personally verified.

Steps to Reproduce

  1. go test -count=1 -v ./internal/provider with a covering acceptance test

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingreflectionIssues and PRs about the reflection subsystem used to convert between attr.Values and Go values.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions