|
17 | 17 | import static com.google.common.base.Preconditions.checkArgument; |
18 | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
19 | 19 | import static com.google.common.collect.ImmutableList.toImmutableList; |
| 20 | +import static dev.cel.common.Operator.INDEX; |
| 21 | +import static dev.cel.common.Operator.OPTIONAL_INDEX; |
| 22 | +import static dev.cel.common.Operator.OPTIONAL_SELECT; |
20 | 23 | import static dev.cel.extensions.CelOptionalLibrary.Function.FIRST; |
21 | 24 | import static dev.cel.extensions.CelOptionalLibrary.Function.HAS_VALUE; |
22 | 25 | import static dev.cel.extensions.CelOptionalLibrary.Function.LAST; |
@@ -342,54 +345,69 @@ public void setRuntimeOptions( |
342 | 345 | "optional_hasValue", Object.class, val -> ((Optional<?>) val).isPresent()))); |
343 | 346 |
|
344 | 347 | runtimeBuilder.addFunctionBindings( |
345 | | - CelFunctionBinding.from( |
346 | | - "select_optional_field", // This only handles map selection. Proto selection is |
347 | | - // special cased inside the interpreter. |
348 | | - Map.class, |
349 | | - String.class, |
350 | | - runtimeEquality::findInMap), |
351 | | - CelFunctionBinding.from( |
352 | | - "map_optindex_optional_value", Map.class, Object.class, runtimeEquality::findInMap), |
353 | | - CelFunctionBinding.from( |
354 | | - "optional_map_optindex_optional_value", |
355 | | - Optional.class, |
356 | | - Object.class, |
357 | | - (Optional optionalMap, Object key) -> |
358 | | - indexOptionalMap(optionalMap, key, runtimeEquality)), |
359 | | - CelFunctionBinding.from( |
360 | | - "optional_map_index_value", |
361 | | - Optional.class, |
362 | | - Object.class, |
363 | | - (Optional optionalMap, Object key) -> |
364 | | - indexOptionalMap(optionalMap, key, runtimeEquality)), |
365 | | - CelFunctionBinding.from( |
366 | | - "optional_list_index_int", |
367 | | - Optional.class, |
368 | | - Long.class, |
369 | | - CelOptionalLibrary::indexOptionalList), |
370 | | - CelFunctionBinding.from( |
371 | | - "list_optindex_optional_int", |
372 | | - List.class, |
373 | | - Long.class, |
374 | | - (List list, Long index) -> { |
375 | | - int castIndex = Ints.checkedCast(index); |
376 | | - if (castIndex < 0 || castIndex >= list.size()) { |
377 | | - return Optional.empty(); |
378 | | - } |
379 | | - return Optional.of(list.get(castIndex)); |
380 | | - }), |
381 | | - CelFunctionBinding.from( |
382 | | - "optional_list_optindex_optional_int", |
383 | | - Optional.class, |
384 | | - Long.class, |
385 | | - CelOptionalLibrary::indexOptionalList)); |
| 348 | + fromOverloads( |
| 349 | + OPTIONAL_SELECT.getFunction(), |
| 350 | + CelFunctionBinding.from( |
| 351 | + "select_optional_field", // This only handles map selection. Proto selection is |
| 352 | + // special cased inside the interpreter. |
| 353 | + Map.class, |
| 354 | + String.class, |
| 355 | + runtimeEquality::findInMap))); |
| 356 | + |
| 357 | + runtimeBuilder.addFunctionBindings( |
| 358 | + fromOverloads( |
| 359 | + OPTIONAL_INDEX.getFunction(), |
| 360 | + CelFunctionBinding.from( |
| 361 | + "list_optindex_optional_int", |
| 362 | + List.class, |
| 363 | + Long.class, |
| 364 | + (List list, Long index) -> { |
| 365 | + int castIndex = Ints.checkedCast(index); |
| 366 | + if (castIndex < 0 || castIndex >= list.size()) { |
| 367 | + return Optional.empty(); |
| 368 | + } |
| 369 | + return Optional.of(list.get(castIndex)); |
| 370 | + }), |
| 371 | + CelFunctionBinding.from( |
| 372 | + "optional_list_optindex_optional_int", |
| 373 | + Optional.class, |
| 374 | + Long.class, |
| 375 | + CelOptionalLibrary::indexOptionalList), |
| 376 | + CelFunctionBinding.from( |
| 377 | + "map_optindex_optional_value", Map.class, Object.class, runtimeEquality::findInMap), |
| 378 | + CelFunctionBinding.from( |
| 379 | + "optional_map_optindex_optional_value", |
| 380 | + Optional.class, |
| 381 | + Object.class, |
| 382 | + (Optional optionalMap, Object key) -> |
| 383 | + indexOptionalMap(optionalMap, key, runtimeEquality)))); |
| 384 | + |
| 385 | + runtimeBuilder.addFunctionBindings( |
| 386 | + fromOverloads( |
| 387 | + INDEX.getFunction(), |
| 388 | + CelFunctionBinding.from( |
| 389 | + "optional_list_index_int", |
| 390 | + Optional.class, |
| 391 | + Long.class, |
| 392 | + CelOptionalLibrary::indexOptionalList), |
| 393 | + CelFunctionBinding.from( |
| 394 | + "optional_map_index_value", |
| 395 | + Optional.class, |
| 396 | + Object.class, |
| 397 | + (Optional optionalMap, Object key) -> |
| 398 | + indexOptionalMap(optionalMap, key, runtimeEquality)))); |
386 | 399 |
|
387 | 400 | if (version >= 2) { |
388 | 401 | runtimeBuilder.addFunctionBindings( |
389 | | - CelFunctionBinding.from( |
390 | | - "optional_list_first", Collection.class, CelOptionalLibrary::listOptionalFirst), |
391 | | - CelFunctionBinding.from( |
392 | | - "optional_list_last", Collection.class, CelOptionalLibrary::listOptionalLast)); |
| 402 | + fromOverloads( |
| 403 | + "first", |
| 404 | + CelFunctionBinding.from( |
| 405 | + "optional_list_first", Collection.class, CelOptionalLibrary::listOptionalFirst))); |
| 406 | + runtimeBuilder.addFunctionBindings( |
| 407 | + fromOverloads( |
| 408 | + "last", |
| 409 | + CelFunctionBinding.from( |
| 410 | + "optional_list_last", Collection.class, CelOptionalLibrary::listOptionalLast))); |
393 | 411 | } |
394 | 412 | } |
395 | 413 |
|
|
0 commit comments