use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class CheckerTest method check.
@ParameterizedTest
@MethodSource("checkTestCases")
void check(TestCase tc) {
Assumptions.assumeTrue(tc.disabled == null, tc.disabled);
Source src = newTextSource(tc.i);
ParseResult parsed = Parser.parseAllMacros(src);
assertThat(parsed.getErrors().getErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isEmpty();
TypeRegistry reg = ProtoTypeRegistry.newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance(), com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes.getDefaultInstance());
Container cont = Container.newContainer(Container.name(tc.container));
CheckerEnv env = newStandardCheckerEnv(cont, reg);
if (tc.disableStdEnv) {
env = newCheckerEnv(cont, reg);
}
if (tc.homogeneousAggregateLiterals) {
env.enableDynamicAggregateLiterals(false);
}
if (tc.env != null) {
if (tc.env.idents != null) {
for (Decl ident : tc.env.idents) {
env.add(ident);
}
}
if (tc.env.functions != null) {
for (Decl fn : tc.env.functions) {
env.add(fn);
}
}
}
CheckResult checkResult = Checker.Check(parsed, src, env);
if (checkResult.hasErrors()) {
String errorString = checkResult.getErrors().toDisplayString();
if (tc.error != null) {
assertThat(errorString).isEqualTo(tc.error);
} else {
fail(String.format("Unexpected type-check errors: %s", errorString));
}
} else if (tc.error != null) {
assertThat(tc.error).withFailMessage(String.format("Expected error not thrown: %s", tc.error)).isNull();
}
Type actual = checkResult.getCheckedExpr().getTypeMapMap().get(parsed.getExpr().getId());
if (tc.error == null) {
if (actual == null || !actual.equals(tc.type)) {
fail(String.format("Type Error: '%s' vs expected '%s'", actual, tc.type));
}
}
if (tc.r != null) {
String actualStr = print(checkResult.getCheckedExpr().getExpr(), checkResult.getCheckedExpr());
String actualCmp = actualStr.replaceAll("[ \n\t]", "");
String rCmp = tc.r.replaceAll("[ \n\t]", "");
assertThat(actualCmp).isEqualTo(rCmp);
}
}
use of com.google.api.expr.v1alpha1.Type in project cel-java by projectnessie.
the class AstPruner method maybeCreateLiteral.
Expr maybeCreateLiteral(long id, Val v) {
Type t = v.type();
switch(t.typeEnum()) {
case Bool:
return createLiteral(id, Constant.newBuilder().setBoolValue((Boolean) v.value()).build());
case Int:
return createLiteral(id, Constant.newBuilder().setInt64Value(((Number) v.value()).longValue()).build());
case Uint:
return createLiteral(id, Constant.newBuilder().setUint64Value(((Number) v.value()).longValue()).build());
case String:
return createLiteral(id, Constant.newBuilder().setStringValue(v.value().toString()).build());
case Double:
return createLiteral(id, Constant.newBuilder().setDoubleValue(((Number) v.value()).doubleValue()).build());
case Bytes:
return createLiteral(id, Constant.newBuilder().setBytesValue(ByteString.copyFrom((byte[]) v.value())).build());
case Null:
return createLiteral(id, Constant.newBuilder().setNullValue(NullValue.NULL_VALUE).build());
}
// Attempt to build a list literal.
if (v instanceof Lister) {
Lister list = (Lister) v;
int sz = (int) list.size().intValue();
List<Expr> elemExprs = new ArrayList<>(sz);
for (int i = 0; i < sz; i++) {
Val elem = list.get(intOf(i));
if (isUnknownOrError(elem)) {
return null;
}
Expr elemExpr = maybeCreateLiteral(nextID(), elem);
if (elemExpr == null) {
return null;
}
elemExprs.add(elemExpr);
}
return Expr.newBuilder().setId(id).setListExpr(CreateList.newBuilder().addAllElements(elemExprs).build()).build();
}
// Create a map literal if possible.
if (v instanceof Mapper) {
Mapper mp = (Mapper) v;
IteratorT it = mp.iterator();
List<Entry> entries = new ArrayList<>((int) mp.size().intValue());
while (it.hasNext() == True) {
Val key = it.next();
Val val = mp.get(key);
if (isUnknownOrError(key) || isUnknownOrError(val)) {
return null;
}
Expr keyExpr = maybeCreateLiteral(nextID(), key);
if (keyExpr == null) {
return null;
}
Expr valExpr = maybeCreateLiteral(nextID(), val);
if (valExpr == null) {
return null;
}
Entry entry = Entry.newBuilder().setId(nextID()).setMapKey(keyExpr).setValue(valExpr).build();
entries.add(entry);
}
return Expr.newBuilder().setId(id).setStructExpr(CreateStruct.newBuilder().addAllEntries(entries)).build();
}
// the enumeration the fields for a given message.
return null;
}
use of com.google.api.expr.v1alpha1.Type 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.Type 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.Type in project cel-java by projectnessie.
the class JacksonTypeDescriptionTest method checkListType.
private void checkListType(JacksonRegistry reg, String prop, Class<?> valueClass, com.google.api.expr.v1alpha1.Type valueType) {
JacksonFieldType ft = (JacksonFieldType) reg.findFieldType(CollectionsObject.class.getName(), prop);
assertThat(ft).isNotNull();
JavaType javaType = ft.propertyWriter().getType();
assertThat(javaType).extracting(JavaType::isCollectionLikeType).isEqualTo(true);
assertThat(javaType.getContentType()).extracting(JavaType::getRawClass).isSameAs(valueClass);
assertThat(ft.type).extracting(com.google.api.expr.v1alpha1.Type::getListType).extracting(ListType::getElemType).isSameAs(valueType);
}
Aggregations