1 // Copyright 2020 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
15 #include "generator/internal/predicate_utils.h"
16 #include "google/cloud/log.h"
17 #include "google/cloud/optional.h"
18 #include <google/api/client.pb.h>
19 #include <google/longrunning/operations.pb.h>
20 #include <string>
21
22 namespace google {
23 namespace cloud {
24 namespace generator_internal {
25
26 using ::google::protobuf::Descriptor;
27 using ::google::protobuf::FieldDescriptor;
28 using ::google::protobuf::MethodDescriptor;
29
30 // https://google.aip.dev/client-libraries/4233
31 google::cloud::optional<std::pair<std::string, std::string>>
DeterminePagination(google::protobuf::MethodDescriptor const & method)32 DeterminePagination(google::protobuf::MethodDescriptor const& method) {
33 std::string paginated_type;
34 Descriptor const* request_message = method.input_type();
35 FieldDescriptor const* page_size =
36 request_message->FindFieldByName("page_size");
37 if (!page_size || page_size->type() != FieldDescriptor::TYPE_INT32) return {};
38 FieldDescriptor const* page_token =
39 request_message->FindFieldByName("page_token");
40 if (!page_token || page_token->type() != FieldDescriptor::TYPE_STRING)
41 return {};
42
43 Descriptor const* response_message = method.output_type();
44 FieldDescriptor const* next_page_token =
45 response_message->FindFieldByName("next_page_token");
46 if (!next_page_token ||
47 next_page_token->type() != FieldDescriptor::TYPE_STRING)
48 return {};
49
50 std::vector<std::tuple<std::string, Descriptor const*, int>>
51 repeated_message_fields;
52 for (int i = 0; i < response_message->field_count(); ++i) {
53 FieldDescriptor const* field = response_message->field(i);
54 if (field->is_repeated() &&
55 field->type() == FieldDescriptor::TYPE_MESSAGE) {
56 repeated_message_fields.emplace_back(std::make_tuple(
57 field->name(), field->message_type(), field->number()));
58 }
59 }
60
61 if (repeated_message_fields.empty()) return {};
62
63 if (repeated_message_fields.size() > 1) {
64 auto min_field = std::min_element(
65 repeated_message_fields.begin(), repeated_message_fields.end(),
66 [](std::tuple<std::string, Descriptor const*, int> const& lhs,
67 std::tuple<std::string, Descriptor const*, int> const& rhs) {
68 return std::get<2>(lhs) < std::get<2>(rhs);
69 });
70 int min_field_number = std::get<2>(*min_field);
71 if (min_field_number != std::get<2>(repeated_message_fields[0])) {
72 GCP_LOG(FATAL) << "Repeated field in paginated response must be first "
73 "appearing and lowest field number: "
74 << method.full_name() << std::endl;
75 std::exit(1);
76 }
77 }
78 return std::make_pair(std::get<0>(repeated_message_fields[0]),
79 std::get<1>(repeated_message_fields[0])->full_name());
80 }
81
IsPaginated(google::protobuf::MethodDescriptor const & method)82 bool IsPaginated(google::protobuf::MethodDescriptor const& method) {
83 return DeterminePagination(method).has_value();
84 }
85
IsNonStreaming(google::protobuf::MethodDescriptor const & method)86 bool IsNonStreaming(google::protobuf::MethodDescriptor const& method) {
87 return !method.client_streaming() && !method.server_streaming();
88 }
89
IsLongrunningOperation(google::protobuf::MethodDescriptor const & method)90 bool IsLongrunningOperation(google::protobuf::MethodDescriptor const& method) {
91 return method.output_type()->full_name() == "google.longrunning.Operation";
92 }
93
IsResponseTypeEmpty(google::protobuf::MethodDescriptor const & method)94 bool IsResponseTypeEmpty(google::protobuf::MethodDescriptor const& method) {
95 return method.output_type()->full_name() == "google.protobuf.Empty";
96 }
97
IsLongrunningMetadataTypeUsedAsResponse(google::protobuf::MethodDescriptor const & method)98 bool IsLongrunningMetadataTypeUsedAsResponse(
99 google::protobuf::MethodDescriptor const& method) {
100 if (method.output_type()->full_name() == "google.longrunning.Operation") {
101 auto operation_info =
102 method.options().GetExtension(google::longrunning::operation_info);
103 return operation_info.response_type() == "google.protobuf.Empty";
104 }
105 return false;
106 }
107
108 } // namespace generator_internal
109 } // namespace cloud
110 } // namespace google
111