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
1 change: 1 addition & 0 deletions common/src/main/java/dev/cel/common/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ java_library(
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
"@maven//:com_google_protobuf_protobuf_java_util",
"@maven//:org_jspecify_jspecify",
],
)
Expand Down
11 changes: 11 additions & 0 deletions common/src/main/java/dev/cel/common/internal/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
import com.google.protobuf.UInt32Value;
import com.google.protobuf.UInt64Value;
import com.google.protobuf.Value;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import dev.cel.common.CelErrorCode;
import dev.cel.common.CelRuntimeException;
import dev.cel.common.annotations.Internal;
Expand Down Expand Up @@ -475,6 +477,15 @@ public Optional<Message> adaptValueToProto(Object value, String protoTypeName) {
return json.setListValue(listValue).build();
}
}
if (value instanceof Timestamp) {
// CEL follows the proto3 to JSON conversion which formats as an RFC 3339 encoded JSON string.
String ts = Timestamps.toString((Timestamp) value);
return json.setStringValue(ts).build();
}
if (value instanceof Duration) {
String duration = Durations.toString((Duration) value);
return json.setStringValue(duration).build();
}
return null;
}

Expand Down
12 changes: 12 additions & 0 deletions runtime/src/test/resources/jsonConversions.baseline
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Source: google.protobuf.Struct { fields: {'timestamp': ts, 'duration': du } }
declare ts {
value google.protobuf.Timestamp
}
declare du {
value google.protobuf.Duration
}
=====>
bindings: {ts=seconds: 100
, du=nanos: 200000000
}
result: {timestamp=1970-01-01T00:01:40Z, duration=0.200s}
10 changes: 10 additions & 0 deletions testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1725,6 +1725,16 @@ public void jsonValueTypes() throws Exception {
runTest(Activation.of("x", xStruct));
}

@Test
public void jsonConversions() throws Exception {
declareVariable("ts", CelTypes.TIMESTAMP);
declareVariable("du", CelTypes.DURATION);
source = "google.protobuf.Struct { fields: {'timestamp': ts, 'duration': du } }";
runTest(
Activation.copyOf(
ImmutableMap.of("ts", Timestamps.fromSeconds(100), "du", Durations.fromMillis(200))));
}

@Test
public void typeComparisons() throws Exception {
container = TestAllTypes.getDescriptor().getFile().getPackage();
Expand Down