|
| 1 | +package path |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/hashicorp/terraform-plugin-framework/attr" |
| 5 | +) |
| 6 | + |
| 7 | +// Expression represents an attribute path with expression steps, which can |
| 8 | +// represent zero, one, or more actual Paths. |
| 9 | +type Expression struct { |
| 10 | + // steps is the transversals included with the expression. In general, |
| 11 | + // operations against the path should protect against modification of the |
| 12 | + // original. |
| 13 | + steps ExpressionSteps |
| 14 | +} |
| 15 | + |
| 16 | +// AtAnyListIndex returns a copied expression with a new list index step at the |
| 17 | +// end. The returned path is safe to modify without affecting the original. |
| 18 | +func (e Expression) AtAnyListIndex() Expression { |
| 19 | + copiedPath := e.Copy() |
| 20 | + |
| 21 | + copiedPath.steps.Append(ExpressionStepElementKeyIntAny{}) |
| 22 | + |
| 23 | + return copiedPath |
| 24 | +} |
| 25 | + |
| 26 | +// AtAnyMapKey returns a copied expression with a new map key step at the end. |
| 27 | +// The returned path is safe to modify without affecting the original. |
| 28 | +func (e Expression) AtAnyMapKey() Expression { |
| 29 | + copiedPath := e.Copy() |
| 30 | + |
| 31 | + copiedPath.steps.Append(ExpressionStepElementKeyStringAny{}) |
| 32 | + |
| 33 | + return copiedPath |
| 34 | +} |
| 35 | + |
| 36 | +// AtAnySetValue returns a copied expression with a new set value step at the |
| 37 | +// end. The returned path is safe to modify without affecting the original. |
| 38 | +func (e Expression) AtAnySetValue() Expression { |
| 39 | + copiedPath := e.Copy() |
| 40 | + |
| 41 | + copiedPath.steps.Append(ExpressionStepElementKeyValueAny{}) |
| 42 | + |
| 43 | + return copiedPath |
| 44 | +} |
| 45 | + |
| 46 | +// AtListIndex returns a copied expression with a new list index step at the |
| 47 | +// end. The returned path is safe to modify without affecting the original. |
| 48 | +func (e Expression) AtListIndex(index int) Expression { |
| 49 | + copiedPath := e.Copy() |
| 50 | + |
| 51 | + copiedPath.steps.Append(ExpressionStepElementKeyIntExact(index)) |
| 52 | + |
| 53 | + return copiedPath |
| 54 | +} |
| 55 | + |
| 56 | +// AtMapKey returns a copied expression with a new map key step at the end. |
| 57 | +// The returned path is safe to modify without affecting the original. |
| 58 | +func (e Expression) AtMapKey(key string) Expression { |
| 59 | + copiedPath := e.Copy() |
| 60 | + |
| 61 | + copiedPath.steps.Append(ExpressionStepElementKeyStringExact(key)) |
| 62 | + |
| 63 | + return copiedPath |
| 64 | +} |
| 65 | + |
| 66 | +// AtName returns a copied expression with a new attribute or block name step |
| 67 | +// at the end. The returned path is safe to modify without affecting the |
| 68 | +// original. |
| 69 | +func (e Expression) AtName(name string) Expression { |
| 70 | + copiedPath := e.Copy() |
| 71 | + |
| 72 | + copiedPath.steps.Append(ExpressionStepAttributeNameExact(name)) |
| 73 | + |
| 74 | + return copiedPath |
| 75 | +} |
| 76 | + |
| 77 | +// AtParent returns a copied expression with a new parent step at the end. |
| 78 | +// The returned path is safe to modify without affecting the original. |
| 79 | +func (e Expression) AtParent() Expression { |
| 80 | + copiedPath := e.Copy() |
| 81 | + |
| 82 | + copiedPath.steps.Append(ExpressionStepParent{}) |
| 83 | + |
| 84 | + return copiedPath |
| 85 | +} |
| 86 | + |
| 87 | +// AtSetValue returns a copied expression with a new set value step at the end. |
| 88 | +// The returned path is safe to modify without affecting the original. |
| 89 | +func (e Expression) AtSetValue(value attr.Value) Expression { |
| 90 | + copiedPath := e.Copy() |
| 91 | + |
| 92 | + copiedPath.steps.Append(ExpressionStepElementKeyValueExact{Value: value}) |
| 93 | + |
| 94 | + return copiedPath |
| 95 | +} |
| 96 | + |
| 97 | +// Copy returns a duplicate of the expression that is safe to modify without |
| 98 | +// affecting the original. |
| 99 | +func (e Expression) Copy() Expression { |
| 100 | + return Expression{ |
| 101 | + steps: e.Steps(), |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +// Equal returns true if the given expression is exactly equivalent. |
| 106 | +func (e Expression) Equal(o Expression) bool { |
| 107 | + if e.steps == nil && o.steps == nil { |
| 108 | + return true |
| 109 | + } |
| 110 | + |
| 111 | + if e.steps == nil { |
| 112 | + return false |
| 113 | + } |
| 114 | + |
| 115 | + if !e.steps.Equal(o.steps) { |
| 116 | + return false |
| 117 | + } |
| 118 | + |
| 119 | + return true |
| 120 | +} |
| 121 | + |
| 122 | +// Matches returns true if the given Path is valid for the Expression. |
| 123 | +func (e Expression) Matches(path Path) bool { |
| 124 | + return e.steps.Matches(path.Steps()) |
| 125 | +} |
| 126 | + |
| 127 | +// Steps returns a copy of the underlying expression steps. Returns an empty |
| 128 | +// collection of steps if expression is nil. |
| 129 | +func (e Expression) Steps() ExpressionSteps { |
| 130 | + if len(e.steps) == 0 { |
| 131 | + return ExpressionSteps{} |
| 132 | + } |
| 133 | + |
| 134 | + return e.steps.Copy() |
| 135 | +} |
| 136 | + |
| 137 | +// String returns the human-readable representation of the path. |
| 138 | +// It is intended for logging and error messages and is not protected by |
| 139 | +// compatibility guarantees. |
| 140 | +func (e Expression) String() string { |
| 141 | + return e.steps.String() |
| 142 | +} |
| 143 | + |
| 144 | +// MatchParent creates an attribute path expression starting with |
| 145 | +// ExpressionStepParent. This allows creating a relative expression in |
| 146 | +// nested schemas. |
| 147 | +func MatchParent() Expression { |
| 148 | + return Expression{ |
| 149 | + steps: ExpressionSteps{ |
| 150 | + ExpressionStepParent{}, |
| 151 | + }, |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +// MatchRoot creates an attribute path expression starting with |
| 156 | +// ExpressionStepAttributeNameExact. |
| 157 | +func MatchRoot(rootAttributeName string) Expression { |
| 158 | + return Expression{ |
| 159 | + steps: ExpressionSteps{ |
| 160 | + ExpressionStepAttributeNameExact(rootAttributeName), |
| 161 | + }, |
| 162 | + } |
| 163 | +} |
0 commit comments