1<%! 2 3 Copyright (c) Facebook, Inc. and its affiliates. 4 5 Licensed under the Apache License, Version 2.0 (the "License"); 6 you may not use this file except in compliance with the License. 7 You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11 Unless required by applicable law or agreed to in writing, software 12 distributed under the License is distributed on an "AS IS" BASIS, 13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 See the License for the specific language governing permissions and 15 limitations under the License. 16 17%><% > Autogen%> 18#include <thrift/lib/cpp2/gen/module_metadata_cpp.h> 19#include "<%program:include_prefix%><%program:name%>_metadata.h" 20 21namespace apache { 22namespace thrift { 23namespace detail { 24namespace md { 25using ThriftMetadata = ::apache::thrift::metadata::ThriftMetadata; 26using ThriftPrimitiveType = ::apache::thrift::metadata::ThriftPrimitiveType; 27using ThriftType = ::apache::thrift::metadata::ThriftType; 28using ThriftService = ::apache::thrift::metadata::ThriftService; 29using ThriftServiceContext = ::apache::thrift::metadata::ThriftServiceContext; 30using ThriftFunctionGenerator = void (*)(ThriftMetadata&, ThriftService&); 31 32<%#program:enums%> 33void EnumMetadata<<% > common/namespace_cpp2%><%enum:cpp_name%>>::gen(ThriftMetadata& metadata) { 34 auto res = metadata.enums_ref()->emplace("<%program:name%>.<%enum:name%>", ::apache::thrift::metadata::ThriftEnum{}); 35 if (!res.second) { 36 return; 37 } 38 ::apache::thrift::metadata::ThriftEnum& enum_metadata = res.first->second; 39 enum_metadata.name_ref() = "<%program:name%>.<%enum:name%>"; 40 using EnumTraits = TEnumTraits<<% > common/namespace_cpp2%><%enum:cpp_name%>>; 41 for (std::size_t i = 0; i < EnumTraits::size; ++i) { 42 enum_metadata.elements_ref()->emplace(static_cast<int32_t>(EnumTraits::values[i]), EnumTraits::names[i].str()); 43 } 44<%#enum:structured_annotations%> 45<%#structured_annotation:const_struct?%> 46 enum_metadata.structured_annotations_ref()->push_back(<% > metadata/const_struct%>); 47<%/structured_annotation:const_struct?%> 48<%/enum:structured_annotations%> 49} 50<%/program:enums%> 51 52<%#program:structs%> 53<%#program:unstructured_annotations_in_metadata?%> 54void StructMetadata<<% > common/namespace_cpp2%><%struct:name%>>::unstructured_annotations(::apache::thrift::metadata::ThriftStruct& thriftStruct){ 55 <%#struct:fields%> 56 thriftStruct.fields_ref()[<%field:index%>].unstructured_annotations_ref() = <% > metadata/unstructured_field_gen%>; 57 <%/struct:fields%> 58 <%^struct:fields%> 59 (void)thriftStruct; 60 <%/struct:fields%> 61} 62<%/program:unstructured_annotations_in_metadata?%> 63const ::apache::thrift::metadata::ThriftStruct& 64StructMetadata<<% > common/namespace_cpp2%><%struct:name%>>::gen(ThriftMetadata& metadata) { 65 auto res = metadata.structs_ref()->emplace("<%program:name%>.<%struct:name%>", ::apache::thrift::metadata::ThriftStruct{}); 66 if (!res.second) { 67 return res.first->second; 68 } 69 ::apache::thrift::metadata::ThriftStruct& <%struct:metadata_name%> = res.first->second; 70 <%struct:metadata_name%>.name_ref() = "<%program:name%>.<%struct:name%>"; 71 <%struct:metadata_name%>.is_union_ref() = <%struct:union?%>; 72<%#struct:fields?%> 73 static const EncodedThriftField 74 <%struct:metadata_name%>_fields[] = { 75<%#struct:fields%> 76 <% > metadata/field_gen%> 77<%/struct:fields%> 78 }; 79 for (const auto& f : <%struct:metadata_name%>_fields) { 80 ::apache::thrift::metadata::ThriftField field; 81 field.id_ref() = f.id; 82 field.name_ref() = f.name; 83 field.is_optional_ref() = f.is_optional; 84 f.metadata_type_interface->writeAndGenType(*field.type_ref(), metadata); 85 field.structured_annotations_ref() = f.structured_annotations; 86 <%struct:metadata_name%>.fields_ref()->push_back(std::move(field)); 87 } 88 <%#program:unstructured_annotations_in_metadata?%> 89 StructMetadata::unstructured_annotations(<%struct:metadata_name%>); 90 <%/program:unstructured_annotations_in_metadata?%> 91<%/struct:fields?%> 92<%#struct:structured_annotations%> 93<%#structured_annotation:const_struct?%> 94 <%struct:metadata_name%>.structured_annotations_ref()->push_back(<% > metadata/const_struct%>); 95<%/structured_annotation:const_struct?%> 96<%/struct:structured_annotations%> 97 return res.first->second; 98} 99<%/program:structs%> 100 101<%#program:structs%> 102<%#struct:exception?%> 103void ExceptionMetadata<<% > common/namespace_cpp2%><%struct:name%>>::gen(ThriftMetadata& metadata) { 104 auto res = metadata.exceptions_ref()->emplace("<%program:name%>.<%struct:name%>", ::apache::thrift::metadata::ThriftException{}); 105 if (!res.second) { 106 return; 107 } 108 ::apache::thrift::metadata::ThriftException& <%struct:metadata_name%> = res.first->second; 109 <%struct:metadata_name%>.name_ref() = "<%program:name%>.<%struct:name%>"; 110<%#struct:fields?%> 111 static const EncodedThriftField 112 <%struct:metadata_name%>_fields[] = { 113<%#struct:fields%> 114 <% > metadata/field_gen%> 115<%/struct:fields%> 116 }; 117 for (const auto& f : <%struct:metadata_name%>_fields) { 118 ::apache::thrift::metadata::ThriftField field; 119 field.id_ref() = f.id; 120 field.name_ref() = f.name; 121 field.is_optional_ref() = f.is_optional; 122 f.metadata_type_interface->writeAndGenType(*field.type_ref(), metadata); 123 <%struct:metadata_name%>.fields_ref()->push_back(std::move(field)); 124 } 125<%/struct:fields?%> 126<%#struct:structured_annotations%> 127<%#structured_annotation:const_struct?%> 128 <%struct:metadata_name%>.structured_annotations_ref()->push_back(<% > metadata/const_struct%>); 129<%/structured_annotation:const_struct?%> 130<%/struct:structured_annotations%> 131} 132<%/struct:exception?%> 133<%/program:structs%> 134<%#program:services%> 135<%#service:functions%><%^function:starts_interaction?%> 136void ServiceMetadata<::<% > common/service_namespace_cpp2%>::<%service:name%>SvIf>::gen_<%function:cpp_name%>(ThriftMetadata& metadata, ThriftService& service) { 137 ::apache::thrift::metadata::ThriftFunction func; 138 (void)metadata; 139 func.name_ref() = "<%function:name%>"; 140<%#function:return_type%> 141 auto func_ret_type = <% > metadata/type_gen%>; 142 func_ret_type->writeAndGenType(*func.return_type_ref(), metadata); 143<%/function:return_type%> 144<%#function:args%> 145 <% > metadata/func_field_gen%> 146func.arguments_ref()->push_back(std::move(<% > metadata/func_field_name%>)); 147<%/function:args%> 148<%#function:exceptions%> 149 <% > metadata/func_field_gen%> 150func.exceptions_ref()->push_back(std::move(<% > metadata/func_field_name%>)); 151<%#field:type%> 152<%#type:struct?%> 153 ExceptionMetadata<<% > common/type_namespace_cpp2%><%type:cpp_name%>>::gen(metadata); 154<%/type:struct?%> 155<%/field:type%> 156<%/function:exceptions%> 157 func.is_oneway_ref() = <%function:oneway?%>; 158<%#function:structured_annotations%> 159<%#structured_annotation:const_struct?%> 160 func.structured_annotations_ref()->push_back(<% > metadata/const_struct%>); 161<%/structured_annotation:const_struct?%> 162<%/function:structured_annotations%> 163 service.functions_ref()->push_back(std::move(func)); 164} 165<%/function:starts_interaction?%><%/service:functions%> 166 167void ServiceMetadata<::<% > common/service_namespace_cpp2%>::<%service:name%>SvIf>::gen(::apache::thrift::metadata::ThriftServiceMetadataResponse& response) { 168 const ::apache::thrift::metadata::ThriftServiceContextRef* self = genRecurse(*response.metadata_ref(), *response.services_ref()); 169 DCHECK(self != nullptr); 170 // TODO(praihan): Remove ThriftServiceContext from response. But in the meantime, we need to fill the field with the result of looking up in ThriftMetadata. 171 ::apache::thrift::metadata::ThriftServiceContext context; 172 context.module_ref() = *self->module_ref(); 173 context.service_info_ref() = response.metadata_ref()->services_ref()->at(*self->service_name_ref()); 174 response.context_ref() = std::move(context); 175} 176 177const ThriftServiceContextRef* ServiceMetadata<::<% > common/service_namespace_cpp2%>::<%service:name%>SvIf>::genRecurse(ThriftMetadata& metadata, std::vector<ThriftServiceContextRef>& services) { 178 (void) metadata; 179 ::apache::thrift::metadata::ThriftService <%service:metadata_name%>; 180 <%service:metadata_name%>.name_ref() = "<%program:name%>.<%service:name%>"; 181<%#service:functions?%> 182 static const ThriftFunctionGenerator functions[] = { 183<%#service:functions%><%^function:starts_interaction?%> 184 ServiceMetadata<::<% > common/service_namespace_cpp2%>::<%service:name%>SvIf>::gen_<%function:cpp_name%>, 185<%/function:starts_interaction?%><%/service:functions%> 186 }; 187 for (auto& function_gen : functions) { 188 function_gen(metadata, <%service:metadata_name%>); 189 } 190<%/service:functions?%> 191 // We need to keep the index around because a reference or iterator could be invalidated. 192 auto selfIndex = services.size(); 193 services.emplace_back(); 194<%#service:extends?%> 195 <%service:metadata_name%>.parent_ref() = "<%#service:extends%><%service:program_name%>.<%service:name%><%/service:extends%>"; 196<%#service:extends%> 197 ServiceMetadata<::<% > common/service_namespace_cpp2%>::<%service:name%>SvIf>::genRecurse(metadata, services); 198<%/service:extends%> 199<%/service:extends?%> 200<%#service:structured_annotations%> 201<%#structured_annotation:const_struct?%> 202 <%service:metadata_name%>.structured_annotations_ref()->push_back(<% > metadata/const_struct%>); 203<%/structured_annotation:const_struct?%> 204<%/service:structured_annotations%> 205 ThriftServiceContextRef& context = services[selfIndex]; 206 metadata.services_ref()->emplace("<%program:name%>.<%service:name%>", std::move(<%service:metadata_name%>)); 207 context.service_name_ref() = "<%program:name%>.<%service:name%>"; 208 ::apache::thrift::metadata::ThriftModuleContext module; 209 module.name_ref() = "<%program:name%>"; 210 context.module_ref() = std::move(module); 211 return &context; 212} 213<%/program:services%> 214} // namespace md 215} // namespace detail 216} // namespace thrift 217} // namespace apache 218