Skip to content

Commit 31f0895

Browse files
jnthntatumcopybara-github
authored andcommitted
Export unparser utility to github.
PiperOrigin-RevId: 810194860
1 parent f4068db commit 31f0895

7 files changed

Lines changed: 1573 additions & 119 deletions

File tree

common/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ cc_library(
290290
"operators.h",
291291
],
292292
deps = [
293+
"@com_google_absl//absl/container:flat_hash_map",
293294
"@com_google_absl//absl/strings",
294295
"@com_google_absl//absl/types:optional",
295296
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",

common/operators.cc

Lines changed: 100 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,143 +1,135 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
#include "common/operators.h"
216

3-
#include <map>
4-
#include <memory>
517
#include <string>
618

19+
#include "absl/container/flat_hash_map.h"
20+
#include "absl/strings/string_view.h"
21+
#include "absl/types/optional.h"
22+
723
#undef IN
824

9-
namespace google {
10-
namespace api {
11-
namespace expr {
12-
namespace common {
25+
namespace google::api::expr::common {
1326

1427
namespace {
1528
// These functions provide access to reverse mappings for operators.
1629
// Functions generally map from text expression to Expr representation,
1730
// e.g., from "&&" to "_&&_". Reverse operators provides a mapping from
1831
// Expr to textual mapping, e.g., from "_&&_" to "&&".
1932

20-
const std::map<std::string, std::string>& UnaryOperators() {
21-
static std::shared_ptr<std::map<std::string, std::string>> unaries_map =
22-
[&]() {
23-
auto u = std::make_shared<std::map<std::string, std::string>>(
24-
std::map<std::string, std::string>{
25-
{CelOperator::NEGATE, "-"}, {CelOperator::LOGICAL_NOT, "!"}});
26-
return u;
27-
}();
33+
const absl::flat_hash_map<std::string, std::string>& UnaryOperators() {
34+
static auto* unaries_map = new absl::flat_hash_map<std::string, std::string>{
35+
{CelOperator::NEGATE, "-"}, {CelOperator::LOGICAL_NOT, "!"}};
2836
return *unaries_map;
2937
}
3038

31-
const std::map<std::string, std::string>& BinaryOperators() {
32-
static std::shared_ptr<std::map<std::string, std::string>> binops_map =
33-
[&]() {
34-
auto c = std::make_shared<std::map<std::string, std::string>>(
35-
std::map<std::string, std::string>{
36-
{CelOperator::LOGICAL_OR, "||"},
37-
{CelOperator::LOGICAL_AND, "&&"},
38-
{CelOperator::LESS_EQUALS, "<="},
39-
{CelOperator::LESS, "<"},
40-
{CelOperator::GREATER_EQUALS, ">="},
41-
{CelOperator::GREATER, ">"},
42-
{CelOperator::EQUALS, "=="},
43-
{CelOperator::NOT_EQUALS, "!="},
44-
{CelOperator::IN_DEPRECATED, "in"},
45-
{CelOperator::IN, "in"},
46-
{CelOperator::ADD, "+"},
47-
{CelOperator::SUBTRACT, "-"},
48-
{CelOperator::MULTIPLY, "*"},
49-
{CelOperator::DIVIDE, "/"},
50-
{CelOperator::MODULO, "%"}});
51-
return c;
52-
}();
39+
const absl::flat_hash_map<std::string, std::string>& BinaryOperators() {
40+
static auto* binops_map = new absl::flat_hash_map<std::string, std::string>{
41+
{CelOperator::LOGICAL_OR, "||"},
42+
{CelOperator::LOGICAL_AND, "&&"},
43+
{CelOperator::LESS_EQUALS, "<="},
44+
{CelOperator::LESS, "<"},
45+
{CelOperator::GREATER_EQUALS, ">="},
46+
{CelOperator::GREATER, ">"},
47+
{CelOperator::EQUALS, "=="},
48+
{CelOperator::NOT_EQUALS, "!="},
49+
{CelOperator::IN_DEPRECATED, "in"},
50+
{CelOperator::IN, "in"},
51+
{CelOperator::ADD, "+"},
52+
{CelOperator::SUBTRACT, "-"},
53+
{CelOperator::MULTIPLY, "*"},
54+
{CelOperator::DIVIDE, "/"},
55+
{CelOperator::MODULO, "%"}};
5356
return *binops_map;
5457
}
5558

56-
const std::map<std::string, std::string>& ReverseOperators() {
57-
static std::shared_ptr<std::map<std::string, std::string>> operators_map =
58-
[&]() {
59-
auto c = std::make_shared<std::map<std::string, std::string>>(
60-
std::map<std::string, std::string>{
61-
{"+", CelOperator::ADD},
62-
{"-", CelOperator::SUBTRACT},
63-
{"*", CelOperator::MULTIPLY},
64-
{"/", CelOperator::DIVIDE},
65-
{"%", CelOperator::MODULO},
66-
{"==", CelOperator::EQUALS},
67-
{"!=", CelOperator::NOT_EQUALS},
68-
{">", CelOperator::GREATER},
69-
{">=", CelOperator::GREATER_EQUALS},
70-
{"<", CelOperator::LESS},
71-
{"<=", CelOperator::LESS_EQUALS},
72-
{"&&", CelOperator::LOGICAL_AND},
73-
{"!", CelOperator::LOGICAL_NOT},
74-
{"||", CelOperator::LOGICAL_OR},
75-
{"in", CelOperator::IN},
76-
});
77-
return c;
78-
}();
59+
const absl::flat_hash_map<std::string, std::string>& ReverseOperators() {
60+
static auto* operators_map =
61+
new absl::flat_hash_map<std::string, std::string>{
62+
{"+", CelOperator::ADD},
63+
{"-", CelOperator::SUBTRACT},
64+
{"*", CelOperator::MULTIPLY},
65+
{"/", CelOperator::DIVIDE},
66+
{"%", CelOperator::MODULO},
67+
{"==", CelOperator::EQUALS},
68+
{"!=", CelOperator::NOT_EQUALS},
69+
{">", CelOperator::GREATER},
70+
{">=", CelOperator::GREATER_EQUALS},
71+
{"<", CelOperator::LESS},
72+
{"<=", CelOperator::LESS_EQUALS},
73+
{"&&", CelOperator::LOGICAL_AND},
74+
{"!", CelOperator::LOGICAL_NOT},
75+
{"||", CelOperator::LOGICAL_OR},
76+
{"in", CelOperator::IN},
77+
};
7978
return *operators_map;
8079
}
8180

82-
const std::map<std::string, std::string>& Operators() {
83-
static std::shared_ptr<std::map<std::string, std::string>> operators_map =
84-
[&]() {
85-
auto c = std::make_shared<std::map<std::string, std::string>>(
86-
std::map<std::string, std::string>{
87-
{CelOperator::ADD, "+"},
88-
{CelOperator::SUBTRACT, "-"},
89-
{CelOperator::MULTIPLY, "*"},
90-
{CelOperator::DIVIDE, "/"},
91-
{CelOperator::MODULO, "%"},
92-
{CelOperator::EQUALS, "=="},
93-
{CelOperator::NOT_EQUALS, "!="},
94-
{CelOperator::GREATER, ">"},
95-
{CelOperator::GREATER_EQUALS, ">="},
96-
{CelOperator::LESS, "<"},
97-
{CelOperator::LESS_EQUALS, "<="},
98-
{CelOperator::LOGICAL_AND, "&&"},
99-
{CelOperator::LOGICAL_NOT, "!"},
100-
{CelOperator::LOGICAL_OR, "||"},
101-
{CelOperator::IN, "in"},
102-
{CelOperator::IN_DEPRECATED, "in"},
103-
{CelOperator::NEGATE, "-"}});
104-
return c;
105-
}();
81+
const absl::flat_hash_map<std::string, std::string>& Operators() {
82+
static auto* operators_map =
83+
new absl::flat_hash_map<std::string, std::string>{
84+
{CelOperator::ADD, "+"},
85+
{CelOperator::SUBTRACT, "-"},
86+
{CelOperator::MULTIPLY, "*"},
87+
{CelOperator::DIVIDE, "/"},
88+
{CelOperator::MODULO, "%"},
89+
{CelOperator::EQUALS, "=="},
90+
{CelOperator::NOT_EQUALS, "!="},
91+
{CelOperator::GREATER, ">"},
92+
{CelOperator::GREATER_EQUALS, ">="},
93+
{CelOperator::LESS, "<"},
94+
{CelOperator::LESS_EQUALS, "<="},
95+
{CelOperator::LOGICAL_AND, "&&"},
96+
{CelOperator::LOGICAL_NOT, "!"},
97+
{CelOperator::LOGICAL_OR, "||"},
98+
{CelOperator::IN, "in"},
99+
{CelOperator::IN_DEPRECATED, "in"},
100+
{CelOperator::NEGATE, "-"}};
106101
return *operators_map;
107102
}
108103

109104
// precedence of the operator, where the higher value means higher.
110-
const std::map<std::string, int>& Precedences() {
111-
static std::shared_ptr<std::map<std::string, int>> precedence_map = [&]() {
112-
auto c = std::make_shared<std::map<std::string, int>>(
113-
std::map<std::string, int>{{CelOperator::CONDITIONAL, 8},
105+
const absl::flat_hash_map<std::string, int>& Precedences() {
106+
static auto* precedence_map = new absl::flat_hash_map<std::string, int>{
107+
{CelOperator::CONDITIONAL, 8},
114108

115-
{CelOperator::LOGICAL_OR, 7},
109+
{CelOperator::LOGICAL_OR, 7},
116110

117-
{CelOperator::LOGICAL_AND, 6},
111+
{CelOperator::LOGICAL_AND, 6},
118112

119-
{CelOperator::EQUALS, 5},
120-
{CelOperator::GREATER, 5},
121-
{CelOperator::GREATER_EQUALS, 5},
122-
{CelOperator::IN, 5},
123-
{CelOperator::LESS, 5},
124-
{CelOperator::LESS_EQUALS, 5},
125-
{CelOperator::NOT_EQUALS, 5},
126-
{CelOperator::IN_DEPRECATED, 5},
113+
{CelOperator::EQUALS, 5},
114+
{CelOperator::GREATER, 5},
115+
{CelOperator::GREATER_EQUALS, 5},
116+
{CelOperator::IN, 5},
117+
{CelOperator::LESS, 5},
118+
{CelOperator::LESS_EQUALS, 5},
119+
{CelOperator::NOT_EQUALS, 5},
120+
{CelOperator::IN_DEPRECATED, 5},
127121

128-
{CelOperator::ADD, 4},
129-
{CelOperator::SUBTRACT, 4},
122+
{CelOperator::ADD, 4},
123+
{CelOperator::SUBTRACT, 4},
130124

131-
{CelOperator::DIVIDE, 3},
132-
{CelOperator::MODULO, 3},
133-
{CelOperator::MULTIPLY, 3},
125+
{CelOperator::DIVIDE, 3},
126+
{CelOperator::MODULO, 3},
127+
{CelOperator::MULTIPLY, 3},
134128

135-
{CelOperator::LOGICAL_NOT, 2},
136-
{CelOperator::NEGATE, 2},
129+
{CelOperator::LOGICAL_NOT, 2},
130+
{CelOperator::NEGATE, 2},
137131

138-
{CelOperator::INDEX, 1}});
139-
return c;
140-
}();
132+
{CelOperator::INDEX, 1}};
141133
return *precedence_map;
142134
}
143135

@@ -238,7 +230,4 @@ bool IsOperatorLeftRecursive(const std::string& op) {
238230
return op != CelOperator::LOGICAL_AND && op != CelOperator::LOGICAL_OR;
239231
}
240232

241-
} // namespace common
242-
} // namespace expr
243-
} // namespace api
244-
} // namespace google
233+
} // namespace google::api::expr::common

common/operators.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
// Copyright 2019 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
115
#ifndef THIRD_PARTY_CEL_CPP_COMMON_OPERATORS_H_
216
#define THIRD_PARTY_CEL_CPP_COMMON_OPERATORS_H_
317

@@ -8,10 +22,7 @@
822
#include "absl/strings/string_view.h"
923
#include "absl/types/optional.h"
1024

11-
namespace google {
12-
namespace api {
13-
namespace expr {
14-
namespace common {
25+
namespace google::api::expr::common {
1526

1627
// Operator function names.
1728
struct CelOperator {
@@ -71,9 +82,6 @@ bool IsOperatorSamePrecedence(const std::string& op,
7182
// return true if operator is left recursive, i.e., neither && nor ||.
7283
bool IsOperatorLeftRecursive(const std::string& op);
7384

74-
} // namespace common
75-
} // namespace expr
76-
} // namespace api
77-
} // namespace google
85+
} // namespace google::api::expr::common
7886

7987
#endif // THIRD_PARTY_CEL_CPP_COMMON_OPERATORS_H_

tools/BUILD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,49 @@ cc_test(
3131
],
3232
)
3333

34+
cc_library(
35+
name = "cel_unparser",
36+
srcs = [
37+
"cel_unparser.cc",
38+
],
39+
hdrs = [
40+
"cel_unparser.h",
41+
],
42+
deps = [
43+
"//common:operators",
44+
"//internal:status_macros",
45+
"//internal:strings",
46+
"@com_google_absl//absl/base:core_headers",
47+
"@com_google_absl//absl/base:no_destructor",
48+
"@com_google_absl//absl/container:flat_hash_set",
49+
"@com_google_absl//absl/status",
50+
"@com_google_absl//absl/status:statusor",
51+
"@com_google_absl//absl/strings",
52+
"@com_google_absl//absl/types:optional",
53+
"@com_google_cel_spec//proto/cel/expr:checked_cc_proto",
54+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
55+
"@com_google_protobuf//:duration_cc_proto",
56+
"@com_google_protobuf//:timestamp_cc_proto",
57+
"@com_googlesource_code_re2//:re2",
58+
],
59+
)
60+
61+
cc_test(
62+
name = "cel_unparser_test",
63+
srcs = ["cel_unparser_test.cc"],
64+
deps = [
65+
":cel_unparser",
66+
"//internal:proto_matchers",
67+
"//internal:testing",
68+
"//parser",
69+
"//parser:options",
70+
"@com_google_absl//absl/status",
71+
"@com_google_absl//absl/status:statusor",
72+
"@com_google_cel_spec//proto/cel/expr:syntax_cc_proto",
73+
"@com_google_protobuf//:protobuf",
74+
],
75+
)
76+
3477
cc_library(
3578
name = "flatbuffers_backed_impl",
3679
srcs = [

0 commit comments

Comments
 (0)