Skip to content

Commit d900ee3

Browse files
committed
Add path_matcher_utility for registering HttpRule
1 parent 8c108fc commit d900ee3

File tree

5 files changed

+335
-30
lines changed

5 files changed

+335
-30
lines changed

repositories.bzl

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,22 @@ def googleapis_repositories(protobuf_repo="@protobuf_git//", bind=True):
159159
160160
licenses(["notice"])
161161
162-
load("@protobuf_git//:protobuf.bzl", "cc_proto_library")
162+
load("{0}:protobuf.bzl", "cc_proto_library")
163163
164164
exports_files(glob(["google/**"]))
165165
166+
cc_proto_library(
167+
name = "http_api_protos",
168+
srcs = [
169+
"google/api/annotations.proto",
170+
"google/api/http.proto",
171+
],
172+
default_runtime = "//external:protobuf",
173+
protoc = "//external:protoc",
174+
visibility = ["//visibility:public"],
175+
deps = ["{0}:cc_wkt_protos"],
176+
)
177+
166178
cc_proto_library(
167179
name = "servicecontrol",
168180
srcs = [
@@ -190,7 +202,6 @@ cc_proto_library(
190202
cc_proto_library(
191203
name = "service_config",
192204
srcs = [
193-
"google/api/annotations.proto",
194205
"google/api/auth.proto",
195206
"google/api/backend.proto",
196207
"google/api/billing.proto",
@@ -199,7 +210,6 @@ cc_proto_library(
199210
"google/api/control.proto",
200211
"google/api/documentation.proto",
201212
"google/api/endpoint.proto",
202-
"google/api/http.proto",
203213
"google/api/label.proto",
204214
"google/api/log.proto",
205215
"google/api/logging.proto",
@@ -217,26 +227,12 @@ cc_proto_library(
217227
include = ".",
218228
visibility = ["//visibility:public"],
219229
deps = [
230+
":http_api_protos",
220231
"//external:cc_wkt_protos",
221232
],
222233
protoc = "//external:protoc",
223234
default_runtime = "//external:protobuf",
224235
)
225-
226-
cc_proto_library(
227-
name = "cloud_trace",
228-
srcs = [
229-
"google/devtools/cloudtrace/v1/trace.proto",
230-
],
231-
include = ".",
232-
default_runtime = "//external:protobuf",
233-
protoc = "//external:protoc",
234-
visibility = ["//visibility:public"],
235-
deps = [
236-
":service_config",
237-
"//external:cc_wkt_protos",
238-
],
239-
)
240236
""".format(protobuf_repo)
241237

242238
native.new_git_repository(
@@ -247,22 +243,12 @@ cc_proto_library(
247243
)
248244

249245
if bind:
250-
native.bind(
251-
name = "servicecontrol",
252-
actual = "@googleapis_git//:servicecontrol",
253-
)
254-
255-
native.bind(
256-
name = "servicecontrol_genproto",
257-
actual = "@googleapis_git//:servicecontrol_genproto",
258-
)
259-
260246
native.bind(
261247
name = "service_config",
262248
actual = "@googleapis_git//:service_config",
263249
)
264250

265251
native.bind(
266-
name = "cloud_trace",
267-
actual = "@googleapis_git//:cloud_trace",
252+
name = "http_api_protos",
253+
actual = "@googleapis_git//:http_api_protos",
268254
)

src/BUILD

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,20 @@ cc_library(
131131
],
132132
)
133133

134+
cc_library(
135+
name = "path_matcher_utility",
136+
hdrs = [
137+
"include/grpc_transcoding/path_matcher_utility.h",
138+
],
139+
includes = [
140+
"include/"
141+
],
142+
deps = [
143+
":path_matcher",
144+
"//external:http_api_protos",
145+
],
146+
)
147+
134148
cc_library(
135149
name = "json_request_translator",
136150
srcs = [
@@ -208,6 +222,7 @@ cc_library(
208222
deps = [
209223
":json_request_translator",
210224
":message_stream",
225+
":path_matcher_utility",
211226
":response_to_json_translator",
212227
":type_helper",
213228
"//external:protobuf",
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright 2017 Google Inc. All Rights Reserved.
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+
* http://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+
*/
15+
16+
#ifndef GRPC_HTTPJSON_TRANSCODING_PATH_MATCHER_UTILITY_H
17+
#define GRPC_HTTPJSON_TRANSCODING_PATH_MATCHER_UTILITY_H
18+
19+
#include "google/api/http.pb.h"
20+
#include "path_matcher.h"
21+
22+
namespace google {
23+
namespace grpc {
24+
namespace transcoding {
25+
26+
class PathMatcherUtility {
27+
public:
28+
template <class Method>
29+
static bool RegisterByHttpRule(
30+
PathMatcherBuilder<Method> &pmb, const google::api::HttpRule &http_rule,
31+
const std::set<std::string> &system_query_parameter_names,
32+
const Method &method);
33+
34+
template <class Method>
35+
static bool RegisterByHttpRule(PathMatcherBuilder<Method> &pmb,
36+
const google::api::HttpRule &http_rule,
37+
const Method &method) {
38+
return RegisterByHttpRule(pmb, http_rule, {}, method);
39+
}
40+
};
41+
42+
template <class Method>
43+
bool PathMatcherUtility::RegisterByHttpRule(
44+
PathMatcherBuilder<Method> &pmb, const google::api::HttpRule &http_rule,
45+
const std::set<std::string> &system_query_parameter_names,
46+
const Method &method) {
47+
bool ok = true;
48+
switch (http_rule.pattern_case()) {
49+
case ::google::api::HttpRule::kGet:
50+
ok = pmb.Register("GET", http_rule.get(), http_rule.body(),
51+
system_query_parameter_names, method);
52+
break;
53+
case ::google::api::HttpRule::kPut:
54+
ok = pmb.Register("PUT", http_rule.put(), http_rule.body(),
55+
system_query_parameter_names, method);
56+
break;
57+
case ::google::api::HttpRule::kPost:
58+
ok = pmb.Register("POST", http_rule.post(), http_rule.body(),
59+
system_query_parameter_names, method);
60+
break;
61+
case ::google::api::HttpRule::kDelete:
62+
ok = pmb.Register("DELETE", http_rule.delete_(), http_rule.body(),
63+
system_query_parameter_names, method);
64+
break;
65+
case ::google::api::HttpRule::kPatch:
66+
ok = pmb.Register("PATCH", http_rule.patch(), http_rule.body(),
67+
system_query_parameter_names, method);
68+
break;
69+
case ::google::api::HttpRule::kCustom:
70+
ok = pmb.Register(http_rule.custom().kind(), http_rule.custom().path(),
71+
http_rule.body(), system_query_parameter_names, method);
72+
break;
73+
default: // ::google::api::HttpRule::PATTEN_NOT_SET
74+
break;
75+
}
76+
77+
for (const auto &additional_binding : http_rule.additional_bindings()) {
78+
if (!ok) {
79+
return ok;
80+
}
81+
ok = RegisterByHttpRule(pmb, additional_binding,
82+
system_query_parameter_names, method);
83+
}
84+
85+
return ok;
86+
}
87+
88+
} // namespace transcoding
89+
} // namespace grpc
90+
} // namespace google
91+
92+
#endif // GRPC_HTTPJSON_TRANSCODING_PATH_MATCHER_UTILITY_H

test/BUILD

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ cc_test(
4444
],
4545
)
4646

47+
cc_test(
48+
name = "path_matcher_utility_test",
49+
size = "small",
50+
srcs = [
51+
"path_matcher_utility_test.cc",
52+
],
53+
linkstatic = 1,
54+
deps = [
55+
"//external:googletest_main",
56+
"//src:path_matcher_utility",
57+
],
58+
)
59+
4760
cc_test(
4861
name = "prefix_writer_test",
4962
size = "small",

0 commit comments

Comments
 (0)