|
28 | 28 | import com.google.common.collect.ImmutableSet; |
29 | 29 | import com.google.errorprone.annotations.CanIgnoreReturnValue; |
30 | 30 | import dev.cel.bundle.CelEnvironment.Alias; |
| 31 | +import dev.cel.bundle.CelEnvironment.ContextVariable; |
31 | 32 | import dev.cel.bundle.CelEnvironment.ExtensionConfig; |
32 | 33 | import dev.cel.bundle.CelEnvironment.FunctionDecl; |
33 | 34 | import dev.cel.bundle.CelEnvironment.LibrarySubset; |
@@ -320,6 +321,36 @@ private ImmutableSet<String> parseAbbreviations(ParserContext<Node> ctx, Node no |
320 | 321 | return builder.build(); |
321 | 322 | } |
322 | 323 |
|
| 324 | + private ContextVariable parseContextVariable(ParserContext<Node> ctx, Node node) { |
| 325 | + long id = ctx.collectMetadata(node); |
| 326 | + if (!assertYamlType(ctx, id, node, YamlNodeType.MAP)) { |
| 327 | + return ContextVariable.create(""); |
| 328 | + } |
| 329 | + |
| 330 | + MappingNode mapNode = (MappingNode) node; |
| 331 | + String typeName = ""; |
| 332 | + for (NodeTuple nodeTuple : mapNode.getValue()) { |
| 333 | + Node keyNode = nodeTuple.getKeyNode(); |
| 334 | + long keyId = ctx.collectMetadata(keyNode); |
| 335 | + Node valueNode = nodeTuple.getValueNode(); |
| 336 | + String keyName = ((ScalarNode) keyNode).getValue(); |
| 337 | + switch (keyName) { |
| 338 | + case "type_name": |
| 339 | + typeName = newString(ctx, valueNode); |
| 340 | + break; |
| 341 | + default: |
| 342 | + ctx.reportError(keyId, String.format("Unsupported context_variable tag: %s", keyName)); |
| 343 | + break; |
| 344 | + } |
| 345 | + } |
| 346 | + |
| 347 | + if (typeName.isEmpty()) { |
| 348 | + ctx.reportError(id, "Missing required attribute(s): type_name"); |
| 349 | + } |
| 350 | + |
| 351 | + return ContextVariable.create(typeName); |
| 352 | + } |
| 353 | + |
323 | 354 | private ImmutableSet<VariableDecl> parseVariables(ParserContext<Node> ctx, Node node) { |
324 | 355 | long valueId = ctx.collectMetadata(node); |
325 | 356 | ImmutableSet.Builder<VariableDecl> variableSetBuilder = ImmutableSet.builder(); |
@@ -900,6 +931,9 @@ private CelEnvironment.Builder parseConfig(ParserContext<Node> ctx, Node node) { |
900 | 931 | case "limits": |
901 | 932 | builder.setLimits(parseLimits(ctx, valueNode)); |
902 | 933 | break; |
| 934 | + case "context_variable": |
| 935 | + builder.setContextVariable(parseContextVariable(ctx, valueNode)); |
| 936 | + break; |
903 | 937 | default: |
904 | 938 | ctx.reportError(id, "Unknown config tag: " + fieldName); |
905 | 939 | // continue handling the rest of the nodes |
|
0 commit comments