diff --git a/pilota-build/src/middle/context.rs b/pilota-build/src/middle/context.rs index e678065a..bfdcddc6 100644 --- a/pilota-build/src/middle/context.rs +++ b/pilota-build/src/middle/context.rs @@ -142,19 +142,18 @@ impl Context { } pub fn item_path(&self, def_id: DefId) -> Arc<[smol_str::SmolStr]> { - fn calc_item_path(db: &Context, def_id: DefId, segs: &mut Vec) { - let node = db.node(def_id).unwrap(); + fn calc_item_path(cx: &Context, def_id: DefId, segs: &mut Vec) { + let node = cx.node(def_id).unwrap(); match node.kind { NodeKind::Item(_) => {} - _ => calc_item_path(db, node.parent.unwrap(), segs), + _ => calc_item_path(cx, node.parent.unwrap(), segs), } let name = match node.kind { NodeKind::Item(item) => match &*item { - crate::rir::Item::Const(_) => (&*item.symbol_name()).const_ident(), crate::rir::Item::Mod(_) => return, - _ => (&*item.symbol_name()).upper_camel_ident(), + _ => cx.rust_name(def_id), }, NodeKind::Variant(v) => (&**v.name).variant_ident(), _ => panic!(), @@ -164,7 +163,7 @@ impl Context { let mut segs = Vec::from(&*self.mod_path(def_id)); - calc_item_path(&self, def_id, &mut segs); + calc_item_path(self, def_id, &mut segs); Arc::from(segs) } diff --git a/pilota-build/test_data/thrift/pilota_name.rs b/pilota-build/test_data/thrift/pilota_name.rs index 3737b4f1..6a85f8ee 100644 --- a/pilota-build/test_data/thrift/pilota_name.rs +++ b/pilota-build/test_data/thrift/pilota_name.rs @@ -118,12 +118,12 @@ pub mod pilota_name { } pub const LANG_ID: &'static str = "id"; #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] - pub struct Test { + pub struct Test1 { pub id: ::std::string::String, pub hello: ::std::string::String, } #[::async_trait::async_trait] - impl ::pilota::thrift::Message for Test { + impl ::pilota::thrift::Message for Test1 { fn encode( &self, protocol: &mut T, @@ -332,5 +332,348 @@ pub mod pilota_name { protocol.write_i32_len(*self as i32) } } + #[derive(PartialOrd, Hash, Eq, Ord, Debug, :: pilota :: derivative :: Derivative)] + #[derivative(Default)] + #[derive(Clone, PartialEq)] + pub enum TestServiceTestResult { + #[derivative(Default)] + Ok(Test1), + } + #[::async_trait::async_trait] + impl ::pilota::thrift::Message for TestServiceTestResult { + fn encode( + &self, + protocol: &mut T, + ) -> ::std::result::Result<(), ::pilota::thrift::Error> { + protocol.write_struct_begin(&::pilota::thrift::TStructIdentifier { + name: "TestServiceTestResult", + })?; + match self { + TestServiceTestResult::Ok(ref value) => { + protocol.write_field_begin(::pilota::thrift::TType::Struct, 0i16)?; + ::pilota::thrift::Message::encode(value, protocol)?; + protocol.write_field_end()?; + } + } + protocol.write_field_stop()?; + protocol.write_struct_end()?; + Ok(()) + } + fn decode( + protocol: &mut T, + ) -> ::std::result::Result { + let mut ret = None; + protocol.read_struct_begin()?; + loop { + let field_ident = protocol.read_field_begin()?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(0i16) => { + if ret.is_none() { + ret = Some(TestServiceTestResult::Ok( + ::pilota::thrift::Message::decode(protocol)?, + )); + } else { + return Err(::pilota::thrift::new_protocol_error( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "received multiple fields for union from remote Message", + )); + } + } + _ => { + protocol.skip(field_ident.field_type)?; + } + } + } + protocol.read_field_end()?; + protocol.read_struct_end()?; + if let Some(ret) = ret { + Ok(ret) + } else { + Err(::pilota::thrift::new_protocol_error( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "received empty union from remote Message", + )) + } + } + async fn decode_async( + protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol, + ) -> ::std::result::Result { + let mut ret = None; + protocol.read_struct_begin().await?; + loop { + let field_ident = protocol.read_field_begin().await?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(0i16) => { + if ret.is_none() { + ret = Some(TestServiceTestResult::Ok( + ::pilota::thrift::Message::decode_async(protocol).await?, + )); + } else { + return Err(::pilota::thrift::new_protocol_error( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "received multiple fields for union from remote Message", + )); + } + } + _ => { + protocol.skip(field_ident.field_type).await?; + } + } + } + protocol.read_field_end().await?; + protocol.read_struct_end().await?; + if let Some(ret) = ret { + Ok(ret) + } else { + Err(::pilota::thrift::new_protocol_error( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "received empty union from remote Message", + )) + } + } + fn size(&self, protocol: &T) -> usize { + protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier { + name: "TestServiceTestResult", + }) + match self { + TestServiceTestResult::Ok(ref value) => { + protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { + name: Some("Ok"), + field_type: ::pilota::thrift::TType::Struct, + id: Some(0i16), + }) + ::pilota::thrift::Message::size(value, protocol) + + protocol.write_field_end_len() + } + } + protocol.write_field_stop_len() + + protocol.write_struct_end_len() + } + } + #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] + pub struct TestServiceTestArgsSend { + pub req: Test2, + } + #[::async_trait::async_trait] + impl ::pilota::thrift::Message for TestServiceTestArgsSend { + fn encode( + &self, + protocol: &mut T, + ) -> ::std::result::Result<(), ::pilota::thrift::Error> { + let struct_ident = ::pilota::thrift::TStructIdentifier { + name: "TestServiceTestArgsSend", + }; + protocol.write_struct_begin(&struct_ident)?; + { + let value = &self.req; + protocol.write_field_begin(::pilota::thrift::TType::Struct, 1i16)?; + ::pilota::thrift::Message::encode(value, protocol)?; + protocol.write_field_end()?; + } + protocol.write_field_stop()?; + protocol.write_struct_end()?; + Ok(()) + } + fn decode( + protocol: &mut T, + ) -> ::std::result::Result { + let mut req = None; + protocol.read_struct_begin()?; + loop { + let field_ident = protocol.read_field_begin()?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => { + req = Some(::pilota::thrift::Message::decode(protocol)?); + } + _ => { + protocol.skip(field_ident.field_type)?; + } + } + protocol.read_field_end()?; + } + protocol.read_struct_end()?; + let req = if let Some(req) = req { + req + } else { + return Err(::pilota::thrift::Error::Protocol( + ::pilota::thrift::ProtocolError::new( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "field req is required".to_string(), + ), + )); + }; + let data = Self { req }; + Ok(data) + } + async fn decode_async( + protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol, + ) -> ::std::result::Result { + let mut req = None; + protocol.read_struct_begin().await?; + loop { + let field_ident = protocol.read_field_begin().await?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => { + req = Some(::pilota::thrift::Message::decode_async(protocol).await?); + } + _ => { + protocol.skip(field_ident.field_type).await?; + } + } + protocol.read_field_end().await?; + } + protocol.read_struct_end().await?; + let req = if let Some(req) = req { + req + } else { + return Err(::pilota::thrift::Error::Protocol( + ::pilota::thrift::ProtocolError::new( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "field req is required".to_string(), + ), + )); + }; + let data = Self { req }; + Ok(data) + } + fn size(&self, protocol: &T) -> usize { + protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier { + name: "TestServiceTestArgsSend", + }) + { + let value = &self.req; + protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { + name: Some("req"), + field_type: ::pilota::thrift::TType::Struct, + id: Some(1i16), + }) + ::pilota::thrift::Message::size(value, protocol) + + protocol.write_field_end_len() + } + protocol.write_field_stop_len() + + protocol.write_struct_end_len() + } + } + #[derive(PartialOrd, Hash, Eq, Ord, Debug, Default, Clone, PartialEq)] + pub struct TestServiceTestArgsRecv { + pub req: Test2, + } + #[::async_trait::async_trait] + impl ::pilota::thrift::Message for TestServiceTestArgsRecv { + fn encode( + &self, + protocol: &mut T, + ) -> ::std::result::Result<(), ::pilota::thrift::Error> { + let struct_ident = ::pilota::thrift::TStructIdentifier { + name: "TestServiceTestArgsRecv", + }; + protocol.write_struct_begin(&struct_ident)?; + { + let value = &self.req; + protocol.write_field_begin(::pilota::thrift::TType::Struct, 1i16)?; + ::pilota::thrift::Message::encode(value, protocol)?; + protocol.write_field_end()?; + } + protocol.write_field_stop()?; + protocol.write_struct_end()?; + Ok(()) + } + fn decode( + protocol: &mut T, + ) -> ::std::result::Result { + let mut req = None; + protocol.read_struct_begin()?; + loop { + let field_ident = protocol.read_field_begin()?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => { + req = Some(::pilota::thrift::Message::decode(protocol)?); + } + _ => { + protocol.skip(field_ident.field_type)?; + } + } + protocol.read_field_end()?; + } + protocol.read_struct_end()?; + let req = if let Some(req) = req { + req + } else { + return Err(::pilota::thrift::Error::Protocol( + ::pilota::thrift::ProtocolError::new( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "field req is required".to_string(), + ), + )); + }; + let data = Self { req }; + Ok(data) + } + async fn decode_async( + protocol: &mut ::pilota::thrift::TAsyncBinaryProtocol, + ) -> ::std::result::Result { + let mut req = None; + protocol.read_struct_begin().await?; + loop { + let field_ident = protocol.read_field_begin().await?; + if field_ident.field_type == ::pilota::thrift::TType::Stop { + break; + } + let field_id = field_ident.id; + match field_id { + Some(1i16) if field_ident.field_type == ::pilota::thrift::TType::Struct => { + req = Some(::pilota::thrift::Message::decode_async(protocol).await?); + } + _ => { + protocol.skip(field_ident.field_type).await?; + } + } + protocol.read_field_end().await?; + } + protocol.read_struct_end().await?; + let req = if let Some(req) = req { + req + } else { + return Err(::pilota::thrift::Error::Protocol( + ::pilota::thrift::ProtocolError::new( + ::pilota::thrift::ProtocolErrorKind::InvalidData, + "field req is required".to_string(), + ), + )); + }; + let data = Self { req }; + Ok(data) + } + fn size(&self, protocol: &T) -> usize { + protocol.write_struct_begin_len(&::pilota::thrift::TStructIdentifier { + name: "TestServiceTestArgsRecv", + }) + { + let value = &self.req; + protocol.write_field_begin_len(&::pilota::thrift::TFieldIdentifier { + name: Some("req"), + field_type: ::pilota::thrift::TType::Struct, + id: Some(1i16), + }) + ::pilota::thrift::Message::size(value, protocol) + + protocol.write_field_end_len() + } + protocol.write_field_stop_len() + + protocol.write_struct_end_len() + } + } + #[::async_trait::async_trait] + pub trait TestService {} } } diff --git a/pilota-build/test_data/thrift/pilota_name.thrift b/pilota-build/test_data/thrift/pilota_name.thrift index cc97297a..17264f2c 100644 --- a/pilota-build/test_data/thrift/pilota_name.thrift +++ b/pilota-build/test_data/thrift/pilota_name.thrift @@ -8,9 +8,13 @@ const string id = "id" (pilota.name="LANG_ID"); struct Test { 1: required string ID, 2: required string Id (pilota.name="hello"), -} +}(pilota.name="Test1") enum Index { A (pilota.name="AA"), B, +} + +service TestService { + Test test(1: TEST req); } \ No newline at end of file