Skip to content

Commit b451c16

Browse files
committed
jsonpath/eval: graceful type unmatched handling
Previously, if the type is not found in the switch case, we panic with a unhandled type error. Now we implement a more graceful way for erroring out. Release note: None
1 parent ac39c2e commit b451c16

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

pkg/util/jsonpath/eval/eval.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ func (ctx *jsonpathCtx) executeAnyItem(
274274
}
275275
}
276276
default:
277-
panic(errors.AssertionFailedf("executeAnyItem called with type: %s", jsonValue.Type()))
277+
return nil, errors.AssertionFailedf("executeAnyItem called with type: %s", jsonValue.Type())
278278
}
279279
return agg, nil
280280
}

pkg/util/jsonpath/eval/method.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (ctx *jsonpathCtx) evalNumericMethod(
8383
dec.Negative = false
8484
}
8585
default:
86-
panic(errors.Newf("unimplemented method: %s", method.Type))
86+
return nil, errors.Newf("unsupported jsonpath method type %d for numeric evaluation", method.Type)
8787
}
8888
if err != nil {
8989
return nil, err

pkg/util/jsonpath/eval/operation.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (ctx *jsonpathCtx) evalOperation(
7575
case jsonpath.OpPlus, jsonpath.OpMinus:
7676
return ctx.evalUnaryArithmetic(op, jsonValue)
7777
default:
78-
panic(errors.AssertionFailedf("unhandled operation type"))
78+
return nil, errors.AssertionFailedf("unhandled operation type")
7979
}
8080
}
8181

@@ -98,7 +98,7 @@ func (ctx *jsonpathCtx) evalBoolean(
9898
case jsonpath.OpStartsWith:
9999
return ctx.evalPredicate(op, jsonValue, evalStartsWithFunc, true /* evalRight */, false /* unwrapRight */)
100100
default:
101-
panic(errors.AssertionFailedf("unhandled operation type"))
101+
return jsonpathBoolUnknown, errors.AssertionFailedf("unhandled operation type")
102102
}
103103
}
104104

@@ -224,7 +224,7 @@ func (ctx *jsonpathCtx) evalLogical(
224224
}
225225
return jsonpathBoolTrue, nil
226226
default:
227-
panic(errors.AssertionFailedf("unhandled logical operation type"))
227+
return jsonpathBoolUnknown, errors.AssertionFailedf("unhandled logical operation type")
228228
}
229229

230230
rightOp, ok := op.Right.(jsonpath.Operation)
@@ -247,7 +247,7 @@ func (ctx *jsonpathCtx) evalLogical(
247247
}
248248
return rightBool, nil
249249
default:
250-
panic(errors.AssertionFailedf("unhandled logical operation type"))
250+
return jsonpathBoolUnknown, errors.AssertionFailedf("unhandled logical operation type")
251251
}
252252
}
253253

@@ -359,7 +359,7 @@ func evalComparisonFunc(operation jsonpath.Operation, l, r json.JSON) (jsonpathB
359359
// Don't evaluate non-scalar types.
360360
return jsonpathBoolUnknown, nil
361361
default:
362-
panic(errors.AssertionFailedf("unhandled json type"))
362+
return jsonpathBoolUnknown, errors.AssertionFailedf("unhandled json type")
363363
}
364364

365365
var res bool
@@ -377,7 +377,7 @@ func evalComparisonFunc(operation jsonpath.Operation, l, r json.JSON) (jsonpathB
377377
case jsonpath.OpCompGreaterEqual:
378378
res = cmp >= 0
379379
default:
380-
panic(errors.AssertionFailedf("unhandled jsonpath comparison type"))
380+
return jsonpathBoolUnknown, errors.AssertionFailedf("unhandled jsonpath comparison type")
381381
}
382382
if res {
383383
return jsonpathBoolTrue, nil
@@ -473,7 +473,7 @@ func performArithmetic(
473473
}
474474
_, err = tree.DecimalCtx.Rem(&res, leftNum, rightNum)
475475
default:
476-
panic(errors.AssertionFailedf("unhandled jsonpath arithmetic type"))
476+
return nil, errors.AssertionFailedf("unhandled jsonpath arithmetic type")
477477
}
478478
if err != nil {
479479
return nil, err

0 commit comments

Comments
 (0)