-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(fabricitem): enable multi-format support
- Loading branch information
1 parent
edb6fa7
commit e597bc8
Showing
26 changed files
with
276 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
internal/framework/validators/regexp_if_attribute_is_one_of.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
package validators | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/path" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
) | ||
|
||
var _ validator.String = RegexpIfAttributeIsOneOfValidator{} | ||
|
||
type RegexpIfAttributeIsOneOfValidator struct { | ||
pathExpression path.Expression | ||
exceptedValues []attr.Value | ||
patterns []string | ||
message string | ||
} | ||
|
||
func RegexpIfAttributeIsOneOf(p path.Expression, exceptedValue []attr.Value, patterns []string, message string) RegexpIfAttributeIsOneOfValidator { | ||
return RegexpIfAttributeIsOneOfValidator{ | ||
pathExpression: p, | ||
exceptedValues: exceptedValue, | ||
patterns: patterns, | ||
message: message, | ||
} | ||
} | ||
|
||
func (v RegexpIfAttributeIsOneOfValidator) Description(_ context.Context) string { | ||
if v.message != "" { | ||
return v.message | ||
} | ||
|
||
return fmt.Sprintf("value must match pattern expression '%s'", strings.Join(v.patterns, ", ")) | ||
} | ||
|
||
func (v RegexpIfAttributeIsOneOfValidator) MarkdownDescription(ctx context.Context) string { | ||
return v.Description(ctx) | ||
} | ||
|
||
func (v RegexpIfAttributeIsOneOfValidator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { | ||
paths, diags := req.Config.PathMatches(ctx, req.PathExpression.Merge(v.pathExpression)) | ||
if diags.HasError() { | ||
resp.Diagnostics.Append(diags...) | ||
|
||
return | ||
} | ||
|
||
if len(paths) == 0 { | ||
resp.Diagnostics.AddError( | ||
fmt.Sprintf("Invalid configuration for attribute %s", req.Path), | ||
"Path must be set", | ||
) | ||
|
||
return | ||
} | ||
|
||
p := paths[0] | ||
|
||
// mpVal is the value of the attribute in the path | ||
var mpVal attr.Value | ||
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, p, &mpVal)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
resp.Diagnostics.AddError( | ||
fmt.Sprintf("Invalid configuration for attribute %s", req.Path), | ||
fmt.Sprintf("Unable to retrieve attribute path: %q", p), | ||
) | ||
|
||
return | ||
} | ||
|
||
// If the target attribute configuration is unknown or null, there is nothing else to validate | ||
if mpVal.IsNull() || mpVal.IsUnknown() { | ||
return | ||
} | ||
|
||
for _, expectedValue := range v.exceptedValues { | ||
// If the value of the target attribute is equal to one of the expected values, we need to validate the value of the current attribute | ||
if mpVal.Equal(expectedValue) || mpVal.String() == expectedValue.String() { | ||
if req.ConfigValue.IsNull() || req.ConfigValue.IsUnknown() { | ||
resp.Diagnostics.AddAttributeError( | ||
p, | ||
fmt.Sprintf("Invalid configuration for attribute %s", req.Path), | ||
"Value is empty. "+v.Description(ctx), | ||
) | ||
|
||
return | ||
} | ||
|
||
re, err := v.convertPatternsToRegexp(v.patterns) | ||
if err != nil { | ||
resp.Diagnostics.AddError( | ||
fmt.Sprintf("Invalid configuration for attribute %s", req.Path), | ||
fmt.Sprintf("Unable to compile regular expression: %q", err), | ||
) | ||
|
||
return | ||
} | ||
|
||
value := req.ConfigValue.ValueString() | ||
|
||
if !re.MatchString(value) { | ||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueMatchDiagnostic( | ||
req.Path, | ||
v.Description(ctx), | ||
value, | ||
)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (v RegexpIfAttributeIsOneOfValidator) convertPatternsToRegexp(patterns []string) (*regexp.Regexp, error) { | ||
p := make([]string, 0) | ||
|
||
p = append(p, "^(") | ||
|
||
for _, pattern := range patterns { | ||
p = append(p, regexp.QuoteMeta(pattern)) | ||
if pattern != patterns[len(patterns)-1] { | ||
p = append(p, "|") | ||
} | ||
} | ||
|
||
p = append(p, ")$") | ||
|
||
out := strings.Join(p, "") | ||
out = strings.ReplaceAll(out, `\*`, ".+") | ||
|
||
re, err := regexp.Compile(out) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return re, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.