-
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
Extend attr.Value
interface to support IsFullyNullableKnown()
#980
base: main
Are you sure you want to change the base?
Conversation
This new method is similar to the `Value.IsFullyKnown()` that is available in the `github.com/hashicorp/terraform-plugin-go/tftypes`. The difference here is that in `tftypes`, each value can only has two states: a concrete value (including `nil`) or "unknown". While in the fw, each value can has three states: null, unknown and known. This is why the method name is chose so (as I can't figure out another better name, as `IsFullyNotKnown` or `IsPartiallyUnknown` are ambiguous than the current one, IMO). The reason for introducing this method is to allow provider developers to check the state of an aggregate value during the `ModifyPlan`, where the code might stop processing that property if its value contains any unknwon value. Currently, the developer has two solutions: - Convert the `ToTerraformValue()` to convert the `attr.Value` to `tftypes.Value`, then call its `IsFullyKnown()`. This works fine (and it is also used in the FW itself somewhere), while it is a bit over kill to do the conversion where the intent is only to check the whole (un)known-ness. - Self implement the `IsFullyKnown()` for the `attr.Value`, similar to: ```go func IsFullyKnown(val attr.Value) bool { if val == nil { return true } if val.IsUnknown() { return false } switch v := val.(type) { case types.Dynamic: return IsFullyKnown(v.UnderlyingValue()) case types.List: for _, e := range v.Elements() { if !IsFullyKnown(e) { return false } } return true case types.Set: for _, e := range v.Elements() { if !IsFullyKnown(e) { return false } } return true case types.Tuple: for _, e := range v.Elements() { if !IsFullyKnown(e) { return false } } return true case types.Map: for _, e := range v.Elements() { if !IsFullyKnown(e) { return false } } return true case types.Object: for _, e := range v.Attributes() { if !IsFullyKnown(e) { return false } } return true default: return true } } ``` This PR tries to put this common logic to the FW so that more developers can save the run/develop time effort for the same purpose. I chose to extend the `attr.Value` interface, instead of introducing a helper method in the `attr` package, as a random choice. If the latter looks better, then I can rework this PR.
Related feature request issue: #597 For any reviewers, please note that the current |
@bflad The other possible option I can think of is the 2nd one I mentioned above, which won't handle the custom types. I don't know how can we avoid breaking change while still taking custom types into consideration at this moment.. |
This new method is similar to the
Value.IsFullyKnown()
that is available in thegithub.com/hashicorp/terraform-plugin-go/tftypes
.The difference here is that in
tftypes
, each value can only has two states: a concrete value (includingnil
) or "unknown". While in the fw, each value can has three states: null, unknown and known. This is why the method name is chose so (as I can't figure out another better name, asIsFullyNotKnown
orIsPartiallyUnknown
are ambiguous than the current one, IMO).The reason for introducing this method is to allow provider developers to check the state of an aggregate value during the
ModifyPlan
, where the code might stop processing that property if its value contains any unknwon value. Currently, the developer has two solutions:Convert the
ToTerraformValue()
to convert theattr.Value
totftypes.Value
, then call itsIsFullyKnown()
. This works fine (and it is also used in the FW itself somewhere), while it is a bit over kill to do the conversion where the intent is only to check the whole (un)known-ness.Self implement the
IsFullyKnown()
for theattr.Value
, similar to:This PR tries to put this common logic to the FW so that more developers can save the run/develop time effort for the same purpose. I chose to extend the
attr.Value
interface, instead of introducing a helper method in theattr
package, as a random choice. If the latter looks better, then I can rework this PR.