1414
1515#include " extensions/comprehensions_v2_macros.h"
1616
17+ #include < string>
1718#include < utility>
1819#include < vector>
1920
2021#include " absl/base/no_destructor.h"
2122#include " absl/log/absl_check.h"
2223#include " absl/status/status.h"
24+ #include " absl/strings/match.h"
2325#include " absl/strings/str_cat.h"
2426#include " absl/types/optional.h"
2527#include " absl/types/span.h"
@@ -38,16 +40,21 @@ namespace {
3840
3941using ::google::api::expr::common::CelOperator;
4042
43+ bool IsSimpleIdentifier (const Expr& expr) {
44+ return expr.has_ident_expr () && !expr.ident_expr ().name ().empty () &&
45+ !absl::StartsWith (expr.ident_expr ().name (), " ." );
46+ }
47+
4148absl::optional<Expr> ExpandAllMacro2 (MacroExprFactory& factory, Expr& target,
4249 absl::Span<Expr> args) {
4350 if (args.size () != 3 ) {
4451 return factory.ReportError (" all() requires 3 arguments" );
4552 }
46- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
53+ if (!IsSimpleIdentifier ( args[0 ])) {
4754 return factory.ReportErrorAt (
4855 args[0 ], " all() first variable name must be a simple identifier" );
4956 }
50- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
57+ if (!IsSimpleIdentifier ( args[1 ])) {
5158 return factory.ReportErrorAt (
5259 args[1 ], " all() second variable name must be a simple identifier" );
5360 }
@@ -89,11 +96,11 @@ absl::optional<Expr> ExpandExistsMacro2(MacroExprFactory& factory, Expr& target,
8996 if (args.size () != 3 ) {
9097 return factory.ReportError (" exists() requires 3 arguments" );
9198 }
92- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
99+ if (!IsSimpleIdentifier ( args[0 ])) {
93100 return factory.ReportErrorAt (
94101 args[0 ], " exists() first variable name must be a simple identifier" );
95102 }
96- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
103+ if (!IsSimpleIdentifier ( args[1 ])) {
97104 return factory.ReportErrorAt (
98105 args[1 ], " exists() second variable name must be a simple identifier" );
99106 }
@@ -138,11 +145,11 @@ absl::optional<Expr> ExpandExistsOneMacro2(MacroExprFactory& factory,
138145 if (args.size () != 3 ) {
139146 return factory.ReportError (" existsOne() requires 3 arguments" );
140147 }
141- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
148+ if (!IsSimpleIdentifier ( args[0 ])) {
142149 return factory.ReportErrorAt (
143150 args[0 ], " existsOne() first variable name must be a simple identifier" );
144151 }
145- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
152+ if (!IsSimpleIdentifier ( args[1 ])) {
146153 return factory.ReportErrorAt (
147154 args[1 ],
148155 " existsOne() second variable name must be a simple identifier" );
@@ -190,12 +197,12 @@ absl::optional<Expr> ExpandTransformList3Macro(MacroExprFactory& factory,
190197 if (args.size () != 3 ) {
191198 return factory.ReportError (" transformList() requires 3 arguments" );
192199 }
193- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
200+ if (!IsSimpleIdentifier ( args[0 ])) {
194201 return factory.ReportErrorAt (
195202 args[0 ],
196203 " transformList() first variable name must be a simple identifier" );
197204 }
198- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
205+ if (!IsSimpleIdentifier ( args[1 ])) {
199206 return factory.ReportErrorAt (
200207 args[1 ],
201208 " transformList() second variable name must be a simple identifier" );
@@ -239,12 +246,12 @@ absl::optional<Expr> ExpandTransformList4Macro(MacroExprFactory& factory,
239246 if (args.size () != 4 ) {
240247 return factory.ReportError (" transformList() requires 4 arguments" );
241248 }
242- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
249+ if (!IsSimpleIdentifier ( args[0 ])) {
243250 return factory.ReportErrorAt (
244251 args[0 ],
245252 " transformList() first variable name must be a simple identifier" );
246253 }
247- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
254+ if (!IsSimpleIdentifier ( args[1 ])) {
248255 return factory.ReportErrorAt (
249256 args[1 ],
250257 " transformList() second variable name must be a simple identifier" );
@@ -290,12 +297,12 @@ absl::optional<Expr> ExpandTransformMap3Macro(MacroExprFactory& factory,
290297 if (args.size () != 3 ) {
291298 return factory.ReportError (" transformMap() requires 3 arguments" );
292299 }
293- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
300+ if (!IsSimpleIdentifier ( args[0 ])) {
294301 return factory.ReportErrorAt (
295302 args[0 ],
296303 " transformMap() first variable name must be a simple identifier" );
297304 }
298- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
305+ if (!IsSimpleIdentifier ( args[1 ])) {
299306 return factory.ReportErrorAt (
300307 args[1 ],
301308 " transformMap() second variable name must be a simple identifier" );
@@ -338,12 +345,12 @@ absl::optional<Expr> ExpandTransformMap4Macro(MacroExprFactory& factory,
338345 if (args.size () != 4 ) {
339346 return factory.ReportError (" transformMap() requires 4 arguments" );
340347 }
341- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
348+ if (!IsSimpleIdentifier ( args[0 ])) {
342349 return factory.ReportErrorAt (
343350 args[0 ],
344351 " transformMap() first variable name must be a simple identifier" );
345352 }
346- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
353+ if (!IsSimpleIdentifier ( args[1 ])) {
347354 return factory.ReportErrorAt (
348355 args[1 ],
349356 " transformMap() second variable name must be a simple identifier" );
@@ -388,12 +395,12 @@ absl::optional<Expr> ExpandTransformMapEntry3Macro(MacroExprFactory& factory,
388395 if (args.size () != 3 ) {
389396 return factory.ReportError (" transformMapEntry() requires 3 arguments" );
390397 }
391- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
398+ if (!IsSimpleIdentifier ( args[0 ])) {
392399 return factory.ReportErrorAt (
393400 args[0 ],
394401 " transformMapEntry() first variable name must be a simple identifier" );
395402 }
396- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
403+ if (!IsSimpleIdentifier ( args[1 ])) {
397404 return factory.ReportErrorAt (
398405 args[1 ],
399406 " transformMapEntry() second variable name must be a simple identifier" );
@@ -438,12 +445,12 @@ absl::optional<Expr> ExpandTransformMapEntry4Macro(MacroExprFactory& factory,
438445 if (args.size () != 4 ) {
439446 return factory.ReportError (" transformMapEntry() requires 4 arguments" );
440447 }
441- if (!args[ 0 ]. has_ident_expr () || args[0 ]. ident_expr (). name (). empty ( )) {
448+ if (!IsSimpleIdentifier ( args[0 ])) {
442449 return factory.ReportErrorAt (
443450 args[0 ],
444451 " transformMapEntry() first variable name must be a simple identifier" );
445452 }
446- if (!args[ 1 ]. has_ident_expr () || args[1 ]. ident_expr (). name (). empty ( )) {
453+ if (!IsSimpleIdentifier ( args[1 ])) {
447454 return factory.ReportErrorAt (
448455 args[1 ],
449456 " transformMapEntry() second variable name must be a simple identifier" );
0 commit comments