-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce CelValue. Implement CelValues for primitive types.
PiperOrigin-RevId: 582030585
- Loading branch information
1 parent
c36119a
commit 01de256
Showing
24 changed files
with
1,048 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
36
common/src/main/java/dev/cel/common/values/BytesValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
67
common/src/main/java/dev/cel/common/values/DoubleValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
common/src/main/java/dev/cel/common/values/DurationValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.