Skip to content
Closed
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
31 changes: 31 additions & 0 deletions common/src/main/java/dev/cel/common/CelProtoJsonAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@

package dev.cel.common;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.Immutable;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import com.google.protobuf.Empty;
import com.google.protobuf.FieldMask;
import com.google.protobuf.ListValue;
import com.google.protobuf.NullValue;
import com.google.protobuf.Struct;
import com.google.protobuf.Timestamp;
import com.google.protobuf.Value;
import com.google.protobuf.util.Durations;
import com.google.protobuf.util.Timestamps;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
Expand Down Expand Up @@ -113,11 +119,36 @@ public static Value adaptValueToJsonValue(Object value) {
String duration = Durations.toString((Duration) value);
return json.setStringValue(duration).build();
}
if (value instanceof FieldMask) {
String fieldMaskStr = toJsonString((FieldMask) value);
return json.setStringValue(fieldMaskStr).build();
}
if (value instanceof Empty) {
// google.protobuf.Empty is just an empty json map {}
return json.setStructValue(Struct.getDefaultInstance()).build();
}

throw new IllegalArgumentException(
String.format("Value %s cannot be adapted to a JSON Value.", value));
}

/**
* Joins the field mask's paths into a single string with commas.
* This logic is copied from Protobuf's FieldMaskUtil.java, which we
* cannot directly use here due to its dependency to descriptors.
*/
private static String toJsonString(FieldMask fieldMask) {
List<String> paths = new ArrayList<>(fieldMask.getPathsCount());

for (String path : fieldMask.getPathsList()) {
if (!path.isEmpty()) {
paths.add(CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, path));
}
}

return Joiner.on(",").join(paths);
}

/**
* Adapts an iterable to a JSON list value.
*
Expand Down
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 @@ -174,6 +174,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",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public MessageLite adaptValueToWellKnownProto(Object value, WellKnownProto wellK
case TIMESTAMP:
return (Timestamp) value;
default:
throw new IllegalArgumentException("Unexpceted wellKnownProto kind: " + wellKnownProto);
throw new IllegalArgumentException("Unexpected wellKnownProto kind: " + wellKnownProto);
}
}

Expand Down
4 changes: 0 additions & 4 deletions conformance/src/test/java/dev/cel/conformance/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ _TESTS_TO_SKIP = [
"timestamps/timestamp_arithmetic/add_time_to_duration_nanos_negative",

# Skip until fixed.
"wrappers/field_mask/to_json",
"wrappers/empty/to_json",
"wrappers/duration/to_json",
"wrappers/timestamp/to_json",
"fields/qualified_identifier_resolution/map_value_repeat_key_heterogeneous",
# TODO: Add strings.format and strings.quote.
"string_ext/quote",
Expand Down