Skip to content

Commit 1d38ec6

Browse files
l46kokcopybara-github
authored andcommitted
Implement OpaqueValue, ErrorValue
PiperOrigin-RevId: 584658086
1 parent f645749 commit 1d38ec6

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

common/src/main/java/dev/cel/common/values/BUILD.bazel

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ CEL_VALUES_SOURCES = [
1414
"DoubleValue.java",
1515
"DurationValue.java",
1616
"EnumValue.java",
17+
"ErrorValue.java",
1718
"ImmutableListValue.java",
1819
"ImmutableMapValue.java",
1920
"IntValue.java",
2021
"ListValue.java",
2122
"MapValue.java",
2223
"NullValue.java",
24+
"OpaqueValue.java",
2325
"OptionalValue.java",
2426
"StringValue.java",
2527
"StructValue.java",
@@ -48,6 +50,7 @@ java_library(
4850
":cel_byte_string",
4951
":cel_value",
5052
"//:auto_value",
53+
"//common/annotations",
5154
"//common/types:type_providers",
5255
"@maven//:com_google_errorprone_error_prone_annotations",
5356
"@maven//:com_google_guava_guava",
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.values;
16+
17+
import com.google.auto.value.AutoValue;
18+
import dev.cel.common.annotations.Internal;
19+
20+
/**
21+
* CelErrorValue represent the intermediate error that occurs during evaluation in the form of Java
22+
* exception. This is used to capture the error that occurs during a non-strict evaluation, which
23+
* may or may not be propagated to the caller.
24+
*
25+
* <p>CEL Library Internals. Do Not Use.
26+
*/
27+
@AutoValue
28+
@AutoValue.CopyAnnotations
29+
@Internal
30+
@SuppressWarnings(
31+
"Immutable") // Exception is technically not immutable as the stacktrace is malleable.
32+
public abstract class ErrorValue extends CelValue {
33+
34+
@Override
35+
public abstract Exception value();
36+
37+
@Override
38+
public boolean isZeroValue() {
39+
return false;
40+
}
41+
42+
public static ErrorValue create(Exception value) {
43+
return new AutoValue_ErrorValue(value);
44+
}
45+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.values;
16+
17+
import com.google.auto.value.AutoValue;
18+
19+
/** OpaqueValue is the value representation of OpaqueType. */
20+
@AutoValue
21+
@AutoValue.CopyAnnotations
22+
@SuppressWarnings("Immutable") // Java Object is mutable.
23+
public abstract class OpaqueValue extends CelValue {
24+
25+
@Override
26+
public boolean isZeroValue() {
27+
return false;
28+
}
29+
30+
public static OpaqueValue create(Object value) {
31+
return new AutoValue_OpaqueValue(value);
32+
}
33+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.values;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static org.junit.Assert.assertThrows;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.JUnit4;
23+
24+
@RunWith(JUnit4.class)
25+
public class ErrorValueTest {
26+
@Test
27+
public void errorValue_construct() {
28+
IllegalArgumentException exception = new IllegalArgumentException("test");
29+
ErrorValue opaqueValue = ErrorValue.create(exception);
30+
31+
assertThat(opaqueValue.value()).isEqualTo(exception);
32+
assertThat(opaqueValue.isZeroValue()).isFalse();
33+
}
34+
35+
@Test
36+
public void create_nullValue_throws() {
37+
assertThrows(NullPointerException.class, () -> ErrorValue.create(null));
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright 2023 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package dev.cel.common.values;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
import static org.junit.Assert.assertThrows;
19+
20+
import org.junit.Test;
21+
import org.junit.runner.RunWith;
22+
import org.junit.runners.JUnit4;
23+
24+
@RunWith(JUnit4.class)
25+
public class OpaqueValueTest {
26+
@Test
27+
public void opaqueValue_construct() {
28+
OpaqueValue opaqueValue = OpaqueValue.create("test");
29+
30+
assertThat(opaqueValue.value()).isEqualTo("test");
31+
assertThat(opaqueValue.isZeroValue()).isFalse();
32+
}
33+
34+
@Test
35+
public void create_nullValue_throws() {
36+
assertThrows(NullPointerException.class, () -> OpaqueValue.create(null));
37+
}
38+
}

0 commit comments

Comments
 (0)