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: 582030585
  • Loading branch information
l46kok authored and copybara-github committed Nov 13, 2023
1 parent c36119a commit 01de256
Show file tree
Hide file tree
Showing 24 changed files with 1,048 additions and 0 deletions.
39 changes: 39 additions & 0 deletions common/src/main/java/dev/cel/common/values/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,45 @@ package(
],
)

# 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"],
Expand Down
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);
}
}
42 changes: 42 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,42 @@
// 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();

// TOOD(b/309695452): Add CelEquals method
// TODO: Add a getter for CelType

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 value().isZero();
}

public static DurationValue create(Duration value) {
return new AutoValue_DurationValue(value);
}
}
41 changes: 41 additions & 0 deletions common/src/main/java/dev/cel/common/values/EnumValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// 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;

/**
* EnumValue is a simple CelValue wrapper around Java enums.
*
* <p>Note: CEL-Java currently does not support strongly typed enum. This value class will not be
* used until the said support is added in.
*/
@AutoValue
@Immutable(containerOf = "E")
public abstract class EnumValue<E extends Enum<E>> extends CelValue {

@Override
public abstract Enum<E> value();

@Override
public boolean isZeroValue() {
return false;
}

public static <E extends Enum<E>> EnumValue<E> create(Enum<E> value) {
return new AutoValue_EnumValue<>(value);
}
}
66 changes: 66 additions & 0 deletions common/src/main/java/dev/cel/common/values/IntValue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// 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;

/** IntValue is a simple CelValue wrapper around Java longs. */
@Immutable
public final class IntValue extends CelValue {
private final long value;

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

public long longValue() {
return value;
}

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

public static IntValue create(long value) {
return new IntValue(value);
}

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

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

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

return ((IntValue) o).value == this.value;
}

private IntValue(long value) {
this.value = value;
}
}
Loading

0 comments on commit 01de256

Please sign in to comment.