Skip to content

Commit

Permalink
Introduce CelValue. Implement CelValues for primitive types.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 576690957
  • Loading branch information
l46kok authored and copybara-github committed Nov 2, 2023
1 parent 22a2c8e commit 6b839a4
Show file tree
Hide file tree
Showing 26 changed files with 1,275 additions and 0 deletions.
58 changes: 58 additions & 0 deletions common/src/main/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package(
default_applicable_licenses = [
"//:license",
],
default_visibility = [
"//common/values:__pkg__",
],
)

# keep sorted
CEL_VALUES_SOURCES = [
"BoolValue.java",
"BytesValue.java",
"DoubleValue.java",
"DurationValue.java",
"EnumValue.java",
"IntValue.java",
"NullValue.java",
"StringValue.java",
"TimestampValue.java",
"UintValue.java",
]

java_library(
name = "cel_value",
srcs = ["CelValue.java"],
tags = [
],
deps = [
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "values",
srcs = CEL_VALUES_SOURCES,
tags = [
],
deps = [
":cel_byte_string",
":cel_value",
"//:auto_value",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "cel_byte_string",
srcs = ["CelByteString.java"],
tags = [
],
deps = [
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)
36 changes: 36 additions & 0 deletions common/src/main/java/dev/cel/common/values/BoolValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;

/** BoolValue is a simple CelValue wrapper around Java booleans. */
@AutoValue
@Immutable
public abstract class BoolValue extends CelValue {

@Override
public abstract Boolean value();

@Override
public boolean isZeroValue() {
return !value();
}

public static BoolValue create(Boolean value) {
return new AutoValue_BoolValue(value);
}
}
36 changes: 36 additions & 0 deletions common/src/main/java/dev/cel/common/values/BytesValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;

/** BytesValue is a simple CelValue wrapper around CelByteString (immutable byte string). */
@AutoValue
@Immutable
public abstract class BytesValue extends CelValue {

@Override
public abstract CelByteString value();

@Override
public boolean isZeroValue() {
return value().isEmpty();
}

public static BytesValue create(CelByteString value) {
return new AutoValue_BytesValue(value);
}
}
93 changes: 93 additions & 0 deletions common/src/main/java/dev/cel/common/values/CelByteString.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.Immutable;
import java.util.Arrays;

/** CelByteString is an immutable sequence of a byte array. */
@Immutable
@SuppressWarnings("Immutable") // We make defensive copies on the byte array.
public final class CelByteString {
private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
public static final CelByteString EMPTY = new CelByteString(EMPTY_BYTE_ARRAY);

private final byte[] data;

private volatile int hash = 0;

public static CelByteString of(byte[] buffer) {
Preconditions.checkNotNull(buffer);
if (buffer.length == 0) {
return EMPTY;
}
return new CelByteString(buffer);
}

public int size() {
return data.length;
}

public boolean isEmpty() {
return data.length == 0;
}

public byte[] toByteArray() {
int size = size();
if (size == 0) {
return EMPTY_BYTE_ARRAY;
}

return Arrays.copyOf(data, size);
}

private CelByteString(byte[] buffer) {
data = Arrays.copyOf(buffer, buffer.length);
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof CelByteString)) {
return false;
}

return Arrays.equals(data, ((CelByteString) o).data);
}

/**
* Note that we do not use Arrays.hashCode directly due to its implementation using 31 as an odd
* prime, which is outdated and is more prone to hash collisions. This code is very similar to
* what AutoValue generates.
*/
@Override
public int hashCode() {
if (hash == 0) {
int h = 1;
h *= 1000003;
h ^= Arrays.hashCode(data);
if (h == 0) {
h = 1;
}
hash = h;
}

return hash;
}
}
39 changes: 39 additions & 0 deletions common/src/main/java/dev/cel/common/values/CelValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;

/**
* A representation of a CEL value for the runtime. Clients should never directly extend from
* CelValue. Clients may extend some subclasses of CelValue, such as StructValue if it has been
* explicitly designed and documented for extension.
*/
@Immutable
@Internal
public abstract class CelValue {

/**
* The underlying value. This is typically the Java native value or a derived instance of CelValue
* (ex: an element in lists or key/value pair in maps).
*/
public abstract Object value();

/** Returns true if the {@link #value()} is a zero value for its type. */
public abstract boolean isZeroValue();

public CelValue() {}
}
67 changes: 67 additions & 0 deletions common/src/main/java/dev/cel/common/values/DoubleValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.errorprone.annotations.Immutable;

/** DoubleValue is a simple CelValue wrapper around Java doubles. */
@Immutable
public final class DoubleValue extends CelValue {
private final double value;

@Override
public Double value() {
return value;
}

public double doubleValue() {
return value;
}

@Override
public boolean isZeroValue() {
return value() == 0;
}

public static DoubleValue create(double value) {
return new DoubleValue(value);
}

@Override
public int hashCode() {
int h = 1;
h *= 1000003;
h ^= Double.hashCode(value);
return h;
}

@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}

if (!(o instanceof DoubleValue)) {
return false;
}

return Double.doubleToLongBits(((DoubleValue) o).doubleValue())
== Double.doubleToLongBits(this.doubleValue());
}

private DoubleValue(double value) {
this.value = value;
}
}
37 changes: 37 additions & 0 deletions common/src/main/java/dev/cel/common/values/DurationValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright 2023 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.common.values;

import com.google.auto.value.AutoValue;
import com.google.errorprone.annotations.Immutable;
import java.time.Duration;

/** DurationValue is a simple CelValue wrapper around {@link java.time.Duration} */
@AutoValue
@Immutable
public abstract class DurationValue extends CelValue {

@Override
public abstract Duration value();

@Override
public boolean isZeroValue() {
return Duration.ZERO.equals(value());
}

public static DurationValue create(Duration value) {
return new AutoValue_DurationValue(value);
}
}
Loading

0 comments on commit 6b839a4

Please sign in to comment.