Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions extensions/comprehensions_v2_macros.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@

#include "extensions/comprehensions_v2_macros.h"

#include <string>
#include <utility>
#include <vector>

#include "absl/base/no_destructor.h"
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/types/optional.h"
#include "absl/types/span.h"
Expand All @@ -38,16 +40,21 @@ namespace {

using ::google::api::expr::common::CelOperator;

bool IsSimpleIdentifier(const Expr& expr) {
return expr.has_ident_expr() && !expr.ident_expr().name().empty() &&
!absl::StartsWith(expr.ident_expr().name(), ".");
}

absl::optional<Expr> ExpandAllMacro2(MacroExprFactory& factory, Expr& target,
absl::Span<Expr> args) {
if (args.size() != 3) {
return factory.ReportError("all() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "all() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1], "all() second variable name must be a simple identifier");
}
Expand Down Expand Up @@ -89,11 +96,11 @@ absl::optional<Expr> ExpandExistsMacro2(MacroExprFactory& factory, Expr& target,
if (args.size() != 3) {
return factory.ReportError("exists() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "exists() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1], "exists() second variable name must be a simple identifier");
}
Expand Down Expand Up @@ -138,11 +145,11 @@ absl::optional<Expr> ExpandExistsOneMacro2(MacroExprFactory& factory,
if (args.size() != 3) {
return factory.ReportError("existsOne() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "existsOne() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"existsOne() second variable name must be a simple identifier");
Expand Down Expand Up @@ -190,12 +197,12 @@ absl::optional<Expr> ExpandTransformList3Macro(MacroExprFactory& factory,
if (args.size() != 3) {
return factory.ReportError("transformList() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformList() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformList() second variable name must be a simple identifier");
Expand Down Expand Up @@ -239,12 +246,12 @@ absl::optional<Expr> ExpandTransformList4Macro(MacroExprFactory& factory,
if (args.size() != 4) {
return factory.ReportError("transformList() requires 4 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformList() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformList() second variable name must be a simple identifier");
Expand Down Expand Up @@ -290,12 +297,12 @@ absl::optional<Expr> ExpandTransformMap3Macro(MacroExprFactory& factory,
if (args.size() != 3) {
return factory.ReportError("transformMap() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformMap() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformMap() second variable name must be a simple identifier");
Expand Down Expand Up @@ -338,12 +345,12 @@ absl::optional<Expr> ExpandTransformMap4Macro(MacroExprFactory& factory,
if (args.size() != 4) {
return factory.ReportError("transformMap() requires 4 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformMap() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformMap() second variable name must be a simple identifier");
Expand Down Expand Up @@ -388,12 +395,12 @@ absl::optional<Expr> ExpandTransformMapEntry3Macro(MacroExprFactory& factory,
if (args.size() != 3) {
return factory.ReportError("transformMapEntry() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformMapEntry() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformMapEntry() second variable name must be a simple identifier");
Expand Down Expand Up @@ -438,12 +445,12 @@ absl::optional<Expr> ExpandTransformMapEntry4Macro(MacroExprFactory& factory,
if (args.size() != 4) {
return factory.ReportError("transformMapEntry() requires 4 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0],
"transformMapEntry() first variable name must be a simple identifier");
}
if (!args[1].has_ident_expr() || args[1].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[1])) {
return factory.ReportErrorAt(
args[1],
"transformMapEntry() second variable name must be a simple identifier");
Expand Down
22 changes: 14 additions & 8 deletions parser/macro.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "absl/log/absl_check.h"
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "absl/types/optional.h"
Expand All @@ -40,6 +41,11 @@ namespace {

using google::api::expr::common::CelOperator;

bool IsSimpleIdentifier(const Expr& expr) {
return expr.has_ident_expr() && !expr.ident_expr().name().empty() &&
!absl::StartsWith(expr.ident_expr().name(), ".");
}

inline MacroExpander ToMacroExpander(GlobalMacroExpander expander) {
ABSL_DCHECK(expander);
return [expander = std::move(expander)](
Expand Down Expand Up @@ -87,7 +93,7 @@ absl::optional<Expr> ExpandAllMacro(MacroExprFactory& factory, Expr& target,
if (args.size() != 2) {
return factory.ReportError("all() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "all() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -119,7 +125,7 @@ absl::optional<Expr> ExpandExistsMacro(MacroExprFactory& factory, Expr& target,
if (args.size() != 2) {
return factory.ReportError("exists() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "exists() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -153,7 +159,7 @@ absl::optional<Expr> ExpandExistsOneMacro(MacroExprFactory& factory,
if (args.size() != 2) {
return factory.ReportError("exists_one() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "exists_one() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -192,7 +198,7 @@ absl::optional<Expr> ExpandMap2Macro(MacroExprFactory& factory, Expr& target,
if (args.size() != 2) {
return factory.ReportError("map() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "map() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -225,7 +231,7 @@ absl::optional<Expr> ExpandMap3Macro(MacroExprFactory& factory, Expr& target,
if (args.size() != 3) {
return factory.ReportError("map() requires 3 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "map() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -260,7 +266,7 @@ absl::optional<Expr> ExpandFilterMacro(MacroExprFactory& factory, Expr& target,
if (args.size() != 2) {
return factory.ReportError("filter() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "filter() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -298,7 +304,7 @@ absl::optional<Expr> ExpandOptMapMacro(MacroExprFactory& factory, Expr& target,
if (args.size() != 2) {
return factory.ReportError("optMap() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "optMap() variable name must be a simple identifier");
}
Expand Down Expand Up @@ -337,7 +343,7 @@ absl::optional<Expr> ExpandOptFlatMapMacro(MacroExprFactory& factory,
if (args.size() != 2) {
return factory.ReportError("optFlatMap() requires 2 arguments");
}
if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) {
if (!IsSimpleIdentifier(args[0])) {
return factory.ReportErrorAt(
args[0], "optFlatMap() variable name must be a simple identifier");
}
Expand Down
21 changes: 21 additions & 0 deletions parser/parser_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,27 @@ std::vector<TestInfo> test_cases = {
"ERROR: <input>:1:7: all() variable name must be a simple identifier\n"
" | 1.all(2, 3)\n"
" | ......^"},
{"[].all(.x, x)", "",
"ERROR: <input>:1:9: all() variable name must be a simple identifier\n"
" | [].all(.x, x)\n"
" | ........^"},
{"[].exists(.x, x)", "",
"ERROR: <input>:1:12: exists() variable name must be a simple identifier\n"
" | [].exists(.x, x)\n"
" | ...........^"},
{"[].exists_one(.x, x)", "",
"ERROR: <input>:1:16: exists_one() variable name must be a simple "
"identifier\n"
" | [].exists_one(.x, x)\n"
" | ...............^"},
{"[].map(.x, x, x)", "",
"ERROR: <input>:1:9: map() variable name must be a simple identifier\n"
" | [].map(.x, x, x)\n"
" | ........^"},
{"[].filter(.x, x)", "",
"ERROR: <input>:1:12: filter() variable name must be a simple identifier\n"
" | [].filter(.x, x)\n"
" | ...........^"},
{"x[\"a\"].single_int32 == 23",
"_==_(\n"
" _[_](\n"
Expand Down
Loading