-
Notifications
You must be signed in to change notification settings - Fork 93
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
Add an attr.Convert helper. #120
Conversation
5168172
to
3aa7c59
Compare
Depends on #119. |
b936f57
to
2faf1c2
Compare
Still depends on #119, but has tests and is otherwise code complete. |
15a059c
to
322ff86
Compare
This function converts an `attr.Value` to use a new `attr.Type`, so long as they share a compatible `tftypes.Type` representation. This allows code that doesn't know the type of an `attr.Value` to still use it, provided they know the kind of data it holds. For example, a validation helper that works on strings--MaxStringLength, say--could look something like this: ``` // assume req.Config is an attr.Value strAttr, err := attr.Convert(ctx, req.Config, types.StringType) if err != nil { panic(err) } var str types.String diags := tfsdk.ValueAs(ctx, strAttr, &str) if diags.HasError() { panic("oops") } // do something with `str`, which is now strongly typed ``` Then no matter what `attr.Type` provider devs use in their schema, this validator would still work, then. Without the conversion step, it wouldn't work, as `tfsdk.ValueAs` implementation needs to know the `attr.Type` of the `attr.Value`, and reusable validators _can't_ know that. So the point of this helper is to get an `attr.Value` into a known `attr.Type` format.
Import cycle in tests if we don't.
Being able to return good error messages depends on human-friendly representations of types existing.
322ff86
to
f623439
Compare
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |
This function converts an
attr.Value
to use a newattr.Type
, so longas they share a compatible
tftypes.Type
representation. This allowscode that doesn't know the type of an
attr.Value
to still use it,provided they know the kind of data it holds.
For example, a validation helper that works on strings--MaxStringLength,
say--could look something like this:
Then no matter what
attr.Type
provider devs use in their schema, thisvalidator would still work, then. Without the conversion step, it
wouldn't work, as
tfsdk.ValueAs
implementation needs to know theattr.Type
of theattr.Value
, and reusable validators can't knowthat. So the point of this helper is to get an
attr.Value
into a knownattr.Type
format.This still needs tests.