Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 567418443
  • Loading branch information
jcking authored and copybara-github committed Sep 21, 2023
1 parent 1520cfe commit 480287f
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion common/src/main/java/dev/cel/common/internal/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.primitives.Ints;
import com.google.common.primitives.UnsignedInteger;
import com.google.common.primitives.UnsignedInts;
import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.CheckReturnValue;
Expand Down Expand Up @@ -55,6 +56,7 @@
import dev.cel.common.CelRuntimeException;
import dev.cel.common.annotations.Internal;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -477,6 +479,11 @@ public Optional<Message> adaptValueToProto(Object value, String protoTypeName) {
return null;
}

private static final long JSON_MAX_INT_VALUE = (1L << 53) - 1;
private static final long JSON_MIN_INT_VALUE = -JSON_MAX_INT_VALUE;
private static final UnsignedLong JSON_MAX_UINT_VALUE =
UnsignedLong.fromLongBits(JSON_MAX_INT_VALUE);

private @Nullable Value adaptValueToJsonValue(Object value) {
Value.Builder json = Value.newBuilder();
if (value == null || value instanceof NullValue) {
Expand All @@ -485,9 +492,31 @@ public Optional<Message> adaptValueToProto(Object value, String protoTypeName) {
if (value instanceof Boolean) {
return json.setBoolValue((Boolean) value).build();
}
if (value instanceof Number) {
if (value instanceof Integer || value instanceof Long) {
long longValue = ((Number) value).longValue();
if (longValue < JSON_MIN_INT_VALUE || longValue > JSON_MAX_INT_VALUE) {
return json.setStringValue(Long.toString(longValue)).build();
}
return json.setNumberValue((double) longValue).build();
}
if (value instanceof UnsignedInteger) {
// Promote UnsignedInteger to UnsignedLong.
value = UnsignedLong.fromLongBits(((Number) value).longValue());
}
if (value instanceof UnsignedLong) {
if (((UnsignedLong) value).compareTo(JSON_MAX_UINT_VALUE) > 0) {
return json.setStringValue(((UnsignedLong) value).toString()).build();
}
return json.setNumberValue((double) ((UnsignedLong) value).longValue()).build();
}
if (value instanceof Float || value instanceof Double) {
return json.setNumberValue(((Number) value).doubleValue()).build();
}
if (value instanceof ByteString) {
return json.setStringValue(
Base64.getEncoder().encodeToString(((ByteString) value).toByteArray()))
.build();
}
if (value instanceof String) {
return json.setStringValue((String) value).build();
}
Expand Down

0 comments on commit 480287f

Please sign in to comment.