From 6e37065821b5116f47ae7f7fd5a458b212e1d94b Mon Sep 17 00:00:00 2001 From: Sokwhan Huh Date: Tue, 3 Jun 2025 15:26:02 -0700 Subject: [PATCH] Remove deprecated builder methods for accepting unknown attribute patterns PiperOrigin-RevId: 766844913 --- .../cel/runtime/async/AsyncProgramImpl.java | 10 -- .../runtime/async/CelAsyncRuntimeBuilder.java | 23 --- .../runtime/async/CelAsyncRuntimeImpl.java | 35 +---- .../async/CelAsyncRuntimeImplTest.java | 132 +++++++++--------- 4 files changed, 71 insertions(+), 129 deletions(-) diff --git a/runtime/src/main/java/dev/cel/runtime/async/AsyncProgramImpl.java b/runtime/src/main/java/dev/cel/runtime/async/AsyncProgramImpl.java index c4e36204c..245dba691 100644 --- a/runtime/src/main/java/dev/cel/runtime/async/AsyncProgramImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/async/AsyncProgramImpl.java @@ -29,7 +29,6 @@ import com.google.common.util.concurrent.ListeningExecutorService; import javax.annotation.concurrent.ThreadSafe; import dev.cel.runtime.CelAttribute; -import dev.cel.runtime.CelAttributePattern; import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelRuntime.Program; import dev.cel.runtime.CelUnknownSet; @@ -60,17 +59,14 @@ final class AsyncProgramImpl implements CelAsyncRuntime.AsyncProgram { private final UnknownContext startingUnknownContext; private final Program program; private final ListeningExecutorService executor; - private final ImmutableMap resolvers; AsyncProgramImpl( Program program, ListeningExecutorService executor, - ImmutableMap resolvers, int maxEvaluateIterations, UnknownContext startingUnknownContext) { this.program = program; this.executor = executor; - this.resolvers = resolvers; this.maxEvaluateIterations = maxEvaluateIterations; // The following is populated from CelAsyncRuntime. The impl is immutable, thus safe to reuse as // a starting context. @@ -86,12 +82,6 @@ private Optional lookupResolver( } } - for (Map.Entry entry : - resolvers.entrySet()) { - if (entry.getKey().isPartialMatch(attribute)) { - return Optional.of(entry.getValue()); - } - } return Optional.empty(); } diff --git a/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeBuilder.java b/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeBuilder.java index 5ebe7363e..23400fcf0 100644 --- a/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeBuilder.java +++ b/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeBuilder.java @@ -14,9 +14,7 @@ package dev.cel.runtime.async; import com.google.errorprone.annotations.CanIgnoreReturnValue; -import dev.cel.runtime.CelAttributePattern; import dev.cel.runtime.CelRuntime; -import dev.cel.runtime.async.CelAsyncRuntime.AsyncProgram; import java.util.concurrent.ExecutorService; /** Builder interface for {@link CelAsyncRuntime}. */ @@ -27,27 +25,6 @@ public interface CelAsyncRuntimeBuilder { @CanIgnoreReturnValue CelAsyncRuntimeBuilder setRuntime(CelRuntime runtime); - /** - * Add attributes that are declared as Unknown, without any resolver. - * - * @deprecated Use {@link AsyncProgram#evaluateToCompletion(CelResolvableAttributePattern...)} - * instead to propagate the unknown attributes along with the resolvers into the program. - */ - @CanIgnoreReturnValue - @Deprecated - CelAsyncRuntimeBuilder addUnknownAttributePatterns(CelAttributePattern... attributes); - - /** - * Marks an attribute pattern as unknown and associates a resolver with it. - * - * @deprecated Use {@link AsyncProgram#evaluateToCompletion(CelResolvableAttributePattern...)} - * instead to propagate the unknown attributes along with the resolvers into the program. - */ - @CanIgnoreReturnValue - @Deprecated - CelAsyncRuntimeBuilder addResolvableAttributePattern( - CelAttributePattern attribute, CelUnknownAttributeValueResolver resolver); - /** * Set the maximum number of allowed evaluation passes. * diff --git a/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeImpl.java b/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeImpl.java index e39c2f9c7..902d9d7c6 100644 --- a/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeImpl.java +++ b/runtime/src/main/java/dev/cel/runtime/async/CelAsyncRuntimeImpl.java @@ -15,13 +15,11 @@ package dev.cel.runtime.async; import com.google.common.base.Preconditions; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableList; import com.google.common.util.concurrent.ListeningExecutorService; import com.google.common.util.concurrent.MoreExecutors; import javax.annotation.concurrent.ThreadSafe; import dev.cel.common.CelAbstractSyntaxTree; -import dev.cel.runtime.CelAttributePattern; import dev.cel.runtime.CelEvaluationException; import dev.cel.runtime.CelRuntime; import dev.cel.runtime.CelRuntimeFactory; @@ -32,23 +30,16 @@ /** Default {@link CelAsyncRuntime} runtime implementation. See {@link AsyncProgramImpl}. */ @ThreadSafe final class CelAsyncRuntimeImpl implements CelAsyncRuntime { - private final ImmutableMap - unknownAttributeResolvers; - private final ImmutableSet unknownAttributePatterns; private final CelRuntime runtime; private final ListeningExecutorService executorService; private final ThreadSafeCelVariableResolver variableResolver; private final int maxEvaluateIterations; private CelAsyncRuntimeImpl( - ImmutableMap unknownAttributeResolvers, - ImmutableSet unknownAttributePatterns, ThreadSafeCelVariableResolver variableResolver, CelRuntime runtime, ListeningExecutorService executorService, int maxEvaluateIterations) { - this.unknownAttributeResolvers = unknownAttributeResolvers; - this.unknownAttributePatterns = unknownAttributePatterns; this.variableResolver = variableResolver; this.runtime = runtime; this.executorService = executorService; @@ -56,7 +47,7 @@ private CelAsyncRuntimeImpl( } private UnknownContext newAsyncContext() { - return UnknownContext.create(variableResolver, unknownAttributePatterns); + return UnknownContext.create(variableResolver, ImmutableList.of()); } @Override @@ -64,7 +55,6 @@ public AsyncProgram createProgram(CelAbstractSyntaxTree ast) throws CelEvaluatio return new AsyncProgramImpl( runtime.createProgram(ast), executorService, - unknownAttributeResolvers, maxEvaluateIterations, newAsyncContext()); } @@ -76,17 +66,12 @@ static Builder newBuilder() { /** {@link CelAsyncRuntimeBuilder} implementation for {@link CelAsyncRuntimeImpl}. */ private static final class Builder implements CelAsyncRuntimeBuilder { private CelRuntime runtime; - private final ImmutableSet.Builder unknownAttributePatterns; - private final ImmutableMap.Builder - unknownAttributeResolvers; private ListeningExecutorService executorService; private Optional variableResolver; private int maxEvaluateIterations; private Builder() { runtime = CelRuntimeFactory.standardCelRuntimeBuilder().build(); - unknownAttributeResolvers = ImmutableMap.builder(); - unknownAttributePatterns = ImmutableSet.builder(); variableResolver = Optional.empty(); maxEvaluateIterations = DEFAULT_MAX_EVALUATE_ITERATIONS; } @@ -97,20 +82,6 @@ public Builder setRuntime(CelRuntime runtime) { return this; } - @Override - public Builder addUnknownAttributePatterns(CelAttributePattern... attributes) { - unknownAttributePatterns.add(attributes); - return this; - } - - @Override - public Builder addResolvableAttributePattern( - CelAttributePattern attribute, CelUnknownAttributeValueResolver resolver) { - unknownAttributeResolvers.put(attribute, resolver); - unknownAttributePatterns.add(attribute); - return this; - } - @Override public Builder setMaxEvaluateIterations(int n) { Preconditions.checkArgument(n > 0, "maxEvaluateIterations must be positive"); @@ -134,8 +105,6 @@ public Builder setExecutorService(ExecutorService executorService) { public CelAsyncRuntime build() { Preconditions.checkNotNull(executorService, "executorService must be specified."); return new CelAsyncRuntimeImpl( - unknownAttributeResolvers.buildOrThrow(), - unknownAttributePatterns.build(), variableResolver.orElse((unused) -> Optional.empty()), runtime, executorService, diff --git a/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java b/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java index 744f41ee1..2597dc97f 100644 --- a/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java +++ b/runtime/src/test/java/dev/cel/runtime/async/CelAsyncRuntimeImplTest.java @@ -74,12 +74,6 @@ public void asyncProgram_basicUnknownResolution() throws Exception { CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), resolveName) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) .setExecutorService(newDirectExecutorService()) .build(); @@ -93,7 +87,14 @@ public void asyncProgram_basicUnknownResolution() throws Exception { AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), resolveName), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) + ); Object result = future.get(2, SECONDS); // Assert @@ -174,15 +175,6 @@ public void asyncProgram_honorsCancellation() throws Exception { CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), - CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var1)) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), - CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var2)) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), - CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var3)) .setExecutorService(Executors.newSingleThreadExecutor()) .build(); @@ -192,7 +184,17 @@ public void asyncProgram_honorsCancellation() throws Exception { AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), + CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var1)), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), + CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var2)), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), + CelUnknownAttributeValueResolver.fromAsyncResolver((attr) -> var3)) + ); var1.set("first"); future.cancel(true); assertThrows(CancellationException.class, () -> future.get(1, SECONDS)); @@ -232,15 +234,6 @@ public void asyncProgram_concurrency( CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), - resolverFactory.get("first")) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), - resolverFactory.get("second")) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), - resolverFactory.get("third")) .setExecutorService(Executors.newFixedThreadPool(3)) .build(); @@ -250,7 +243,17 @@ public void asyncProgram_concurrency( AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), + resolverFactory.get("first")), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), + resolverFactory.get("second")), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), + resolverFactory.get("third")) + ); // Total wait is 2 times the worker delay. This is a little conservative for the size of the // threadpool executor above, but should prevent flakes. @@ -284,12 +287,6 @@ public void asyncProgram_elementResolver() throws Exception { CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributeParser.parsePattern("com.google.listVar[0]"), resolver) - .addResolvableAttributePattern( - CelAttributeParser.parsePattern("com.google.listVar[1]"), resolver) - .addResolvableAttributePattern( - CelAttributeParser.parsePattern("com.google.listVar[2]"), resolver) .setExecutorService(Executors.newSingleThreadExecutor()) .build(); @@ -299,7 +296,14 @@ public void asyncProgram_elementResolver() throws Exception { AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributeParser.parsePattern("com.google.listVar[0]"), resolver), + CelResolvableAttributePattern.of( + CelAttributeParser.parsePattern("com.google.listVar[1]"), resolver), + CelResolvableAttributePattern.of( + CelAttributeParser.parsePattern("com.google.listVar[2]"), resolver) + ); Object result = future.get(1, SECONDS); // Assert @@ -331,16 +335,6 @@ public void asyncProgram_thrownExceptionPropagatesImmediately() throws Exception CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), - CelUnknownAttributeValueResolver.fromResolver( - (attr) -> { - throw new IllegalArgumentException("example_var2"); - })) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) .setExecutorService(newDirectExecutorService()) .build(); @@ -354,7 +348,17 @@ public void asyncProgram_thrownExceptionPropagatesImmediately() throws Exception AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), + CelUnknownAttributeValueResolver.fromResolver( + (attr) -> { + throw new IllegalArgumentException("example_var2"); + })), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName)); // Assert ExecutionException e = assertThrows(ExecutionException.class, () -> future.get(2, SECONDS)); @@ -386,14 +390,6 @@ public void asyncProgram_returnedExceptionPropagatesToEvaluator() throws Excepti CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), - CelUnknownAttributeValueResolver.fromResolver( - (attr) -> new IllegalStateException("example_var2"))) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) .setExecutorService(newDirectExecutorService()) .build(); @@ -407,7 +403,16 @@ public void asyncProgram_returnedExceptionPropagatesToEvaluator() throws Excepti AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), + CelUnknownAttributeValueResolver.fromResolver( + (attr) -> new IllegalStateException("example_var2"))), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) + ); // Assert ExecutionException e = assertThrows(ExecutionException.class, () -> future.get(2, SECONDS)); @@ -440,14 +445,6 @@ public void asyncProgram_returnedExceptionPropagatesToEvaluatorIsPruneable() thr CelAsyncRuntime asyncRuntime = CelAsyncRuntimeFactory.defaultAsyncRuntime() .setRuntime(cel) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), - CelUnknownAttributeValueResolver.fromResolver( - (attr) -> new IllegalStateException("example"))) - .addResolvableAttributePattern( - CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) .setExecutorService(newDirectExecutorService()) .build(); @@ -458,7 +455,16 @@ public void asyncProgram_returnedExceptionPropagatesToEvaluatorIsPruneable() thr AsyncProgram program = asyncRuntime.createProgram(ast); // Act - ListenableFuture future = program.evaluateToCompletion(); + ListenableFuture future = program.evaluateToCompletion( + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var1"), resolveName), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var2"), + CelUnknownAttributeValueResolver.fromResolver( + (attr) -> new IllegalStateException("example"))), + CelResolvableAttributePattern.of( + CelAttributePattern.fromQualifiedIdentifier("com.google.var3"), resolveName) + ); Object result = future.get(2, SECONDS); // Assert