• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/service_config/service_config_parser.h"
18 
19 #include <grpc/support/port_platform.h>
20 #include <stdlib.h>
21 
22 #include <string>
23 
24 #include "absl/log/log.h"
25 
26 namespace grpc_core {
27 
Build()28 ServiceConfigParser ServiceConfigParser::Builder::Build() {
29   return ServiceConfigParser(std::move(registered_parsers_));
30 }
31 
RegisterParser(std::unique_ptr<Parser> parser)32 void ServiceConfigParser::Builder::RegisterParser(
33     std::unique_ptr<Parser> parser) {
34   for (const auto& registered_parser : registered_parsers_) {
35     if (registered_parser->name() == parser->name()) {
36       LOG(ERROR) << "Parser with name '" << parser->name()
37                  << "' already registered";
38       // We'll otherwise crash later.
39       abort();
40     }
41   }
42   registered_parsers_.emplace_back(std::move(parser));
43 }
44 
45 ServiceConfigParser::ParsedConfigVector
ParseGlobalParameters(const ChannelArgs & args,const Json & json,ValidationErrors * errors) const46 ServiceConfigParser::ParseGlobalParameters(const ChannelArgs& args,
47                                            const Json& json,
48                                            ValidationErrors* errors) const {
49   ParsedConfigVector parsed_global_configs;
50   for (auto& parser : registered_parsers_) {
51     parsed_global_configs.push_back(
52         parser->ParseGlobalParams(args, json, errors));
53   }
54   return parsed_global_configs;
55 }
56 
57 ServiceConfigParser::ParsedConfigVector
ParsePerMethodParameters(const ChannelArgs & args,const Json & json,ValidationErrors * errors) const58 ServiceConfigParser::ParsePerMethodParameters(const ChannelArgs& args,
59                                               const Json& json,
60                                               ValidationErrors* errors) const {
61   ParsedConfigVector parsed_method_configs;
62   for (auto& parser : registered_parsers_) {
63     parsed_method_configs.push_back(
64         parser->ParsePerMethodParams(args, json, errors));
65   }
66   return parsed_method_configs;
67 }
68 
GetParserIndex(absl::string_view name) const69 size_t ServiceConfigParser::GetParserIndex(absl::string_view name) const {
70   for (size_t i = 0; i < registered_parsers_.size(); ++i) {
71     if (registered_parsers_[i]->name() == name) return i;
72   }
73   return -1;
74 }
75 
76 }  // namespace grpc_core
77