2222import static dev .cel .common .formats .YamlHelper .newString ;
2323import static dev .cel .common .formats .YamlHelper .parseYamlSource ;
2424import static dev .cel .common .formats .YamlHelper .validateYamlType ;
25- import static java .util .Collections .singletonList ;
2625
2726import com .google .common .collect .ImmutableList ;
2827import com .google .common .collect .ImmutableSet ;
2928import com .google .errorprone .annotations .CanIgnoreReturnValue ;
29+ import com .google .errorprone .annotations .CheckReturnValue ;
3030import dev .cel .bundle .CelEnvironment .Alias ;
3131import dev .cel .bundle .CelEnvironment .ContextVariable ;
3232import dev .cel .bundle .CelEnvironment .ExtensionConfig ;
6060 */
6161public final class CelEnvironmentYamlParser {
6262 // Sentinel values to be returned for various declarations when parsing failure is encountered.
63- private static final TypeDecl ERROR_TYPE_DECL = TypeDecl . create ( ERROR ) ;
63+ private static final TypeDecl ERROR_TYPE_DECL = TypeSpecifierParser . ERROR_TYPE_DECL ;
6464 private static final VariableDecl ERROR_VARIABLE_DECL =
6565 VariableDecl .create (ERROR , ERROR_TYPE_DECL );
6666 private static final FunctionDecl ERROR_FUNCTION_DECL =
@@ -71,11 +71,13 @@ public final class CelEnvironmentYamlParser {
7171 private static final Alias ERROR_ALIAS =
7272 Alias .newBuilder ().setAlias (ERROR ).setQualifiedName (ERROR ).build ();
7373
74- /** Generates a new instance of {@code CelEnvironmentYamlParser}. */
74+ /** Generates a new instance of {@code CelEnvironmentYamlParser} with default options . */
7575 public static CelEnvironmentYamlParser newInstance () {
7676 return new CelEnvironmentYamlParser ();
7777 }
7878
79+ private CelEnvironmentYamlParser () {}
80+
7981 /** Parsers the input {@code environmentYamlSource} and returns a {@link CelEnvironment}. */
8082 public CelEnvironment parse (String environmentYamlSource ) throws CelEnvironmentException {
8183 return parse (environmentYamlSource , "<input>" );
@@ -335,6 +337,7 @@ private ContextVariable parseContextVariable(ParserContext<Node> ctx, Node node)
335337 Node valueNode = nodeTuple .getValueNode ();
336338 String keyName = ((ScalarNode ) keyNode ).getValue ();
337339 switch (keyName ) {
340+ case "type" :
338341 case "type_name" :
339342 typeName = newString (ctx , valueNode );
340343 break ;
@@ -478,7 +481,7 @@ private FunctionDecl parseFunction(ParserContext<Node> ctx, Node node) {
478481 return builder .build ();
479482 }
480483
481- private static ImmutableSet <OverloadDecl > parseOverloads (ParserContext <Node > ctx , Node node ) {
484+ private ImmutableSet <OverloadDecl > parseOverloads (ParserContext <Node > ctx , Node node ) {
482485 long listId = ctx .collectMetadata (node );
483486 ImmutableSet .Builder <OverloadDecl > overloadSetBuilder = ImmutableSet .builder ();
484487 if (!assertYamlType (ctx , listId , node , YamlNodeType .LIST )) {
@@ -553,8 +556,7 @@ private static ImmutableList<String> parseOverloadExamples(ParserContext<Node> c
553556 return builder .build ();
554557 }
555558
556- private static ImmutableList <TypeDecl > parseOverloadArguments (
557- ParserContext <Node > ctx , Node node ) {
559+ private ImmutableList <TypeDecl > parseOverloadArguments (ParserContext <Node > ctx , Node node ) {
558560 long listValueId = ctx .collectMetadata (node );
559561 if (!assertYamlType (ctx , listValueId , node , YamlNodeType .LIST )) {
560562 return ImmutableList .of ();
@@ -791,7 +793,7 @@ private static ImmutableSet<OverloadSelector> parseFunctionOverloadsSelector(
791793 }
792794
793795 @ CanIgnoreReturnValue
794- private static TypeDecl .Builder parseInlinedTypeDecl (
796+ private TypeDecl .Builder parseInlinedTypeDecl (
795797 ParserContext <Node > ctx , long keyId , Node keyNode , Node valueNode , TypeDecl .Builder builder ) {
796798 if (!assertYamlType (ctx , keyId , keyNode , YamlNodeType .STRING , YamlNodeType .TEXT )) {
797799 return builder ;
@@ -800,24 +802,28 @@ private static TypeDecl.Builder parseInlinedTypeDecl(
800802 // Create a synthetic node to make this behave as if a `type: ` parent node actually exists.
801803 MappingNode mapNode =
802804 new MappingNode (
803- Tag .MAP , /* value= */ singletonList (new NodeTuple (keyNode , valueNode )), FlowStyle .AUTO );
805+ Tag .MAP ,
806+ /* value= */ ImmutableList .of (new NodeTuple (keyNode , valueNode )),
807+ FlowStyle .AUTO );
804808
805809 return parseTypeDeclFields (ctx , mapNode , builder );
806810 }
807811
808- private static TypeDecl parseTypeDecl (ParserContext <Node > ctx , Node node ) {
809- TypeDecl .Builder builder = TypeDecl .newBuilder ();
812+ private TypeDecl parseTypeDecl (ParserContext <Node > ctx , Node node ) {
810813 long id = ctx .collectMetadata (node );
811- if (! assertYamlType ( ctx , id , node , YamlNodeType .MAP )) {
812- return ERROR_TYPE_DECL ;
814+ if (validateYamlType ( node , YamlNodeType . STRING , YamlNodeType .TEXT )) {
815+ return TypeSpecifierParser . parse ( ctx , id , newString ( ctx , node )) ;
813816 }
814-
815- MappingNode mapNode = (MappingNode ) node ;
816- return parseTypeDeclFields (ctx , mapNode , builder ).build ();
817+ if (validateYamlType (node , YamlNodeType .MAP )) {
818+ TypeDecl .Builder builder = TypeDecl .newBuilder ();
819+ return parseTypeDeclFields (ctx , (MappingNode ) node , builder ).build ();
820+ }
821+ assertYamlType (ctx , id , node , YamlNodeType .STRING , YamlNodeType .TEXT , YamlNodeType .MAP );
822+ return ERROR_TYPE_DECL ;
817823 }
818824
819825 @ CanIgnoreReturnValue
820- private static TypeDecl .Builder parseTypeDeclFields (
826+ private TypeDecl .Builder parseTypeDeclFields (
821827 ParserContext <Node > ctx , MappingNode mapNode , TypeDecl .Builder builder ) {
822828 for (NodeTuple nodeTuple : mapNode .getValue ()) {
823829 Node keyNode = nodeTuple .getKeyNode ();
@@ -943,6 +949,4 @@ private CelEnvironment.Builder parseConfig(ParserContext<Node> ctx, Node node) {
943949 return builder ;
944950 }
945951 }
946-
947- private CelEnvironmentYamlParser () {}
948952}
0 commit comments