1 //
2 // Copyright 2015 gRPC authors.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include <grpc/support/port_platform.h>
18
19 #include "src/core/lib/service_config/service_config_parser.h"
20
21 #include <stdlib.h>
22
23 #include <string>
24
25 #include "absl/strings/str_cat.h"
26
27 #include <grpc/support/log.h>
28
29 namespace grpc_core {
30
Build()31 ServiceConfigParser ServiceConfigParser::Builder::Build() {
32 return ServiceConfigParser(std::move(registered_parsers_));
33 }
34
RegisterParser(std::unique_ptr<Parser> parser)35 void ServiceConfigParser::Builder::RegisterParser(
36 std::unique_ptr<Parser> parser) {
37 for (const auto& registered_parser : registered_parsers_) {
38 if (registered_parser->name() == parser->name()) {
39 gpr_log(GPR_ERROR, "%s",
40 absl::StrCat("Parser with name '", parser->name(),
41 "' already registered")
42 .c_str());
43 // We'll otherwise crash later.
44 abort();
45 }
46 }
47 registered_parsers_.emplace_back(std::move(parser));
48 }
49
50 ServiceConfigParser::ParsedConfigVector
ParseGlobalParameters(const ChannelArgs & args,const Json & json,ValidationErrors * errors) const51 ServiceConfigParser::ParseGlobalParameters(const ChannelArgs& args,
52 const Json& json,
53 ValidationErrors* errors) const {
54 ParsedConfigVector parsed_global_configs;
55 for (auto& parser : registered_parsers_) {
56 parsed_global_configs.push_back(
57 parser->ParseGlobalParams(args, json, errors));
58 }
59 return parsed_global_configs;
60 }
61
62 ServiceConfigParser::ParsedConfigVector
ParsePerMethodParameters(const ChannelArgs & args,const Json & json,ValidationErrors * errors) const63 ServiceConfigParser::ParsePerMethodParameters(const ChannelArgs& args,
64 const Json& json,
65 ValidationErrors* errors) const {
66 ParsedConfigVector parsed_method_configs;
67 for (auto& parser : registered_parsers_) {
68 parsed_method_configs.push_back(
69 parser->ParsePerMethodParams(args, json, errors));
70 }
71 return parsed_method_configs;
72 }
73
GetParserIndex(absl::string_view name) const74 size_t ServiceConfigParser::GetParserIndex(absl::string_view name) const {
75 for (size_t i = 0; i < registered_parsers_.size(); ++i) {
76 if (registered_parsers_[i]->name() == name) return i;
77 }
78 return -1;
79 }
80
81 } // namespace grpc_core
82