Skip to content

Commit 6a4fd44

Browse files
committed
CelValueConverter cleanups
1 parent 7f93040 commit 6a4fd44

3 files changed

Lines changed: 12 additions & 12 deletions

File tree

common/src/main/java/dev/cel/common/values/CelValueConverter.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.common.collect.ImmutableMap;
2020
import com.google.common.primitives.UnsignedLong;
2121
import com.google.errorprone.annotations.Immutable;
22-
import com.google.protobuf.ByteString;
2322
import dev.cel.common.annotations.Internal;
2423
import java.util.Map;
2524
import java.util.Map.Entry;
@@ -34,7 +33,7 @@
3433
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
3534
@Internal
3635
@Immutable
37-
public class CelValueConverter {
36+
public abstract class CelValueConverter {
3837

3938
/** Adapts a {@link CelValue} to a plain old Java Object. */
4039
public Object fromCelValueToJavaObject(CelValue celValue) {
@@ -77,10 +76,7 @@ public CelValue fromJavaObjectToCelValue(Object value) {
7776
return (CelValue) value;
7877
}
7978

80-
if (value instanceof ByteString) {
81-
// TODO: CelConstant should hold this value instead of adapting it here
82-
return BytesValue.create(CelByteString.of(((ByteString) value).toByteArray()));
83-
} else if (value instanceof Iterable) {
79+
if (value instanceof Iterable) {
8480
return toListValue((Iterable<Object>) value);
8581
} else if (value instanceof Map) {
8682
return toMapValue((Map<Object, Object>) value);
@@ -91,9 +87,6 @@ public CelValue fromJavaObjectToCelValue(Object value) {
9187
.orElse(OptionalValue.EMPTY);
9288
} else if (value instanceof Exception) {
9389
return ErrorValue.create((Exception) value);
94-
} else if (value instanceof com.google.protobuf.NullValue) {
95-
// TODO: CelConstant should hold this value instead of adapting it here
96-
return NullValue.NULL_VALUE;
9790
}
9891

9992
return fromJavaPrimitiveToCelValue(value);
@@ -152,4 +145,6 @@ private MapValue<CelValue, CelValue> toMapValue(Map<Object, Object> map) {
152145

153146
return ImmutableMapValue.create(mapBuilder.buildOrThrow());
154147
}
148+
149+
protected CelValueConverter() {}
155150
}

runtime/src/test/java/dev/cel/runtime/planner/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ java_library(
4747
"//common/values:cel_value_provider",
4848
"//common/values:proto_message_lite_value",
4949
"//common/values:proto_message_lite_value_provider",
50+
"//common/values:proto_message_value",
5051
"//common/values:proto_message_value_provider",
5152
"//compiler",
5253
"//compiler:compiler_builder",

runtime/src/test/java/dev/cel/runtime/planner/ProgramPlannerTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import dev.cel.common.values.ListValue;
4141
import dev.cel.common.values.MapValue;
4242
import dev.cel.common.values.NullValue;
43+
import dev.cel.common.values.ProtoCelValueConverter;
4344
import dev.cel.common.values.ProtoMessageValueProvider;
4445
import dev.cel.common.values.StringValue;
4546
import dev.cel.common.values.TimestampValue;
@@ -105,14 +106,17 @@ public final class ProgramPlannerTest {
105106
.addLibraries(CelOptionalLibrary.INSTANCE, CelExtensions.comprehensions())
106107
.addMessageTypes(TestAllTypes.getDescriptor())
107108
.build();
109+
110+
// Note that the following deps are ordinarily built from top-level builder APIs
108111
private static final CelDescriptorPool DESCRIPTOR_POOL =
109112
DefaultDescriptorPool.create(CelDescriptorUtil.getAllDescriptorsFromFileDescriptor(TestAllTypes.getDescriptor().getFile()));
110-
private static final CelValueConverter CEL_VALUE_CONVERTER = new CelValueConverter();
113+
private static final DynamicProto DYNAMIC_PROTO = DynamicProto.create(DefaultMessageFactory.create(DESCRIPTOR_POOL));
114+
private static final CelValueConverter CEL_VALUE_CONVERTER = ProtoCelValueConverter.newInstance(DESCRIPTOR_POOL,
115+
DYNAMIC_PROTO);
111116
private static final ProgramPlanner PLANNER = ProgramPlanner.newPlanner(
112117
DefaultTypeProvider.create(),
113118
ProtoMessageValueProvider.newInstance(
114-
CEL_OPTIONS,
115-
DynamicProto.create(DefaultMessageFactory.create(DESCRIPTOR_POOL))
119+
CEL_OPTIONS, DYNAMIC_PROTO
116120
),
117121
CEL_VALUE_CONVERTER,
118122
newDispatcher()

0 commit comments

Comments
 (0)