1616#include " google/cloud/internal/absl_str_cat_quiet.h"
1717#include " google/cloud/internal/absl_str_join_quiet.h"
1818#include " google/cloud/internal/absl_str_replace_quiet.h"
19+ #include " absl/container/flat_hash_set.h"
1920#include " absl/strings/str_split.h"
2021#include " absl/time/clock.h"
2122#include " absl/time/time.h"
@@ -32,6 +33,81 @@ std::vector<std::pair<std::string, std::string>> const& SnakeCaseExceptions() {
3233 {" big_query" , " bigquery" }};
3334 return kExceptions ;
3435}
36+
37+ Status ProcessArgProductPath (
38+ std::vector<std::pair<std::string, std::string>>& command_line_args) {
39+ auto product_path =
40+ std::find_if (command_line_args.begin (), command_line_args.end (),
41+ [](std::pair<std::string, std::string> const & p) {
42+ return p.first == " product_path" ;
43+ });
44+ if (product_path == command_line_args.end () || product_path->second .empty ()) {
45+ return Status (StatusCode::kInvalidArgument ,
46+ " --cpp_codegen_opt=product_path=<path> must be specified." );
47+ }
48+
49+ auto & path = product_path->second ;
50+ if (path.front () == ' /' ) {
51+ path = path.substr (1 );
52+ }
53+ if (path.back () != ' /' ) {
54+ path += ' /' ;
55+ }
56+ return {};
57+ }
58+
59+ Status ProcessArgGoogleapisCommitHash (
60+ std::vector<std::pair<std::string, std::string>>& command_line_args) {
61+ auto googleapis_commit_hash =
62+ std::find_if (command_line_args.begin (), command_line_args.end (),
63+ [](std::pair<std::string, std::string> const & p) {
64+ return p.first == " googleapis_commit_hash" ;
65+ });
66+ if (googleapis_commit_hash == command_line_args.end () ||
67+ googleapis_commit_hash->second .empty ()) {
68+ return Status (
69+ StatusCode::kInvalidArgument ,
70+ " --cpp_codegen_opt=googleapis_commit_hash=<hash> must be specified." );
71+ }
72+ return {};
73+ }
74+
75+ void ProcessArgCopyrightYear (
76+ std::vector<std::pair<std::string, std::string>>& command_line_args) {
77+ auto copyright_year =
78+ std::find_if (command_line_args.begin (), command_line_args.end (),
79+ [](std::pair<std::string, std::string> const & p) {
80+ return p.first == " copyright_year" ;
81+ });
82+ if (copyright_year == command_line_args.end ()) {
83+ command_line_args.emplace_back (" copyright_year" , CurrentCopyrightYear ());
84+ } else if (copyright_year->second .empty ()) {
85+ copyright_year->second = CurrentCopyrightYear ();
86+ }
87+ }
88+
89+ void ProcessArgOmitRpc (
90+ std::vector<std::pair<std::string, std::string>>& command_line_args) {
91+ absl::flat_hash_set<std::string> omitted_rpcs;
92+ auto iter = std::find_if (command_line_args.begin (), command_line_args.end (),
93+ [](std::pair<std::string, std::string> const & p) {
94+ return p.first == " omit_rpc" ;
95+ });
96+ while (iter != command_line_args.end ()) {
97+ omitted_rpcs.insert (iter->second );
98+ command_line_args.erase (iter);
99+ iter = std::find_if (command_line_args.begin (), command_line_args.end (),
100+ [](std::pair<std::string, std::string> const & p) {
101+ return p.first == " omit_rpc" ;
102+ });
103+ }
104+ if (!omitted_rpcs.empty ()) {
105+ command_line_args.emplace_back (
106+ " omitted_rpcs" ,
107+ absl::StrJoin (omitted_rpcs.begin (), omitted_rpcs.end (), " ," ));
108+ }
109+ }
110+
35111} // namespace
36112std::string CurrentCopyrightYear () {
37113 static std::string const kCurrentCopyrightYear =
@@ -115,48 +191,14 @@ ProcessCommandLineArgs(std::string const& parameters) {
115191 std::vector<std::pair<std::string, std::string>> command_line_args;
116192 google::protobuf::compiler::ParseGeneratorParameter (parameters,
117193 &command_line_args);
194+ auto status = ProcessArgProductPath (command_line_args);
195+ if (!status.ok ()) return status;
118196
119- auto product_path =
120- std::find_if (command_line_args.begin (), command_line_args.end (),
121- [](std::pair<std::string, std::string> const & p) {
122- return p.first == " product_path" ;
123- });
124- if (product_path == command_line_args.end () || product_path->second .empty ()) {
125- return Status (StatusCode::kInvalidArgument ,
126- " --cpp_codegen_opt=product_path=<path> must be specified." );
127- }
128-
129- auto & path = product_path->second ;
130- if (path.front () == ' /' ) {
131- path = path.substr (1 );
132- }
133- if (path.back () != ' /' ) {
134- path += ' /' ;
135- }
136-
137- auto googleapis_commit_hash =
138- std::find_if (command_line_args.begin (), command_line_args.end (),
139- [](std::pair<std::string, std::string> const & p) {
140- return p.first == " googleapis_commit_hash" ;
141- });
142- if (googleapis_commit_hash == command_line_args.end () ||
143- googleapis_commit_hash->second .empty ()) {
144- return Status (
145- StatusCode::kInvalidArgument ,
146- " --cpp_codegen_opt=googleapis_commit_hash=<hash> must be specified." );
147- }
148-
149- auto copyright_year =
150- std::find_if (command_line_args.begin (), command_line_args.end (),
151- [](std::pair<std::string, std::string> const & p) {
152- return p.first == " copyright_year" ;
153- });
154- if (copyright_year == command_line_args.end ()) {
155- command_line_args.emplace_back (" copyright_year" , CurrentCopyrightYear ());
156- } else if (copyright_year->second .empty ()) {
157- copyright_year->second = CurrentCopyrightYear ();
158- }
197+ status = ProcessArgGoogleapisCommitHash (command_line_args);
198+ if (!status.ok ()) return status;
159199
200+ ProcessArgCopyrightYear (command_line_args);
201+ ProcessArgOmitRpc (command_line_args);
160202 return command_line_args;
161203}
162204
0 commit comments