2727import dev .cel .common .formats .YamlHelper .YamlNodeType ;
2828import dev .cel .common .formats .YamlParserContextImpl ;
2929import dev .cel .common .internal .CelCodePointArray ;
30+ import dev .cel .policy .CelPolicy .Import ;
3031import dev .cel .policy .CelPolicy .Match ;
3132import dev .cel .policy .CelPolicy .Match .Result ;
3233import dev .cel .policy .CelPolicy .Variable ;
@@ -118,6 +119,9 @@ public CelPolicy parsePolicy(PolicyParserContext<Node> ctx, Node node) {
118119 Node valueNode = nodeTuple .getValueNode ();
119120 String fieldName = ((ScalarNode ) keyNode ).getValue ();
120121 switch (fieldName ) {
122+ case "imports" :
123+ parseImports (policyBuilder , ctx , valueNode );
124+ break ;
121125 case "name" :
122126 policyBuilder .setName (ctx .newValueString (valueNode ));
123127 break ;
@@ -141,6 +145,51 @@ public CelPolicy parsePolicy(PolicyParserContext<Node> ctx, Node node) {
141145 .build ();
142146 }
143147
148+ private void parseImports (
149+ CelPolicy .Builder policyBuilder , PolicyParserContext <Node > ctx , Node node ) {
150+ long id = ctx .collectMetadata (node );
151+ if (!assertYamlType (ctx , id , node , YamlNodeType .LIST )) {
152+ return ;
153+ }
154+
155+ SequenceNode importListNode = (SequenceNode ) node ;
156+ for (Node importNode : importListNode .getValue ()) {
157+ parseImport (policyBuilder , ctx , importNode );
158+ }
159+ }
160+
161+ private void parseImport (
162+ CelPolicy .Builder policyBuilder , PolicyParserContext <Node > ctx , Node node ) {
163+ long importId = ctx .collectMetadata (node );
164+ if (!assertYamlType (ctx , importId , node , YamlNodeType .MAP )) {
165+ return ;
166+ }
167+
168+ MappingNode mappingNode = (MappingNode ) node ;
169+ for (NodeTuple nodeTuple : mappingNode .getValue ()) {
170+ Node key = nodeTuple .getKeyNode ();
171+ long keyId = ctx .collectMetadata (key );
172+ if (!assertYamlType (ctx , keyId , key , YamlNodeType .STRING , YamlNodeType .TEXT )) {
173+ continue ;
174+ }
175+
176+ String fieldName = ((ScalarNode ) key ).getValue ();
177+ if (!fieldName .equals ("name" )) {
178+ ctx .reportError (
179+ keyId , String .format ("Invalid import key: %s, expected 'name'" , fieldName ));
180+ continue ;
181+ }
182+
183+ Node value = nodeTuple .getValueNode ();
184+ long valueId = ctx .collectMetadata (value );
185+ if (!assertYamlType (ctx , valueId , value , YamlNodeType .STRING , YamlNodeType .TEXT )) {
186+ continue ;
187+ }
188+
189+ policyBuilder .addImport (Import .create (valueId , ctx .newValueString (value )));
190+ }
191+ }
192+
144193 @ Override
145194 public CelPolicy .Rule parseRule (
146195 PolicyParserContext <Node > ctx , CelPolicy .Builder policyBuilder , Node node ) {
0 commit comments