Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attr: Consider Adding ValueType Method to Type Interface #496

Closed
bflad opened this issue Sep 23, 2022 · 1 comment · Fixed by #497
Closed

attr: Consider Adding ValueType Method to Type Interface #496

bflad opened this issue Sep 23, 2022 · 1 comment · Fixed by #497
Labels
enhancement New feature or request reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. types Issues and pull requests about our types abstraction and implementations.
Milestone

Comments

@bflad
Copy link
Member

bflad commented Sep 23, 2022

Module version

v0.13.0

Use-cases

When the internal reflection package is attempting to build values or convert them into given types, it needs to raise errors when the given target type/value is incompatible. Today, these errors can tell provider developers what went wrong, but not necessarily offer help for how to resolve it.

An example diagnostic that we'd like to raise:

Received unknown value, however the target type cannot handle unknown values. Use the corresponding `types` package type or a custom type that handles unknown values.

Path: example
Target Type: *bool
Suggested Type: types.Bool <-- this (but also working for any attr.Value)

Attempted Solutions

It's technically possible to try to get the associated attr.Value of an attr.Type by doing something like:

attrValue, err := attrType.ValueFromTerraform(ctx, tftypes.NewValue(attrType.TerraformType(ctx), nil))

However, supporting null values in an attr.Type is not a requirement, as edge case-y as that may be, since in all implementations may be Required in the schema.

Proposal

Consider introducing a new method in the attr.Type interface:

type Type interface {
  // ... existing methods ...

  // ValueType should return the attr.Value type returned by ValueFromTerraform.
  // The returned attr.Value can be any null, unknown, or known value for the type,
  // as this is only intended for type detection.
  ValueType(context.Context) attr.Value
}

Or, make it optional, where the reflection package can pick it up if it is detected:

type TypeWithValueType interface {
  // ValueType should return the attr.Value type returned by ValueFromTerraform.
  // The returned attr.Value can be any null, unknown, or known value for the type,
  // as this is only intended for type detection.
  ValueType(context.Context) attr.Value
}

References

@bflad bflad added enhancement New feature or request types Issues and pull requests about our types abstraction and implementations. reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. labels Sep 23, 2022
@bflad bflad added this to the v0.14.0 milestone Sep 23, 2022
bflad added a commit that referenced this issue Sep 23, 2022
Reference: #496

This will allow the internal reflection package to return better error diagnostics.
bflad added a commit that referenced this issue Sep 23, 2022
Reference: #496

This will allow the internal reflection package to return better error diagnostics.
bflad added a commit that referenced this issue Sep 23, 2022
Reference: #496

This will allow the internal reflection package to return better error diagnostics.
@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request reflection Issues and PRs about the reflection subsystem used to convert between attr.Values and Go values. types Issues and pull requests about our types abstraction and implementations.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant