forked from google/cel-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCelConstant.java
More file actions
231 lines (199 loc) · 6.65 KB
/
CelConstant.java
File metadata and controls
231 lines (199 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// 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.ast;
import com.google.auto.value.AutoOneOf;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.UnsignedLong;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.InlineMe;
import com.google.protobuf.ByteString;
import com.google.protobuf.Duration;
import com.google.protobuf.Timestamp;
import dev.cel.common.annotations.Internal;
import dev.cel.common.values.CelByteString;
import dev.cel.common.values.NullValue;
/**
* Represents a primitive literal.
*
* <p>This is the CEL-Java native type equivalent of Constant message type from syntax.proto.
*/
@AutoOneOf(CelConstant.Kind.class)
@Internal
@Immutable
public abstract class CelConstant {
private static final ImmutableSet<Class<?>> CONSTANT_CLASSES =
ImmutableSet.of(
NullValue.class,
Boolean.class,
Long.class,
UnsignedLong.class,
Double.class,
String.class,
CelByteString.class);
/** Represents the type of the Constant */
public enum Kind {
NOT_SET,
NULL_VALUE,
BOOLEAN_VALUE,
INT64_VALUE,
UINT64_VALUE,
DOUBLE_VALUE,
STRING_VALUE,
BYTES_VALUE,
/**
* @deprecated Do not use. Timestamp is no longer built-in CEL type.
*/
@Deprecated
TIMESTAMP_VALUE,
/**
* @deprecated Do not use. Duration is no longer built-in CEL type.
*/
@Deprecated
DURATION_VALUE
}
public abstract Kind getKind();
/**
* An unset constant.
*
* <p>As the name implies, this constant does nothing. This is used to represent a default
* instance of CelConstant.
*/
@AutoValue
@Immutable
public abstract static class CelConstantNotSet {}
public abstract CelConstantNotSet notSet();
public abstract NullValue nullValue();
public abstract boolean booleanValue();
public abstract long int64Value();
public abstract UnsignedLong uint64Value();
public abstract double doubleValue();
public abstract String stringValue();
public abstract CelByteString bytesValue();
/**
* @deprecated Do not use. Timestamp is no longer built-in CEL type.
*/
@Deprecated
public abstract Timestamp timestampValue();
/**
* @deprecated Do not use. Duration is no longer built-in CEL type.
*/
@Deprecated
public abstract Duration durationValue();
public static CelConstant ofNotSet() {
return AutoOneOf_CelConstant.notSet(new AutoValue_CelConstant_CelConstantNotSet());
}
public static CelConstant ofValue(NullValue value) {
return AutoOneOf_CelConstant.nullValue(value);
}
public static CelConstant ofValue(boolean value) {
return AutoOneOf_CelConstant.booleanValue(value);
}
public static CelConstant ofValue(long value) {
return AutoOneOf_CelConstant.int64Value(value);
}
public static CelConstant ofValue(UnsignedLong value) {
return AutoOneOf_CelConstant.uint64Value(value);
}
public static CelConstant ofValue(double value) {
return AutoOneOf_CelConstant.doubleValue(value);
}
public static CelConstant ofValue(String value) {
return AutoOneOf_CelConstant.stringValue(value);
}
public static CelConstant ofValue(CelByteString value) {
return AutoOneOf_CelConstant.bytesValue(value);
}
/**
* @deprecated Use native type equivalent {@link #ofValue(NullValue)} instead.
*/
@InlineMe(
replacement = "CelConstant.ofValue(NullValue.NULL_VALUE)",
imports = {"dev.cel.common.ast.CelConstant", "dev.cel.common.values.NullValue"})
@Deprecated
public static CelConstant ofValue(com.google.protobuf.NullValue unused) {
return ofValue(NullValue.NULL_VALUE);
}
/**
* @deprecated Use native type equivalent {@link #ofValue(CelByteString)} instead.
*/
@Deprecated
public static CelConstant ofValue(ByteString value) {
CelByteString celByteString = CelByteString.of(value.toByteArray());
return ofValue(celByteString);
}
/**
* @deprecated Do not use. Duration is no longer built-in CEL type.
*/
@Deprecated
public static CelConstant ofValue(Duration value) {
return AutoOneOf_CelConstant.durationValue(value);
}
/**
* @deprecated Do not use. Timestamp is no longer built-in CEL type.
*/
@Deprecated
public static CelConstant ofValue(Timestamp value) {
return AutoOneOf_CelConstant.timestampValue(value);
}
/** Checks whether the provided Java object is a valid CelConstant value. */
public static boolean isConstantValue(Object value) {
return CONSTANT_CLASSES.contains(value.getClass());
}
/**
* Converts the given Java object into a CelConstant value. This is equivalent of calling {@link
* CelConstant#ofValue} with concrete types.
*
* @throws IllegalArgumentException If the value is not a supported CelConstant. This includes the
* deprecated duration and timestamp values.
*/
public static CelConstant ofObjectValue(Object value) {
if (value instanceof NullValue) {
return ofValue((NullValue) value);
} else if (value instanceof Boolean) {
return ofValue((boolean) value);
} else if (value instanceof Long) {
return ofValue((long) value);
} else if (value instanceof UnsignedLong) {
return ofValue((UnsignedLong) value);
} else if (value instanceof Double) {
return ofValue((double) value);
} else if (value instanceof String) {
return ofValue((String) value);
} else if (value instanceof CelByteString) {
return ofValue((CelByteString) value);
}
throw new IllegalArgumentException("Value is not a CelConstant: " + value);
}
public Object objectValue() {
switch (getKind()) {
case NULL_VALUE:
return nullValue();
case BOOLEAN_VALUE:
return booleanValue();
case INT64_VALUE:
return int64Value();
case UINT64_VALUE:
return uint64Value();
case DOUBLE_VALUE:
return doubleValue();
case STRING_VALUE:
return stringValue();
case BYTES_VALUE:
return bytesValue();
default:
throw new IllegalStateException("Unsupported kind: " + getKind());
}
}
}