diff --git a/ttrpc-codegen/Cargo.toml b/ttrpc-codegen/Cargo.toml index 43c30de..89a43f6 100644 --- a/ttrpc-codegen/Cargo.toml +++ b/ttrpc-codegen/Cargo.toml @@ -14,6 +14,6 @@ readme = "README.md" [dependencies] protobuf-support = "3.2.0" -protobuf = { version = "2.27.1" } +protobuf = { version = "3.5.1" } protobuf-codegen = "3.2.0" ttrpc-compiler = "0.6.1" diff --git a/ttrpc-codegen/src/convert.rs b/ttrpc-codegen/src/convert.rs index e819caf..e6e5338 100644 --- a/ttrpc-codegen/src/convert.rs +++ b/ttrpc-codegen/src/convert.rs @@ -56,10 +56,10 @@ enum MessageOrEnum { } impl MessageOrEnum { - fn descriptor_type(&self) -> protobuf::descriptor::FieldDescriptorProto_Type { + fn descriptor_type(&self) -> protobuf::descriptor::field_descriptor_proto::Type { match *self { - MessageOrEnum::Message => protobuf::descriptor::FieldDescriptorProto_Type::TYPE_MESSAGE, - MessageOrEnum::Enum => protobuf::descriptor::FieldDescriptorProto_Type::TYPE_ENUM, + MessageOrEnum::Message => protobuf::descriptor::field_descriptor_proto::Type::TYPE_MESSAGE, + MessageOrEnum::Enum => protobuf::descriptor::field_descriptor_proto::Type::TYPE_ENUM, } } } @@ -407,7 +407,7 @@ impl<'a> Resolver<'a> { output.set_number(number); let (t, t_name) = self.field_type(name, field_type, path_in_file); - output.set_field_type(t); + output.set_type(t); if let Some(t_name) = t_name { output.set_type_name(t_name.path); } @@ -424,14 +424,13 @@ impl<'a> Resolver<'a> { ) -> ConvertResult { let mut output = protobuf::descriptor::DescriptorProto::new(); - output.mut_options().set_map_entry(true); + output.options = Some(protobuf::descriptor::MessageOptions { + map_entry: Some(true), + ..Default::default() + }).into(); output.set_name(Resolver::map_entry_name_for_field_name(field_name)); - output - .mut_field() - .push(self.map_entry_field("key", 1, key, path_in_file)); - output - .mut_field() - .push(self.map_entry_field("value", 2, value, path_in_file)); + output.field.push(self.map_entry_field("key", 1, key, path_in_file)); + output.field.push(self.map_entry_field("value", 2, value, path_in_file)); Ok(output) } @@ -459,7 +458,7 @@ impl<'a> Resolver<'a> { let mut output = protobuf::descriptor::DescriptorProto::new(); output.set_name(input.name.clone()); - let mut nested_messages = protobuf::RepeatedField::new(); + let mut nested_messages = vec![]; for m in &input.messages { nested_messages.push(self.message(m, &nested_path_in_file)?); @@ -471,18 +470,17 @@ impl<'a> Resolver<'a> { } } - output.set_nested_type(nested_messages); + output.nested_type = nested_messages; - output.set_enum_type( + output.enum_type = input .enums .iter() .map(|e| self.enumeration(e)) - .collect::>()?, - ); + .collect::>()?; { - let mut fields = protobuf::RepeatedField::new(); + let mut fields = vec![]; for f in &input.fields { fields.push(self.field(f, None, &nested_path_in_file)?); @@ -495,13 +493,13 @@ impl<'a> Resolver<'a> { } } - output.set_field(fields); + output.field = fields; } let oneofs = input.oneofs.iter().map(|o| self.oneof(o)).collect(); - output.set_oneof_decl(oneofs); + output.oneof_decl = oneofs; - output.set_options(self.message_options(&input.options)?); + output.options = Some(self.message_options(&input.options)?).into(); Ok(output) } @@ -540,7 +538,7 @@ impl<'a> Resolver<'a> { let mut output = protobuf::descriptor::ServiceDescriptorProto::new(); output.set_name(input.name.clone()); - let mut methods = protobuf::RepeatedField::new(); + let mut methods = Vec::new(); for m in &input.methods { let mut mm = protobuf::descriptor::MethodDescriptorProto::new(); mm.set_name(m.name.clone()); @@ -550,13 +548,13 @@ impl<'a> Resolver<'a> { mm.set_client_streaming(m.client_streaming); mm.set_server_streaming(m.server_streaming); - mm.set_options(self.method_options(&m.options)?); + mm.options = Some(self.method_options(&m.options)?).into(); methods.push(mm); } - output.set_method(methods); - output.set_options(self.service_options(&input.options)?); + output.method = methods; + output.options = Some(self.service_options(&input.options)?).into(); Ok(output) } @@ -631,28 +629,29 @@ impl<'a> Resolver<'a> { output.set_name(input.name.clone()); if let model::FieldType::Map(..) = input.typ { - output.set_label(protobuf::descriptor::FieldDescriptorProto_Label::LABEL_REPEATED); + output.set_label(protobuf::descriptor::field_descriptor_proto::Label::LABEL_REPEATED); } else { output.set_label(label(input.rule)); } let (t, t_name) = self.field_type(&input.name, &input.typ, path_in_file); - output.set_field_type(t); + output.set_type(t); if let Some(t_name) = t_name { output.set_type_name(t_name.path); } - - output.set_number(input.number); + output.number = Some(input.number); if let Some(default) = input.options.as_slice().by_name("default") { - let default = match output.get_field_type() { - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_STRING => { + let string_known = protobuf::EnumOrUnknown::new(protobuf::descriptor::field_descriptor_proto::Type::TYPE_STRING); + let bytes_known = protobuf::EnumOrUnknown::new(protobuf::descriptor::field_descriptor_proto::Type::TYPE_BYTES); + let default = match output.type_ { + Some(string_knowns) => { if let model::ProtobufConstant::String(ref s) = *default { s.decode_utf8()? } else { return Err(ConvertError::DefaultValueIsNotStringLiteral); } } - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_BYTES => { + Some(bytes_known) => { if let model::ProtobufConstant::String(ref s) = *default { s.escaped.clone() } else { @@ -664,7 +663,7 @@ impl<'a> Resolver<'a> { output.set_default_value(default); } - output.set_options(self.field_options(&input.options)?); + output.options = Some(self.field_options(&input.options)?).into(); if let Some(oneof_index) = oneof_index { output.set_oneof_index(oneof_index); @@ -736,68 +735,68 @@ impl<'a> Resolver<'a> { input: &model::FieldType, path_in_file: &RelativePath, ) -> ( - protobuf::descriptor::FieldDescriptorProto_Type, + protobuf::descriptor::field_descriptor_proto::Type, Option, ) { match *input { model::FieldType::Bool => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_BOOL, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_BOOL, None, ), model::FieldType::Int32 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_INT32, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_INT32, None, ), model::FieldType::Int64 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_INT64, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_INT64, None, ), model::FieldType::Uint32 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_UINT32, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_UINT32, None, ), model::FieldType::Uint64 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_UINT64, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_UINT64, None, ), model::FieldType::Sint32 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_SINT32, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_SINT32, None, ), model::FieldType::Sint64 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_SINT64, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_SINT64, None, ), model::FieldType::Fixed32 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_FIXED32, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_FIXED32, None, ), model::FieldType::Fixed64 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_FIXED64, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_FIXED64, None, ), model::FieldType::Sfixed32 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_SFIXED32, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_SFIXED32, None, ), model::FieldType::Sfixed64 => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_SFIXED64, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_SFIXED64, None, ), model::FieldType::Float => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_FLOAT, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_FLOAT, None, ), model::FieldType::Double => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_DOUBLE, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_DOUBLE, None, ), model::FieldType::String => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_STRING, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_STRING, None, ), model::FieldType::Bytes => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_BYTES, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_BYTES, None, ), model::FieldType::MessageOrEnum(ref name) => { @@ -809,12 +808,12 @@ impl<'a> Resolver<'a> { type_name.push_relative(path_in_file); type_name.push_simple(&Resolver::map_entry_name_for_field_name(name)); ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_MESSAGE, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_MESSAGE, Some(type_name), ) } model::FieldType::Group(..) => ( - protobuf::descriptor::FieldDescriptorProto_Type::TYPE_GROUP, + protobuf::descriptor::field_descriptor_proto::Type::TYPE_GROUP, None, ), } @@ -852,14 +851,12 @@ impl<'a> Resolver<'a> { ) -> ConvertResult { let mut output = protobuf::descriptor::EnumDescriptorProto::new(); output.set_name(input.name.clone()); - output.set_value( - input + output.value = input .values .iter() .map(|v| self.enum_value(&v.name, v.number)) - .collect(), - ); - output.set_options(self.enum_options(&input.options)?); + .collect(); + output.options = Some(self.enum_options(&input.options)?).into(); Ok(output) } @@ -1008,11 +1005,11 @@ fn syntax(input: model::Syntax) -> String { } } -fn label(input: model::Rule) -> protobuf::descriptor::FieldDescriptorProto_Label { +fn label(input: model::Rule) -> protobuf::descriptor::field_descriptor_proto::Label { match input { - model::Rule::Optional => protobuf::descriptor::FieldDescriptorProto_Label::LABEL_OPTIONAL, - model::Rule::Required => protobuf::descriptor::FieldDescriptorProto_Label::LABEL_REQUIRED, - model::Rule::Repeated => protobuf::descriptor::FieldDescriptorProto_Label::LABEL_REPEATED, + model::Rule::Optional => protobuf::descriptor::field_descriptor_proto::Label::LABEL_OPTIONAL, + model::Rule::Required => protobuf::descriptor::field_descriptor_proto::Label::LABEL_REQUIRED, + model::Rule::Repeated => protobuf::descriptor::field_descriptor_proto::Label::LABEL_REPEATED, } } @@ -1031,35 +1028,32 @@ pub fn file_descriptor( output.set_package(input.package.clone()); output.set_syntax(syntax(input.syntax)); - let mut messages = protobuf::RepeatedField::new(); + let mut messages = Vec::new(); for m in &input.messages { messages.push(resolver.message(m, &RelativePath::empty())?); } - output.set_message_type(messages); + output.message_type = messages; - let mut services = protobuf::RepeatedField::new(); + let mut services = Vec::new(); for s in &input.services { services.push(resolver.service(s, &input.package)?); } - output.set_service(services); + output.service = services; - output.set_enum_type( - input + output.enum_type = input .enums .iter() .map(|e| resolver.enumeration(e)) - .collect::>()?, - ); - - output.set_options(resolver.file_options(&input.options)?); + .collect::>()?; + output.options = Some(resolver.file_options(&input.options)?).into(); - let mut extensions = protobuf::RepeatedField::new(); + let mut extensions = Vec::new(); for e in &input.extensions { extensions.push(resolver.extension(e)?); } - output.set_extension(extensions); + output.extension = extensions; Ok(output) }