Skip to content
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

Fix path matching in AtLeastProductOfValidator.Validate() #746

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 34 additions & 31 deletions apstra/apstra_validator/at_least_product_of.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,42 +65,45 @@ func (o AtLeastProductOfValidator) Validate(ctx context.Context, req AtLeastProd

multiplier := big.NewFloat(o.Multiplier)

matchedPaths, diags := req.Config.PathMatches(ctx, o.PathExpression)
resp.Diagnostics.Append(diags...)
if diags.HasError() {
return
}

for _, mp := range matchedPaths {
var mpVal attr.Value
diags = req.Config.GetAttribute(ctx, mp, &mpVal)
expressions := req.PathExpression.MergeExpressions(o.PathExpression)
for _, expression := range expressions {
matchedPaths, diags := req.Config.PathMatches(ctx, expression)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Unknown and Null attributes can't be multiplied
if mpVal.IsNull() || mpVal.IsUnknown() {
continue
}

// convert the value we're comparing to *big.Float
var mpBF *big.Float
switch t := mpVal.(type) {
case basetypes.Float64Value:
mpBF = big.NewFloat(t.ValueFloat64())
case basetypes.Int64Value:
mpBF = big.NewFloat(float64(t.ValueInt64()))
case basetypes.NumberValue:
mpBF = t.ValueBigFloat()
}

if thisBF.Cmp(mpBF.Mul(mpBF, multiplier)) == -1 {
resp.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("value must be at least %f times the value at %s (%s), got %s",
multiplier, mp, mpVal, req.ConfigValue),
))
for _, mp := range matchedPaths {
var mpVal attr.Value
diags = req.Config.GetAttribute(ctx, mp, &mpVal)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

// Unknown and Null attributes can't be multiplied
if mpVal.IsNull() || mpVal.IsUnknown() {
continue
}

// convert the value we're comparing to *big.Float
var mpBF *big.Float
switch t := mpVal.(type) {
case basetypes.Float64Value:
mpBF = big.NewFloat(t.ValueFloat64())
case basetypes.Int64Value:
mpBF = big.NewFloat(float64(t.ValueInt64()))
case basetypes.NumberValue:
mpBF = t.ValueBigFloat()
}

if thisBF.Cmp(mpBF.Mul(mpBF, multiplier)) == -1 {
resp.Diagnostics.Append(validatordiag.InvalidAttributeCombinationDiagnostic(
req.Path,
fmt.Sprintf("value must be at least %f times the value at %s (%s), got %s",
multiplier, mp, mpVal, req.ConfigValue),
))
}
}
}
}
Expand Down
Loading