Skip to content
Merged
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 @@ -78,6 +78,7 @@
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import org.projectnessie.cel.common.types.TypeT;
import org.projectnessie.cel.common.types.ref.FieldType;
import org.projectnessie.cel.common.types.ref.TypeRegistry;
Expand All @@ -87,11 +88,13 @@ public final class ProtoTypeRegistry implements TypeRegistry {
private static final ProtoTypeRegistry DEFAULT_REGISTRY = newDefaultRegistry();

private final Map<String, org.projectnessie.cel.common.types.ref.Type> revTypeMap;
private final Map<String, FieldType> fieldTypeCache;
private final Db pbdb;

private ProtoTypeRegistry(
Map<String, org.projectnessie.cel.common.types.ref.Type> revTypeMap, Db pbdb) {
this.revTypeMap = revTypeMap;
this.fieldTypeCache = new ConcurrentHashMap<>();
this.pbdb = pbdb;
}

Expand Down Expand Up @@ -197,6 +200,11 @@ public Val enumValue(String enumName) {

@Override
public FieldType findFieldType(String messageType, String fieldName) {
String cacheKey = messageType + '\n' + fieldName;
return fieldTypeCache.computeIfAbsent(cacheKey, key -> loadFieldType(messageType, fieldName));

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.

just double checking:
why do we here care about concurrent access whereas there we dont:

private FieldType findFieldType(String messageType, String fieldName) {
String key = messageType + '\n' + fieldName;
FieldType ft = fieldTypes.get(key);
if (ft != null) {
return ft;
}
ft = env.provider.findFieldType(messageType, fieldName);
if (ft != null) {
fieldTypes.put(key, ft);
}
return ft;
}

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

ProtoTypeRegistry can be shared, while Checker is not.

}

private FieldType loadFieldType(String messageType, String fieldName) {
PbTypeDescription msgType = pbdb.describeType(messageType);
if (msgType == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,11 @@ public Activation parent() {
/** ResolveName implements the Activation interface method. */
@Override
public ResolvedValue resolveName(String name) {
if (!bindings.containsKey(name)) {
return ResolvedValue.ABSENT;
}

Object obj = bindings.get(name);
if (obj == null) {
if (!bindings.containsKey(name)) {
return ResolvedValue.ABSENT;
}
return ResolvedValue.NULL_VALUE;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ Interpretable planCall(Expr expr) {
// Otherwise, generate Interpretable calls specialized by argument count.
// Try to find the specific function by overload id.
Overload fnDef = null;
if (resolvedFunc.overloadId != null && resolvedFunc.overloadId.isEmpty()) {
if (resolvedFunc.overloadId != null && !resolvedFunc.overloadId.isEmpty()) {
fnDef = disp.findOverload(resolvedFunc.overloadId);
}
// If the overload id couldn't resolve the function, try the simple function name.
Expand Down
25 changes: 25 additions & 0 deletions core/src/test/java/org/projectnessie/cel/CELTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,31 @@ void CustomEnvCanSubsetStandardLibrary() {
assertThat(prg.eval(mapOf("resource.name", "buckets/example")).getVal()).isSameAs(False);
}

@Test
void ProgramUsesCheckedOverloadIdForCustomFunctionDispatch() {
Env env =
newEnv(
declarations(
Decls.newVar("value", Decls.Int),
Decls.newFunction(
"is_even",
Decls.newOverload("is_even_int", singletonList(Decls.Int), Decls.Bool))));

AstIssuesTuple astIss = env.compile("is_even(value)");
assertThat(astIss.hasIssues()).isFalse();

Program program =
env.program(
astIss.getAst(),
functions(
Overload.unary(
"is_even_int",
value -> boolOf(((Number) value.value()).longValue() % 2 == 0))));

assertThat(program.eval(mapOf("value", 42L)).getVal()).isSameAs(True);
assertThat(program.eval(mapOf("value", 41L)).getVal()).isSameAs(False);
}

@Test
void HomogeneousAggregateLiterals() {
Env e =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ void resolve() {
assertThat(activation.resolveName("a").value()).isSameAs(True);
}

@Test
void resolveNullAndAbsentFromMapActivation() {
Map<String, Object> map = new HashMap<>();
map.put("nullValue", null);
Activation activation = newActivation(map);

assertThat(activation.resolveName("nullValue")).isSameAs(ResolvedValue.NULL_VALUE);
assertThat(activation.resolveName("absent")).isSameAs(ResolvedValue.ABSENT);
}

@Test
void resolveLazy() {
AtomicReference<Val> v = new AtomicReference<>();
Expand Down
Loading