|
| 1 | +--- |
| 2 | +page_title: 'Plugin Development - Framework: Path Expressions' |
| 3 | +description: >- |
| 4 | + How to implement path expressions in the provider development framework. |
| 5 | + Path expressions are logic built on top of paths, which may represent one or |
| 6 | + more actual paths within schema data. |
| 7 | +--- |
| 8 | + |
| 9 | +# Path Expressions |
| 10 | + |
| 11 | +Path expressions are logic built on top of [paths](/plugin/framework/paths), which may represent one or more actual paths within a schema or schema-based data. Expressions enable providers to work outside the restrictions of absolute paths and steps. |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +Example uses include: |
| 16 | + |
| 17 | +- [Path based attribute validators](/plugin/framework/validation#path-based-attribute-validators), such as those in the [`terraform-plugin-framework-validators` module `schemavalidator` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/schemavalidator). |
| 18 | + |
| 19 | +Use cases which require exact locations, such as [diagnostics](/plugin/framework/diagnostics), implement [paths](/plugin/framework/paths). |
| 20 | + |
| 21 | +## Concepts |
| 22 | + |
| 23 | +Path expressions are an abstraction above [paths](/plugin/framework/paths). This page assumes knowledge of path concepts and implementations. |
| 24 | + |
| 25 | +At its core, expressions implement the following on top of paths: |
| 26 | + |
| 27 | +- Information that designates whether path information is intended to be absolute, similar to paths, or relative, where it is assumed it will be merged with other absolute path information. |
| 28 | +- Parent steps, which enables backwards traversal towards the root of a schema in relative paths, after being merged with other absolute path information. |
| 29 | +- Path matching, which enables path information to logically return one or more actual paths. |
| 30 | + |
| 31 | +Similar to paths, expressions are built using steps. There are expression steps which directly correspond to exact path steps, such as `AtListIndex()`, `AtMapKey()`, `AtName()`, `AtSetValue()`. Their implementation is the same. However, there are additional expression steps, such as `AtAnyListIndex()`, which cannot be represented in paths due to the potential for ambiguity. |
| 32 | + |
| 33 | +Path matching is the notion that each expression step implements a method that logically determines if a given exact path step should match. For example, the `AtAnyListIndex()` expression step will accept any exact path step for a list index. Path matching with an expression is a collection of matching each expression step against each exact path step, after resolving any potential parent steps. |
| 34 | + |
| 35 | +Every path expression must align with the schema definition or an error diagnostic will be raised when working with path matching within the framework. Provider-defined functionality that is schema-based, such as attribute validation and attribute plan modification, are provided an accurate current path expression since that functionality would not be able to determine its own path expression. |
| 36 | + |
| 37 | +## Building Path Expressions |
| 38 | + |
| 39 | +The framework implementation for path expressions is in the [`path` package](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path), with the [`path.Expression` type](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#Expression) being the main provider developer interaction point. |
| 40 | + |
| 41 | +### Building Absolute Path Expressions |
| 42 | + |
| 43 | +Call the [`path.MatchRoot()` function](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#MatchRoot) with an attribute name or block name at the root of the schema to begin an absolute path expression. |
| 44 | + |
| 45 | +Given this example schema with a root attribute named `example_root_attribute`: |
| 46 | + |
| 47 | +```go |
| 48 | +tfsdk.Schema{ |
| 49 | + Attributes: map[string]tfsdk.Attribute{ |
| 50 | + "example_root_attribute": { |
| 51 | + Required: true, |
| 52 | + Type: types.StringType, |
| 53 | + }, |
| 54 | + }, |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +The call to `path.MatchRoot()` which matches the location of `example_root_attribute` string value is: |
| 59 | + |
| 60 | +```go |
| 61 | +path.MatchRoot("example_root_attribute") |
| 62 | +``` |
| 63 | + |
| 64 | +For blocks, the beginning of a path expression is similarly defined. Attribute and block names cannot overlap, so the framework automatically handles whether a path expression is referring to an attribute or block to start. |
| 65 | + |
| 66 | +Given this example schema with a root block named `example_root_block`: |
| 67 | + |
| 68 | +```go |
| 69 | +tfsdk.Schema{ |
| 70 | + Blocks: map[string]tfsdk.Block{ |
| 71 | + "example_root_block": { |
| 72 | + Attributes: map[string]tfsdk.Attribute{/* ... */}, |
| 73 | + NestingMode: tfsdk.BlockNestingModeList, |
| 74 | + }, |
| 75 | + }, |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +The call to `path.MatchRoot()` which matches the location of `example_root_block` list value is: |
| 80 | + |
| 81 | +```go |
| 82 | +path.MatchRoot("example_root_block") |
| 83 | +``` |
| 84 | + |
| 85 | +Once a `path.Expression` is started, it supports a builder pattern, which allows for chaining method calls to construct a full path. |
| 86 | + |
| 87 | +This example shows a hypothetical path expression that points to any element of a list attribute to highlight the builder pattern: |
| 88 | + |
| 89 | +```go |
| 90 | +path.MatchRoot("example_list_attribute").AtAnyListIndex() |
| 91 | +``` |
| 92 | + |
| 93 | +This pattern can be extended to as many calls as necessary. The [Building Expression Steps section](#building-expression-steps) covers the different framework schema types and any special path step methods. |
| 94 | + |
| 95 | +### Building Relative Path Expressions |
| 96 | + |
| 97 | +Relative path expressions are, by nature, contextual to the actual path where they are defined in a schema. Call the [`path.MatchRelative()` function](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#MatchRelative) to begin a relative path expression. |
| 98 | + |
| 99 | +This example shows a relative path expression which references a child attribute: |
| 100 | + |
| 101 | +```go |
| 102 | +tfsdk.Schema{ |
| 103 | + Attributes: map[string]tfsdk.Attribute{ |
| 104 | + "root_list_attribute": { |
| 105 | + Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{ |
| 106 | + "nested_list_attribute": { |
| 107 | + Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{ |
| 108 | + "deeply_nested_string_attribute": { |
| 109 | + Required: true, |
| 110 | + Type: types.StringType, |
| 111 | + }, |
| 112 | + }), |
| 113 | + Required: true, |
| 114 | + Validators: []tfsdk.AttributeValidator{ |
| 115 | + exampleValidatorThatAcceptsExpressions( |
| 116 | + path.MatchRelative().AtAnyListIndex().AtName("deeply_nested_string_attribute"), |
| 117 | + ), |
| 118 | + }, |
| 119 | + }, |
| 120 | + "nested_string_attribute": { |
| 121 | + Required: true, |
| 122 | + Type: types.StringType, |
| 123 | + }, |
| 124 | + }), |
| 125 | + Required: true, |
| 126 | + }, |
| 127 | + }, |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +This example shows a relative path expression which references a different attribute within the same list index: |
| 132 | + |
| 133 | +```go |
| 134 | +tfsdk.Schema{ |
| 135 | + Attributes: map[string]tfsdk.Attribute{ |
| 136 | + "root_list_attribute": { |
| 137 | + Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{ |
| 138 | + "nested_list_attribute": { |
| 139 | + Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{ |
| 140 | + "deeply_nested_string_attribute": { |
| 141 | + Required: true, |
| 142 | + Type: types.StringType, |
| 143 | + }, |
| 144 | + }), |
| 145 | + Required: true, |
| 146 | + Validators: []tfsdk.AttributeValidator{ |
| 147 | + exampleValidatorThatAcceptsExpressions( |
| 148 | + path.MatchRelative().AtParent().AtName("nested_string_attribute"), |
| 149 | + ), |
| 150 | + }, |
| 151 | + }, |
| 152 | + "nested_string_attribute": { |
| 153 | + Required: true, |
| 154 | + Type: types.StringType, |
| 155 | + }, |
| 156 | + }), |
| 157 | + Required: true, |
| 158 | + }, |
| 159 | + }, |
| 160 | +} |
| 161 | +``` |
| 162 | + |
| 163 | +### Building Expression Steps |
| 164 | + |
| 165 | +Expressions follow similar schema type rules as paths, in particular [Building Attribute Paths](/plugin/framework/paths#building-attribute-paths), [Building Nested Attribute Paths](/plugin/framework/paths#building-nested-attribute-paths), and [Building Block Paths](/plugin/framework/paths#building-block-paths). |
| 166 | + |
| 167 | +The following list shows the [`path.Expression` type](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#Expression) methods that behave similar to [`path.Path` type](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#Path) methods. |
| 168 | + |
| 169 | +- `AtListIndex()` |
| 170 | +- `AtMapKey()` |
| 171 | +- `AtName()` |
| 172 | +- `AtSetValue()` |
| 173 | + |
| 174 | +The following table shows the additional [`path.Expression` type](https://pkg.go.dev/github.com/hashicorp/terraform-plugin-framework/path#Expression) methods and their descriptions. |
| 175 | + |
| 176 | +| Expression Method | Description | |
| 177 | +| ------------------ | ----------- | |
| 178 | +| `AtAnyListIndex()` | Will return matches for any list index. Can be used anywhere `AtListIndex()` can be used. | |
| 179 | +| `AtAnyMapKey()` | Will return matches for any map key. Can be used anywhere `AtMapKey()` can be used. | |
| 180 | +| `AtAnySetValue()` | Will return matches for any set value. Can be used anywhere `AtSetValue()` can be used. | |
| 181 | +| `AtParent()` | Will remove the last expression step, or put differently, will match the path closer to the root of the schema. | |
| 182 | + |
0 commit comments