-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathresolve.go
More file actions
47 lines (38 loc) · 1.85 KB
/
resolve.go
File metadata and controls
47 lines (38 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package pathutils
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
// PathMatchExpressionsAgainstAttributeConfig returns the path.Paths matching the given path.Expressions.
//
// Each path.Expression has been merged with the given attribute path.Expression
// (likely from the tfsdk.ValidateAttributeRequest), resolved,
// and then matched against the given attribute tfsdk.Config (also from the tfsdk.ValidateAttributeRequest).
//
// This is useful for tfsdk.AttributeValidator that accept path.Expressions, and validate the attributes matching
// to the expressions, in relation to the attribute the validator is applied to.
// For example usage, please look at the `schemavalidator` package in this repository.
func PathMatchExpressionsAgainstAttributeConfig(ctx context.Context, pathExps path.Expressions, attrPathExp path.Expression, attrConfig tfsdk.Config) (path.Paths, diag.Diagnostics) {
var resDiags diag.Diagnostics
pathExpressions := MergeExpressionsWithAttribute(pathExps, attrPathExp)
resPaths := make(path.Paths, 0, len(pathExpressions))
for _, pe := range pathExpressions {
// Retrieve all the attribute paths that match the given expressions
matchingPaths, diags := attrConfig.PathMatches(ctx, pe)
resDiags.Append(diags...)
if diags.HasError() {
return nil, resDiags
}
// Confirm at least one attribute was matched.
// If not, collect errors so that the callee can bubble the bugs up.
if len(matchingPaths) == 0 {
resDiags.Append(validatordiag.BugInProviderDiagnostic(fmt.Sprintf("Path expression %q matches no attribute", pe)))
}
resPaths = append(resPaths, matchingPaths...)
}
return resPaths, resDiags
}