Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -619,16 +619,12 @@ private static Val exprValueToRefValue(TypeAdapter adapter, dev.cel.expr.ExprVal
}

private static Val exprValueToRefValue(TypeAdapter adapter, ExprValue ev) {
switch (ev.getKindCase()) {
case VALUE:
return valueToRefValue(adapter, ev.getValue());
case ERROR:
return newErr("XXX add details later");
case UNKNOWN:
return unknownOf(ev.getUnknown().getExprs(0));
default:
throw new IllegalArgumentException("unknown ExprValue kind " + ev.getKindCase());
}
return switch (ev.getKindCase()) {
case VALUE -> valueToRefValue(adapter, ev.getValue());
case ERROR -> newErr("XXX add details later");
case UNKNOWN -> unknownOf(ev.getUnknown().getExprs(0));
default -> throw new IllegalArgumentException("unknown ExprValue kind " + ev.getKindCase());
};
}

private static Val valueToRefValue(TypeAdapter adapter, Value v) {
Expand Down
22 changes: 11 additions & 11 deletions core/src/jmh/java/org/projectnessie/cel/CompileBuildBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ public static class CompileState {
public String expression;

String source() {
switch (expression) {
case "simplePredicate":
return "resource == 'projects/p1' && user == 'alice'";
case "deepSelectors":
return "request.auth.claims.email.endsWith('@example.com')"
+ " && request.resource.labels['env'] == 'prod'";
case "macroPipeline":
return "items.filter(i, i.score > 10).map(i, i.name).exists(n, n.startsWith('a'))";
default:
throw new IllegalArgumentException("Unknown compile benchmark expression: " + expression);
}
return switch (expression) {
case "simplePredicate" -> "resource == 'projects/p1' && user == 'alice'";
case "deepSelectors" ->
"request.auth.claims.email.endsWith('@example.com')"
+ " && request.resource.labels['env'] == 'prod'";
case "macroPipeline" ->
"items.filter(i, i.score > 10).map(i, i.name).exists(n, n.startsWith('a'))";
default ->
throw new IllegalArgumentException(
"Unknown compile benchmark expression: " + expression);
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,30 +87,19 @@ public void nativeToValue(NativeValueState state, Blackhole blackhole) {
}

private static Object value(String kind, int size) {
switch (kind) {
case "arrayList":
return list(size);
case "linkedHashSet":
return set(size);
case "objectArray":
return list(size).toArray();
case "stringArray":
return stringArray(size);
case "intArray":
return intArray(size);
case "longArray":
return longArray(size);
case "doubleArray":
return doubleArray(size);
case "mapStringInt":
return mapStringInt(size);
case "mapValVal":
return mapValVal(size);
case "listValue":
return listValue(size);
default:
throw new IllegalArgumentException("Unknown native value kind: " + kind);
}
return switch (kind) {
case "arrayList" -> list(size);
case "linkedHashSet" -> set(size);
case "objectArray" -> list(size).toArray();
case "stringArray" -> stringArray(size);
case "intArray" -> intArray(size);
case "longArray" -> longArray(size);
case "doubleArray" -> doubleArray(size);
case "mapStringInt" -> mapStringInt(size);
case "mapValVal" -> mapValVal(size);
case "listValue" -> listValue(size);
default -> throw new IllegalArgumentException("Unknown native value kind: " + kind);
};
}

private static List<Long> list(int size) {
Expand Down
5 changes: 2 additions & 3 deletions core/src/main/java/org/projectnessie/cel/Env.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,8 @@ public Env extend(List<EnvOption> opts) {
// be immutable. Since it is possible to set the TypeProvider separately
// from the TypeAdapter, the possible configurations which could use a
// TypeRegistry as the base implementation are captured below.
if (this.adapter instanceof TypeRegistry && this.provider instanceof TypeRegistry) {
TypeRegistry adapterReg = (TypeRegistry) this.adapter;
TypeRegistry providerReg = (TypeRegistry) this.provider;
if (this.adapter instanceof TypeRegistry adapterReg
&& this.provider instanceof TypeRegistry providerReg) {
TypeRegistry reg = providerReg.copy();
provider = reg;
// If the adapter and provider are the same object, set the adapter
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/projectnessie/cel/EnvOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,11 @@ static EnvOption abbrevs(String... qualifiedNames) {
*/
static EnvOption types(List<Object> addTypes) {
return e -> {
if (!(e.provider instanceof TypeRegistry)) {
if (!(e.provider instanceof TypeRegistry reg)) {
throw new RuntimeException(
String.format(
"custom types not supported by provider: %s", e.provider.getClass().getName()));
}
TypeRegistry reg = (TypeRegistry) e.provider;
for (Object t : addTypes) {
reg.register(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,10 @@ static final class SemanticAdorner implements Adorner {

@Override
public String getMetadata(Object elem) {
if (!(elem instanceof Expr)) {
if (!(elem instanceof Expr e)) {
return "";
}
StringBuilder result = new StringBuilder();
Expr e = (Expr) elem;
Type t = checks.getTypeMapMap().get(e.getId());
if (t != null) {
result.append("~");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,6 @@ void typeMismatch(Location l, Type expected, Type actual) {
formatCheckedType(actual));
}

public void unknownType(Location l, String info) {
// reportError(l, "unknown type%s", info != null ? " for: " + info : "");
}

static String formatFunction(Type resultType, List<Type> argTypes, boolean isInstance) {
StringBuilder result = new StringBuilder();
formatFunction(result, resultType, argTypes, isInstance);
Expand Down
148 changes: 56 additions & 92 deletions core/src/main/java/org/projectnessie/cel/checker/Types.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,17 @@ public static String formatCheckedType(Type t) {
case kindNull:
return "null";
case kindPrimitive:
switch (t.getPrimitive()) {
case UINT64:
return "uint";
case INT64:
return "int";
case BOOL:
return "bool";
case BYTES:
return "bytes";
case DOUBLE:
return "double";
case STRING:
return "string";
}
// unrecognizes & not-specified - ignore above
return t.getPrimitive().toString().toLowerCase(Locale.ROOT).trim();
return switch (t.getPrimitive()) {
case UINT64 -> "uint";
case INT64 -> "int";
case BOOL -> "bool";
case BYTES -> "bytes";
case DOUBLE -> "double";
case STRING -> "string";
default ->
// unrecognizes & not-specified - ignore above
t.getPrimitive().toString().toLowerCase(Locale.ROOT).trim();
};
case kindWellKnown:
switch (t.getWellKnown()) {
case ANY:
Expand Down Expand Up @@ -196,14 +191,11 @@ private static void formatCheckedTypePrimitive(StringBuilder sb, Type.PrimitiveT
static boolean isDyn(Type t) {
// Note: object type values that are well-known and map to a DYN value in practice
// are sanitized prior to being added to the environment.
switch (kindOf(t)) {
case kindDyn:
return true;
case kindWellKnown:
return t.getWellKnown() == WellKnownType.ANY;
default:
return false;
}
return switch (kindOf(t)) {
case kindDyn -> true;
case kindWellKnown -> t.getWellKnown() == WellKnownType.ANY;
default -> false;
};
}

/** isDynOrError returns true if the input is either an Error, DYN, or well-known ANY message. */
Expand Down Expand Up @@ -356,28 +348,22 @@ static boolean internalIsAssignable(Mapping m, Type t1, Type t2) {
}

// Test for when the types must agree.
switch (kind1) {
return switch (kind1) {
// ERROR, TYPE_PARAM, and DYN handled above.
case kindAbstract:
return internalIsAssignableAbstractType(m, t1.getAbstractType(), t2.getAbstractType());
case kindFunction:
return internalIsAssignableFunction(m, t1.getFunction(), t2.getFunction());
case kindList:
return internalIsAssignable(
m, t1.getListType().getElemType(), t2.getListType().getElemType());
case kindMap:
return internalIsAssignableMap(m, t1.getMapType(), t2.getMapType());
case kindObject:
return t1.getMessageType().equals(t2.getMessageType());
case kindType:
// A type is a type is a type, any additional parameterization of the
// type cannot affect method resolution or assignability.
return true;
case kindWellKnown:
return t1.getWellKnown() == t2.getWellKnown();
default:
return false;
}
case kindAbstract ->
internalIsAssignableAbstractType(m, t1.getAbstractType(), t2.getAbstractType());
case kindFunction -> internalIsAssignableFunction(m, t1.getFunction(), t2.getFunction());
case kindList ->
internalIsAssignable(m, t1.getListType().getElemType(), t2.getListType().getElemType());
case kindMap -> internalIsAssignableMap(m, t1.getMapType(), t2.getMapType());
case kindObject -> t1.getMessageType().equals(t2.getMessageType());
case kindType ->
// A type is a type is a type, any additional parameterization of the
// type cannot affect method resolution or assignability.
true;
case kindWellKnown -> t1.getWellKnown() == t2.getWellKnown();
default -> false;
};
}

/**
Expand Down Expand Up @@ -429,31 +415,22 @@ static boolean internalIsAssignableMap(Mapping m, MapType m1, MapType m2) {

/** internalIsAssignableNull returns true if the type is nullable. */
static boolean internalIsAssignableNull(Type t) {
switch (kindOf(t)) {
case kindAbstract:
case kindObject:
case kindNull:
case kindWellKnown:
case kindWrapper:
return true;
default:
return false;
}
return switch (kindOf(t)) {
case kindAbstract, kindObject, kindNull, kindWellKnown, kindWrapper -> true;
default -> false;
};
}

/**
* internalIsAssignablePrimitive returns true if the target type is the same or if it is a wrapper
* for the primitive type.
*/
static boolean internalIsAssignablePrimitive(PrimitiveType p, Type target) {
switch (kindOf(target)) {
case kindPrimitive:
return p == target.getPrimitive();
case kindWrapper:
return p == target.getWrapper();
default:
return false;
}
return switch (kindOf(target)) {
case kindPrimitive -> p == target.getPrimitive();
case kindWrapper -> p == target.getWrapper();
default -> false;
};
}

/** isAssignable returns an updated type substitution mapping if t1 is assignable to t2. */
Expand All @@ -479,35 +456,22 @@ static Kind kindOf(Type t) {
if (t == null || t.getTypeKindCase() == TypeKindCase.TYPEKIND_NOT_SET) {
return Kind.kindUnknown;
}
switch (t.getTypeKindCase()) {
case ERROR:
return Kind.kindError;
case FUNCTION:
return Kind.kindFunction;
case DYN:
return Kind.kindDyn;
case PRIMITIVE:
return Kind.kindPrimitive;
case WELL_KNOWN:
return Kind.kindWellKnown;
case WRAPPER:
return Kind.kindWrapper;
case NULL:
return Kind.kindNull;
case ABSTRACT_TYPE:
return Kind.kindAbstract;
case TYPE:
return Kind.kindType;
case LIST_TYPE:
return Kind.kindList;
case MAP_TYPE:
return Kind.kindMap;
case MESSAGE_TYPE:
return Kind.kindObject;
case TYPE_PARAM:
return Kind.kindTypeParam;
}
return Kind.kindUnknown;
return switch (t.getTypeKindCase()) {
case ERROR -> Kind.kindError;
case FUNCTION -> Kind.kindFunction;
case DYN -> Kind.kindDyn;
case PRIMITIVE -> Kind.kindPrimitive;
case WELL_KNOWN -> Kind.kindWellKnown;
case WRAPPER -> Kind.kindWrapper;
case NULL -> Kind.kindNull;
case ABSTRACT_TYPE -> Kind.kindAbstract;
case TYPE -> Kind.kindType;
case LIST_TYPE -> Kind.kindList;
case MAP_TYPE -> Kind.kindMap;
case MESSAGE_TYPE -> Kind.kindObject;
case TYPE_PARAM -> Kind.kindTypeParam;
default -> Kind.kindUnknown;
};
}

/** mostGeneral returns the more general of two types which are known to unify. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,7 @@ public String toDisplayString(Source source) {
// sophisticated way, maybe use jline's WCWidth, but that one is also quite rudimentary wrt
// code-blocks (e.g. doesn't know about emojis).
result.append("\n | ");
for (int i = 0; i < location.column(); i++) {
result.append(dot);
}
result.append(String.valueOf(dot).repeat(Math.max(0, location.column())));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these kind of changes really an improvement in terms of readability/performance?

result.append(ind);
}
return result.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,7 @@ void appendFormat(String f, Object... args) {
void doIndent() {
if (lineStart) {
lineStart = false;
for (int i = 0; i < indent; i++) {
buffer.append(" ");
}
buffer.append(" ".repeat(Math.max(0, indent)));
}
}

Expand Down
Loading
Loading