1717import dev .cel .expr .ExprValue ;
1818import dev .cel .expr .UnknownSet ;
1919import dev .cel .common .annotations .Internal ;
20- import java .util .Arrays ;
2120import java .util .LinkedHashSet ;
22- import java .util .List ;
2321import java .util .Set ;
2422import org .jspecify .annotations .Nullable ;
2523
@@ -56,8 +54,19 @@ public static Object strict(Object valueOrThrowable) throws InterpreterException
5654 * @return boolean value if object is unknown.
5755 */
5856 public static boolean isUnknown (Object obj ) {
59- return obj instanceof ExprValue
60- && ((ExprValue ) obj ).getKindCase () == ExprValue .KindCase .UNKNOWN ;
57+ if (isExprValueUnknown (obj )) {
58+ return true ;
59+ }
60+ return obj instanceof CelUnknownSet ;
61+ }
62+
63+ /** TODO: Remove once clients have been migrated. */
64+ static boolean isExprValueUnknown (Object obj ) {
65+ if (obj instanceof ExprValue ) {
66+ return ((ExprValue ) obj ).getKindCase () == ExprValue .KindCase .UNKNOWN ;
67+ }
68+
69+ return false ;
6170 }
6271
6372 /**
@@ -67,7 +76,7 @@ public static boolean isUnknown(Object obj) {
6776 * @return A new ExprValue object which has all unknown expr ids from input objects, without
6877 * duplication.
6978 */
70- public static ExprValue combineUnknownExprValue (Object ... objs ) {
79+ static ExprValue combineUnknownProtoExprValue (Object ... objs ) {
7180 UnknownSet .Builder unknownsetBuilder = UnknownSet .newBuilder ();
7281 Set <Long > ids = new LinkedHashSet <>();
7382 for (Object object : objs ) {
@@ -79,21 +88,25 @@ public static ExprValue combineUnknownExprValue(Object... objs) {
7988 return ExprValue .newBuilder ().setUnknown (unknownsetBuilder ).build ();
8089 }
8190
82- /** Create a {@code ExprValue} for one or more {@code ids} representing an unknown set. */
83- public static ExprValue createUnknownExprValue (Long ... ids ) {
84- return createUnknownExprValue (Arrays .asList (ids ));
91+ static CelUnknownSet combineUnknownExprValue (Object ... objs ) {
92+ Set <Long > ids = new LinkedHashSet <>();
93+ for (Object object : objs ) {
94+ if (isUnknown (object )) {
95+ ids .addAll (((CelUnknownSet ) object ).unknownExprIds ());
96+ }
97+ }
98+
99+ return CelUnknownSet .create (ids );
85100 }
86101
87102 /**
88103 * Create an ExprValue object has UnknownSet, from a list of unknown expr ids
89104 *
90- * @param ids List of unknown expr ids
105+ * @param id unknown expr id
91106 * @return A new ExprValue object which has all unknown expr ids from input list
92107 */
93- public static ExprValue createUnknownExprValue (List <Long > ids ) {
94- ExprValue .Builder exprValueBuilder = ExprValue .newBuilder ();
95- exprValueBuilder .setUnknown (UnknownSet .newBuilder ().addAllExprs (ids ));
96- return exprValueBuilder .build ();
108+ private static ExprValue createUnknownExprValue (long id ) {
109+ return ExprValue .newBuilder ().setUnknown (UnknownSet .newBuilder ().addExprs (id )).build ();
97110 }
98111
99112 /**
@@ -104,11 +117,14 @@ public static ExprValue createUnknownExprValue(List<Long> ids) {
104117 * from any boolean arguments alone. This allows us to consolidate the error/unknown handling for
105118 * both of these operators.
106119 */
107- public static Object shortcircuitUnknownOrThrowable (Object left , Object right )
120+ public static Object shortcircuitUnknownOrThrowable (
121+ Object left , Object right , boolean adaptUnknownValueSetToNativeType )
108122 throws InterpreterException {
109123 // unknown <op> unknown ==> unknown combined
110124 if (InterpreterUtil .isUnknown (left ) && InterpreterUtil .isUnknown (right )) {
111- return InterpreterUtil .combineUnknownExprValue (left , right );
125+ return adaptUnknownValueSetToNativeType
126+ ? InterpreterUtil .combineUnknownExprValue (left , right )
127+ : InterpreterUtil .combineUnknownProtoExprValue (left , right );
112128 }
113129 // unknown <op> <error> ==> unknown
114130 // unknown <op> t|f ==> unknown
@@ -132,18 +148,24 @@ public static Object shortcircuitUnknownOrThrowable(Object left, Object right)
132148 "Left or/and right object is neither bool, unknown nor error, unexpected behavior." );
133149 }
134150
135- public static Object valueOrUnknown (@ Nullable Object valueOrThrowable , Long id ) {
151+ public static Object valueOrUnknown (
152+ @ Nullable Object valueOrThrowable , Long id , boolean adaptUnknownValueSet ) {
136153 // Handle the unknown value case.
137154 if (isUnknown (valueOrThrowable )) {
138- ExprValue value = (ExprValue ) valueOrThrowable ;
139- if (value .getUnknown ().getExprsCount () != 0 ) {
140- return valueOrThrowable ;
155+ if (adaptUnknownValueSet ) {
156+ return CelUnknownSet .create (id );
157+ } else {
158+ // TODO: Remove once clients have been migrated.
159+ ExprValue value = (ExprValue ) valueOrThrowable ;
160+ if (value .getUnknown ().getExprsCount () != 0 ) {
161+ return valueOrThrowable ;
162+ }
163+ return createUnknownExprValue (id );
141164 }
142- return createUnknownExprValue (id );
143165 }
144166 // Handle the null value case.
145167 if (valueOrThrowable == null ) {
146- return createUnknownExprValue (id );
168+ return adaptUnknownValueSet ? CelUnknownSet . create ( id ) : createUnknownExprValue (id );
147169 }
148170 return valueOrThrowable ;
149171 }
0 commit comments