-
Notifications
You must be signed in to change notification settings - Fork 94
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
path: Initial Path Expression Support #396
Conversation
Reference: #81 Reference: hashicorp/terraform-plugin-framework-validators#14 Reference: hashicorp/terraform-plugin-framework-validators#15 Reference: hashicorp/terraform-plugin-framework-validators#16 Reference: hashicorp/terraform-plugin-framework-validators#17 Reference: hashicorp/terraform-plugin-framework-validators#20 This introduces the concept of an attribute path expression, an abstraction on top of an attribute path, which enables provider developers to declare logic which might match zero, one, or more paths. Paths are directly convertable into path expressions as exact expression steps. The builder-like syntax for exact expression steps matches the syntax for path steps, such as `AtName()` in both cases always represents an exact transversal into the attribute name of an object. Additional expression steps enable matching any list, map, or set element, such as `AtAnyListIndex()`. It also supports relative attribute path expressions, by supporting a parent expression step `AtParent()` or starting an expression with `MatchParent()` which can be combined with a prior path expression. The framework will automatically expose path expressions to attribute plan modifiers and validators, so they can more intuitively support relative paths as inputs to their logic. For example, the `terraform-plugin-framework-validators` Go module will implement support for `terraform-plugin-sdk` multiple attribute schema behaviors such as `ConflictsWith`. It is expected that the downstream implementation can allow provider developers to declare the validator with expressions such as: ```go tfsdk.Attribute{ // ... other fields ... Validators: []AttributeValidators{ schemavalidator.ConflictsWith( // Example absolute path from root path.MatchRoot("root_attribute"), // Example relative path from current attribute // e.g. another attribute at the same list index of ListNestedAttributes path.MatchParent().AtName("another_same_level_attribute"), ), }, } ``` Then the logic within the validator can take the `ValidateAttributeRequest.AttributePathExpression` and use the `(path.Expression).Append()` method to combine the current attribute expression with any incoming expressions. While this introduction will expose the expression types and make them available to attribute plan modifiers and validators, there is not yet a simple methodology for getting valid paths within data stored in `tfsdk.Config`, `tfsdk.Plan`, and `tfsdk.State` that match the expression. This will be added after this initial expression API is reviewed and approved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code wise is 👨🍳 💋
But I do have a "how do I use this?" question.
|
||
// Expression represents an attribute path with expression steps, which can | ||
// represent zero, one, or more actual Paths. | ||
type Expression struct { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the description, I was expecting an .Append()
method here.
I see there is one on the ExpressionSteps
, but not on the expression itself.
The reason for me asking, is that I'm trying to imagine how we would take the input for a schemavalidator
, append it to the path.Expression
of the attribute, and get to the referred-to attributes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh goodness, you are absolutely right that I omitted that method. It's important to be able to merge expressions, especially in plan modifiers and validators. I'll get that pushed up shortly.
@detro I wound up adding the |
…mework-only features Reference: hashicorp/terraform-plugin-framework#396 Reference: hashicorp/terraform-plugin-framework#409 These will be included in the upcoming terraform-plugin-framework v0.10.0 release and are features not available in terraform-plugin-sdk/v2.
…mework-only features (#65) Reference: hashicorp/terraform-plugin-framework#396 Reference: hashicorp/terraform-plugin-framework#409 These were included in the terraform-plugin-framework v0.10.0 release and are features not available in terraform-plugin-sdk/v2.
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. |
Reference: #81
Reference: hashicorp/terraform-plugin-framework-validators#14
Reference: hashicorp/terraform-plugin-framework-validators#15
Reference: hashicorp/terraform-plugin-framework-validators#16
Reference: hashicorp/terraform-plugin-framework-validators#17
Reference: hashicorp/terraform-plugin-framework-validators#20
This introduces the concept of root and relative attribute path expressions, abstractions on top of an attribute path, which enables provider developers to declare logic which might match zero, one, or more paths.
Paths are directly convertible into path expressions as exact expression steps. The builder-like syntax for exact expression steps matches the syntax for regular path steps, such as
AtName()
in both cases always represents an exact transversal into the attribute name of an object. Additional expression steps enable matching any list, map, or set element, such asAtAnyListIndex()
. It also supports relative attribute path expressions, by supporting a parent expression stepAtParent()
and starting an expression withMatchRelative()
so it can be combined with a prior path expression.The framework will automatically expose path expressions to attribute plan modifiers and validators, so they can more intuitively support relative paths as inputs to their logic. For example, the
terraform-plugin-framework-validators
Go module will implement support forterraform-plugin-sdk
multiple attribute schema behaviors such asConflictsWith
. It is expected that the downstream implementation can allow provider developers to declare the validator with expressions such as:Then the logic within the validator can take the
ValidateAttributeRequest.AttributePathExpression
and use the(path.Expression).Merge()
method to combine the current attribute expression with any incoming expressions.To find matching attribute paths based on a path expression within
tfsdk.Config
,tfsdk.Plan
, andtfsdk.State
, aPathMatches(path.Expression)
method has been added to each type. The resulting paths can then be used to fetch data via existing functionality, such as theGetAttribute()
method of each type.