|
19 | 19 | import static dev.cel.common.CelFunctionDecl.newFunctionDeclaration; |
20 | 20 | import static dev.cel.common.CelOverloadDecl.newGlobalOverload; |
21 | 21 | import static dev.cel.common.CelOverloadDecl.newMemberOverload; |
| 22 | +import static dev.cel.common.CelSource.Extension; |
22 | 23 | import static org.junit.Assert.assertThrows; |
23 | 24 |
|
| 25 | +import dev.cel.common.CelValidationException; |
24 | 26 | import dev.cel.expr.CheckedExpr; |
25 | 27 | import dev.cel.expr.Constant; |
| 28 | +import dev.cel.runtime.CelEvaluationException; |
26 | 29 | import dev.cel.expr.Decl; |
27 | 30 | import dev.cel.expr.Decl.FunctionDecl; |
28 | 31 | import dev.cel.expr.Decl.FunctionDecl.Overload; |
|
47 | 50 | import com.google.protobuf.Descriptors.FileDescriptor; |
48 | 51 | import com.google.protobuf.Duration; |
49 | 52 | import com.google.protobuf.DynamicMessage; |
50 | | -import com.google.protobuf.Empty; |
51 | 53 | import com.google.protobuf.ExtensionRegistry; |
52 | 54 | import com.google.protobuf.FieldMask; |
53 | 55 | import com.google.protobuf.Message; |
54 | 56 | import com.google.protobuf.Struct; |
55 | 57 | import com.google.protobuf.TextFormat; |
56 | 58 | import com.google.protobuf.Timestamp; |
57 | 59 | import com.google.protobuf.TypeRegistry; |
58 | | -import com.google.protobuf.WrappersProto; |
59 | 60 | import com.google.rpc.context.AttributeContext; |
60 | 61 | import com.google.testing.junit.testparameterinjector.TestParameter; |
61 | 62 | import com.google.testing.junit.testparameterinjector.TestParameterInjector; |
|
72 | 73 | import dev.cel.common.CelOptions; |
73 | 74 | import dev.cel.common.CelProtoAbstractSyntaxTree; |
74 | 75 | import dev.cel.common.CelSourceLocation; |
75 | | -import dev.cel.common.CelValidationException; |
76 | 76 | import dev.cel.common.CelValidationResult; |
77 | 77 | import dev.cel.common.CelVarDecl; |
78 | 78 | import dev.cel.common.ast.CelExpr; |
|
102 | 102 | import dev.cel.runtime.CelAttribute; |
103 | 103 | import dev.cel.runtime.CelAttribute.Qualifier; |
104 | 104 | import dev.cel.runtime.CelAttributePattern; |
105 | | -import dev.cel.runtime.CelEvaluationException; |
106 | 105 | import dev.cel.runtime.CelEvaluationExceptionBuilder; |
107 | 106 | import dev.cel.runtime.CelFunctionBinding; |
108 | 107 | import dev.cel.runtime.CelRuntime; |
|
112 | 111 | import dev.cel.runtime.CelUnknownSet; |
113 | 112 | import dev.cel.runtime.CelVariableResolver; |
114 | 113 | import dev.cel.runtime.UnknownContext; |
| 114 | +import dev.cel.testing.testdata.SingleFileProto.SingleFile; |
115 | 115 | import dev.cel.testing.testdata.proto3.StandaloneGlobalEnum; |
116 | 116 | import java.time.Instant; |
117 | 117 | import java.util.ArrayList; |
@@ -2193,6 +2193,74 @@ public void toBuilder_isImmutable() { |
2193 | 2193 | assertThat(newRuntimeBuilder).isNotEqualTo(celImpl.toRuntimeBuilder()); |
2194 | 2194 | } |
2195 | 2195 |
|
| 2196 | + @Test |
| 2197 | + public void eval_withJsonFieldName() throws Exception { |
| 2198 | + Cel cel = |
| 2199 | + standardCelBuilderWithMacros() |
| 2200 | + .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) |
| 2201 | + .addMessageTypes(SingleFile.getDescriptor()) |
| 2202 | + .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) |
| 2203 | + .build(); |
| 2204 | + CelAbstractSyntaxTree ast = |
| 2205 | + cel.compile("file.camelCased").getAst(); |
| 2206 | + |
| 2207 | + Object result = cel.createProgram(ast).eval(ImmutableMap.of("file", SingleFile.newBuilder().setSnakeCased("foo").build())); |
| 2208 | + |
| 2209 | + assertThat(result).isEqualTo("foo"); |
| 2210 | + } |
| 2211 | + |
| 2212 | + @Test |
| 2213 | + public void eval_withJsonFieldName_runtimeOptionDisabled_throws() throws Exception { |
| 2214 | + CelCompiler celCompiler = |
| 2215 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 2216 | + .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) |
| 2217 | + .addMessageTypes(SingleFile.getDescriptor()) |
| 2218 | + .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) |
| 2219 | + .build(); |
| 2220 | + CelRuntime celRuntime = |
| 2221 | + CelRuntimeFactory.standardCelRuntimeBuilder() |
| 2222 | + .addMessageTypes(SingleFile.getDescriptor()) |
| 2223 | + .setOptions(CelOptions.current().enableJsonFieldNames(false).build()) |
| 2224 | + .build(); |
| 2225 | + CelAbstractSyntaxTree ast = celCompiler.compile("file.camelCased").getAst(); |
| 2226 | + |
| 2227 | + CelEvaluationException e = |
| 2228 | + assertThrows( |
| 2229 | + CelEvaluationException.class, |
| 2230 | + () -> celRuntime.createProgram(ast).eval(ImmutableMap.of("file", SingleFile.getDefaultInstance()))); |
| 2231 | + assertThat(e).hasMessageThat().contains("field 'camelCased' is not declared in message 'dev.cel.testing.testdata.SingleFile"); |
| 2232 | + } |
| 2233 | + |
| 2234 | + @Test |
| 2235 | + public void compile_withJsonFieldName_astTagged() throws Exception { |
| 2236 | + Cel cel = |
| 2237 | + standardCelBuilderWithMacros() |
| 2238 | + .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) |
| 2239 | + .addMessageTypes(SingleFile.getDescriptor()) |
| 2240 | + .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) |
| 2241 | + .build(); |
| 2242 | + CelAbstractSyntaxTree ast = |
| 2243 | + cel.compile("file.camelCased").getAst(); |
| 2244 | + |
| 2245 | + assertThat(ast.getSource().getExtensions()).contains(Extension.create("json_name", Extension.Version.of(1L, 1L), Extension.Component.COMPONENT_RUNTIME)); |
| 2246 | + } |
| 2247 | + |
| 2248 | + @Test |
| 2249 | + public void compile_withJsonFieldName_protoFieldNameComparison_throws() throws Exception { |
| 2250 | + Cel cel = |
| 2251 | + standardCelBuilderWithMacros() |
| 2252 | + .addVar("file", StructTypeReference.create(SingleFile.getDescriptor().getFullName())) |
| 2253 | + .addMessageTypes(SingleFile.getDescriptor()) |
| 2254 | + .setOptions(CelOptions.current().enableJsonFieldNames(true).build()) |
| 2255 | + .build(); |
| 2256 | + |
| 2257 | + CelValidationException e = |
| 2258 | + assertThrows( |
| 2259 | + CelValidationException.class, |
| 2260 | + () -> cel.compile("file.camelCased == file.snake_cased").getAst()); |
| 2261 | + assertThat(e).hasMessageThat().contains("undefined field 'snake_cased'"); |
| 2262 | + } |
| 2263 | + |
2196 | 2264 | private static TypeProvider aliasingProvider(ImmutableMap<String, Type> typeAliases) { |
2197 | 2265 | return new TypeProvider() { |
2198 | 2266 | @Override |
|
0 commit comments