Skip to content

Commit

Permalink
Merge pull request #746 from Juniper/bug/745-fix-path-matching-in-at-…
Browse files Browse the repository at this point in the history
…least-n-of-validator

Fix path matching in `AtLeastProductOfValidator.Validate()`
  • Loading branch information
chrismarget-j authored Jul 25, 2024
2 parents 5d4c8a3 + 09bfa7c commit 65a00e6
Showing 1 changed file with 34 additions and 31 deletions.
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

0 comments on commit 65a00e6

Please sign in to comment.