|
1 | | -package dev.cel.runtime; |
2 | | - |
3 | | -import com.google.auto.value.AutoValue; |
4 | | -import com.google.common.collect.ImmutableList; |
5 | | -import com.google.common.collect.ImmutableMap; |
6 | | -import com.google.errorprone.annotations.CanIgnoreReturnValue; |
7 | | -import com.google.errorprone.annotations.CheckReturnValue; |
8 | | -import dev.cel.common.annotations.Internal; |
9 | | - |
10 | | -import java.util.Optional; |
11 | | - |
12 | | -@Internal |
13 | | -@AutoValue |
14 | | -public abstract class DefaultDispatcher { |
15 | | - |
16 | | - abstract ImmutableMap<String, CelValueFunctionBinding> overloads(); |
17 | | - |
18 | | - public Optional<CelValueFunctionBinding> findOverload(String overloadId) { |
19 | | - return Optional.ofNullable(overloads().get(overloadId)); |
20 | | - } |
21 | | - |
22 | | - public static Builder newBuilder() { |
23 | | - return new AutoValue_DefaultDispatcher.Builder(); |
24 | | - } |
25 | | - |
26 | | - @AutoValue.Builder |
27 | | - public abstract static class Builder { |
28 | | - public abstract ImmutableMap.Builder<String, CelValueFunctionBinding> overloadsBuilder(); |
29 | | - |
30 | | - @CanIgnoreReturnValue |
31 | | - public Builder addOverload(CelValueFunctionBinding functionBinding) { |
32 | | - overloadsBuilder().put( |
33 | | - functionBinding.overloadId(), |
34 | | - functionBinding); |
35 | | - return this; |
36 | | - } |
37 | | - |
38 | | - @CanIgnoreReturnValue |
39 | | - public Builder addDynamicDispatchOverload(String functionName, CelValueFunctionOverload definition) { |
40 | | - overloadsBuilder().put( |
41 | | - functionName, CelValueFunctionBinding.from(functionName, ImmutableList.of(), definition) |
42 | | - ); |
43 | | - |
44 | | - return this; |
45 | | - } |
46 | | - |
47 | | - @CheckReturnValue |
48 | | - public abstract DefaultDispatcher build(); |
49 | | - } |
50 | | -} |
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.runtime; |
| 16 | + |
| 17 | +import com.google.auto.value.AutoValue; |
| 18 | +import com.google.common.base.Joiner; |
| 19 | +import com.google.common.collect.ImmutableList; |
| 20 | +import com.google.common.collect.ImmutableMap; |
| 21 | +import com.google.errorprone.annotations.Immutable; |
| 22 | +import javax.annotation.concurrent.ThreadSafe; |
| 23 | +import com.google.errorprone.annotations.concurrent.GuardedBy; |
| 24 | +import dev.cel.common.CelErrorCode; |
| 25 | +import dev.cel.common.annotations.Internal; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.HashMap; |
| 29 | +import java.util.List; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Optional; |
| 32 | + |
| 33 | +/** |
| 34 | + * Default implementation of {@link Dispatcher}. |
| 35 | + * |
| 36 | + * <p>Should be final, do not mock; mocking {@link Dispatcher} instead. |
| 37 | + */ |
| 38 | +@ThreadSafe |
| 39 | +@Internal |
| 40 | +public final class DefaultDispatcher implements Dispatcher, Registrar { |
| 41 | + public static DefaultDispatcher create() { |
| 42 | + return new DefaultDispatcher(); |
| 43 | + } |
| 44 | + |
| 45 | + @GuardedBy("this") |
| 46 | + private final Map<String, ResolvedOverload> overloads = new HashMap<>(); |
| 47 | + |
| 48 | + @Override |
| 49 | + @SuppressWarnings("unchecked") |
| 50 | + public synchronized <T> void add( |
| 51 | + String overloadId, Class<T> argType, final Registrar.UnaryFunction<T> function) { |
| 52 | + overloads.put( |
| 53 | + overloadId, |
| 54 | + ResolvedOverloadImpl.of( |
| 55 | + overloadId, new Class<?>[] {argType}, args -> function.apply((T) args[0]))); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + @SuppressWarnings("unchecked") |
| 60 | + public synchronized <T1, T2> void add( |
| 61 | + String overloadId, |
| 62 | + Class<T1> argType1, |
| 63 | + Class<T2> argType2, |
| 64 | + final Registrar.BinaryFunction<T1, T2> function) { |
| 65 | + overloads.put( |
| 66 | + overloadId, |
| 67 | + ResolvedOverloadImpl.of( |
| 68 | + overloadId, |
| 69 | + new Class<?>[] {argType1, argType2}, |
| 70 | + args -> function.apply((T1) args[0], (T2) args[1]))); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public synchronized void add( |
| 75 | + String overloadId, List<Class<?>> argTypes, Registrar.Function function) { |
| 76 | + overloads.put( |
| 77 | + overloadId, |
| 78 | + ResolvedOverloadImpl.of(overloadId, argTypes.toArray(new Class<?>[0]), function)); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public synchronized Optional<ResolvedOverload> findOverload( |
| 83 | + String functionName, List<String> overloadIds, Object[] args) throws CelEvaluationException { |
| 84 | + return DefaultDispatcher.findOverload(functionName, overloadIds, overloads, args); |
| 85 | + } |
| 86 | + |
| 87 | + /** Finds the overload that matches the given function name, overload IDs, and arguments. */ |
| 88 | + public static Optional<ResolvedOverload> findOverload( |
| 89 | + String functionName, |
| 90 | + List<String> overloadIds, |
| 91 | + Map<String, ? extends ResolvedOverload> overloads, |
| 92 | + Object[] args) |
| 93 | + throws CelEvaluationException { |
| 94 | + int matchingOverloadCount = 0; |
| 95 | + ResolvedOverload match = null; |
| 96 | + List<String> candidates = null; |
| 97 | + for (String overloadId : overloadIds) { |
| 98 | + ResolvedOverload overload = overloads.get(overloadId); |
| 99 | + // If the overload is null, it means that the function was not registered; however, it is |
| 100 | + // possible that the overload refers to a late-bound function. |
| 101 | + if (overload != null && overload.canHandle(args)) { |
| 102 | + if (++matchingOverloadCount > 1) { |
| 103 | + if (candidates == null) { |
| 104 | + candidates = new ArrayList<>(); |
| 105 | + candidates.add(match.getOverloadId()); |
| 106 | + } |
| 107 | + candidates.add(overloadId); |
| 108 | + } |
| 109 | + match = overload; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + if (matchingOverloadCount > 1) { |
| 114 | + throw CelEvaluationExceptionBuilder.newBuilder( |
| 115 | + "Ambiguous overloads for function '%s'. Matching candidates: %s", |
| 116 | + functionName, Joiner.on(", ").join(candidates)) |
| 117 | + .setErrorCode(CelErrorCode.AMBIGUOUS_OVERLOAD) |
| 118 | + .build(); |
| 119 | + } |
| 120 | + return Optional.ofNullable(match); |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public synchronized Dispatcher.ImmutableCopy immutableCopy() { |
| 125 | + return new ImmutableCopy(overloads); |
| 126 | + } |
| 127 | + |
| 128 | + @Immutable |
| 129 | + private static final class ImmutableCopy implements Dispatcher.ImmutableCopy { |
| 130 | + private final ImmutableMap<String, ResolvedOverload> overloads; |
| 131 | + |
| 132 | + private ImmutableCopy(Map<String, ResolvedOverload> overloads) { |
| 133 | + this.overloads = ImmutableMap.copyOf(overloads); |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Optional<ResolvedOverload> findOverload( |
| 138 | + String functionName, List<String> overloadIds, Object[] args) |
| 139 | + throws CelEvaluationException { |
| 140 | + return DefaultDispatcher.findOverload(functionName, overloadIds, overloads, args); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public Dispatcher.ImmutableCopy immutableCopy() { |
| 145 | + return this; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private DefaultDispatcher() {} |
| 150 | + |
| 151 | + @AutoValue |
| 152 | + @Immutable |
| 153 | + abstract static class ResolvedOverloadImpl implements ResolvedOverload { |
| 154 | + /** The overload id of the function. */ |
| 155 | + @Override |
| 156 | + public abstract String getOverloadId(); |
| 157 | + |
| 158 | + /** The types of the function parameters. */ |
| 159 | + @Override |
| 160 | + public abstract ImmutableList<Class<?>> getParameterTypes(); |
| 161 | + |
| 162 | + /** The function definition. */ |
| 163 | + @Override |
| 164 | + public abstract FunctionOverload getDefinition(); |
| 165 | + |
| 166 | + static ResolvedOverload of( |
| 167 | + String overloadId, Class<?>[] parameterTypes, FunctionOverload definition) { |
| 168 | + return of(overloadId, ImmutableList.copyOf(parameterTypes), definition); |
| 169 | + } |
| 170 | + |
| 171 | + static ResolvedOverload of( |
| 172 | + String overloadId, ImmutableList<Class<?>> parameterTypes, FunctionOverload definition) { |
| 173 | + return new AutoValue_DefaultDispatcher_ResolvedOverloadImpl( |
| 174 | + overloadId, parameterTypes, definition); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments