use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.
the class Env method check.
/**
* Check performs type-checking on the input Ast and yields a checked Ast and/or set of Issues.
*
* <p>Checking has failed if the returned Issues value and its Issues.Err() value are non-nil.
* Issues should be inspected if they are non-nil, but may not represent a fatal error.
*
* <p>It is possible to have both non-nil Ast and Issues values returned from this call: however,
* the mere presence of an Ast does not imply that it is valid for use.
*/
public AstIssuesTuple check(Ast ast) {
// Note, errors aren't currently possible on the Ast to ParsedExpr conversion.
ParsedExpr pe = astToParsedExpr(ast);
// Construct the internal checker env, erroring if there is an issue adding the declarations.
synchronized (once) {
if (chk == null && chkErr == null) {
CheckerEnv ce = CheckerEnv.newCheckerEnv(container, provider);
ce.enableDynamicAggregateLiterals(true);
if (hasFeature(FeatureDisableDynamicAggregateLiterals)) {
ce.enableDynamicAggregateLiterals(false);
}
try {
ce.add(declarations);
chk = ce;
} catch (RuntimeException e) {
chkErr = e;
} catch (Exception e) {
chkErr = new RuntimeException(e);
}
}
}
// The once call will ensure that this value is set or nil for all invocations.
if (chkErr != null) {
Errors errs = new Errors(ast.getSource());
errs.reportError(NoLocation, "%s", chkErr.toString());
return new AstIssuesTuple(null, newIssues(errs));
}
ParseResult pr = new ParseResult(pe.getExpr(), new Errors(ast.getSource()), pe.getSourceInfo());
CheckResult checkRes = Checker.Check(pr, ast.getSource(), chk);
if (checkRes.hasErrors()) {
return new AstIssuesTuple(null, newIssues(checkRes.getErrors()));
}
// Manually create the Ast to ensure that the Ast source information (which may be more
// detailed than the information provided by Check), is returned to the caller.
CheckedExpr ce = checkRes.getCheckedExpr();
ast = new Ast(ce.getExpr(), ce.getSourceInfo(), ast.getSource(), ce.getReferenceMapMap(), ce.getTypeMapMap());
return new AstIssuesTuple(ast, Issues.noIssues(ast.getSource()));
}
use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.
the class ProviderTest method convertToNative.
@Test
void convertToNative() {
TypeRegistry reg = newRegistry(ParsedExpr.getDefaultInstance());
// Core type conversion tests.
expectValueToNative(True, true);
expectValueToNative(True, True);
expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Object[] { true, false });
expectValueToNative(newGenericArrayList(reg, new Val[] { True, False }), new Val[] { True, False });
expectValueToNative(intOf(-1), -1);
expectValueToNative(intOf(2), 2L);
expectValueToNative(intOf(-1), -1);
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(4) }), new Object[] { 4L });
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(5) }), new Val[] { intOf(5) });
expectValueToNative(uintOf(3), ULong.valueOf(3));
expectValueToNative(uintOf(4), ULong.valueOf(4));
expectValueToNative(uintOf(5), 5);
expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(4) }), // loses "ULong" here
new Object[] { 4L });
expectValueToNative(newGenericArrayList(reg, new Val[] { uintOf(5) }), new Val[] { uintOf(5) });
expectValueToNative(doubleOf(5.5d), 5.5f);
expectValueToNative(doubleOf(-5.5d), -5.5d);
expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Object[] { -5.5 });
expectValueToNative(newGenericArrayList(reg, new Val[] { doubleOf(-5.5) }), new Val[] { doubleOf(-5.5) });
expectValueToNative(doubleOf(-5.5), doubleOf(-5.5));
expectValueToNative(stringOf("hello"), "hello");
expectValueToNative(stringOf("hello"), stringOf("hello"));
expectValueToNative(NullValue, NULL_VALUE);
expectValueToNative(NullValue, NullValue);
expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Object[] { null });
expectValueToNative(newGenericArrayList(reg, new Val[] { NullValue }), new Val[] { NullValue });
expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
expectValueToNative(bytesOf("world"), "world".getBytes(StandardCharsets.UTF_8));
expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Object[] { ByteString.copyFromUtf8("hello") });
expectValueToNative(newGenericArrayList(reg, new Val[] { bytesOf("hello") }), new Val[] { bytesOf("hello") });
expectValueToNative(newGenericArrayList(reg, new Val[] { intOf(1), intOf(2), intOf(3) }), new Object[] { 1L, 2L, 3L });
expectValueToNative(durationOf(Duration.ofSeconds(500)), Duration.ofSeconds(500));
expectValueToNative(durationOf(Duration.ofSeconds(500)), com.google.protobuf.Duration.newBuilder().setSeconds(500).build());
expectValueToNative(durationOf(Duration.ofSeconds(500)), durationOf(Duration.ofSeconds(500)));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Instant.ofEpochSecond(12345, 0).atZone(ZoneIdZ));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), timestampOf(Timestamp.newBuilder().setSeconds(12345).build()));
expectValueToNative(timestampOf(Timestamp.newBuilder().setSeconds(12345).build()), Timestamp.newBuilder().setSeconds(12345).build());
expectValueToNative(newMaybeWrappedMap(reg, mapOf(1L, 1L, 2L, 1L, 3L, 1L)), mapOf(1L, 1L, 2L, 1L, 3L, 1L));
// Null conversion tests.
expectValueToNative(NullValue, NULL_VALUE);
// Proto conversion tests.
ParsedExpr parsedExpr = ParsedExpr.getDefaultInstance();
expectValueToNative(reg.nativeToValue(parsedExpr), parsedExpr);
}
use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.
the class PbObjectTest method protoObjectConvertToType.
@Test
void protoObjectConvertToType() {
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
Val obj = reg.nativeToValue(msg);
assertThat(obj).isInstanceOf(ObjectT.class);
ObjectT objVal = (ObjectT) obj;
Type tv = objVal.type();
assertThat(objVal.convertToType(TypeType).equal(tv)).isSameAs(True);
assertThat(objVal.convertToType(objVal.type())).isSameAs(objVal);
}
use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.
the class PbObjectTest method protoObjectConvertToNative.
@Test
void protoObjectConvertToNative() throws Exception {
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
Val objVal = reg.nativeToValue(msg);
// Proto Message
ParsedExpr val = objVal.convertToNative(ParsedExpr.class);
assertThat(val).isEqualTo(msg);
// Dynamic protobuf
Val dynPB = reg.newValue(msg.getDescriptorForType().getFullName(), Collections.singletonMap("source_info", reg.nativeToValue(msg.getSourceInfo())));
Val dynVal = reg.nativeToValue(dynPB);
val = dynVal.convertToNative(msg.getClass());
assertThat(val).isEqualTo(msg);
// google.protobuf.Any
Any anyVal = objVal.convertToNative(Any.class);
Message unpackedAny = anyVal.unpack(ParsedExpr.class);
assertThat(unpackedAny).isEqualTo(objVal.value());
}
use of com.google.api.expr.v1alpha1.ParsedExpr in project cel-java by projectnessie.
the class PbObjectTest method protoObjectGet.
@Test
void protoObjectGet() {
TypeRegistry reg = newRegistry(Expr.getDefaultInstance());
ParsedExpr msg = ParsedExpr.newBuilder().setSourceInfo(SourceInfo.newBuilder().addAllLineOffsets(Arrays.asList(1, 2, 3)).build()).build();
Val obj = reg.nativeToValue(msg);
assertThat(obj).isInstanceOf(ObjectT.class);
ObjectT objVal = (ObjectT) obj;
assertThat(objVal.get(stringOf("source_info")).equal(reg.nativeToValue(msg.getSourceInfo()))).isSameAs(True);
assertThat(objVal.get(stringOf("expr")).equal(reg.nativeToValue(Expr.getDefaultInstance()))).isSameAs(True);
assertThat(objVal.get(stringOf("bad_field"))).matches(Err::isError);
assertThat(objVal.get(IntZero)).matches(Err::isError);
}
Aggregations