Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: default value path incorrect #210

Merged
merged 1 commit into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pilota-build/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pilota-build"
version = "0.9.4"
version = "0.9.5"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
2 changes: 1 addition & 1 deletion pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ impl Context {
let is_const = fields.iter().all(|(_, is_const)| *is_const);
let fields = fields.into_iter().map(|f| f.0).join(",");

let name = self.rust_name(*did);
let name = self.cur_related_item_path(*did);

(
format! {
Expand Down
1 change: 0 additions & 1 deletion pilota-build/src/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,6 @@ impl Plugin for ImplDefaultPlugin {
.map(|f| {
let name = cx.rust_name(f.did);
let default = cx.default_val(f).map(|v| v.0);

if let Some(default) = default {
let mut val = default;
if f.is_optional() {
Expand Down
155 changes: 155 additions & 0 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,161 @@ pub mod default_value {
protocol.i32_len(*self as i32)
}
}
impl Default for C {
fn default() -> Self {
C {
off: Some(::pilota::FastStr::from_static_str("off")),
}
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Clone, PartialEq)]
pub struct C {
pub off: ::std::option::Option<::pilota::FastStr>,
}
impl ::pilota::thrift::Message for C {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
&self,
protocol: &mut T,
) -> ::std::result::Result<(), ::pilota::thrift::EncodeError> {
#[allow(unused_imports)]
use ::pilota::thrift::TOutputProtocolExt;
let struct_ident = ::pilota::thrift::TStructIdentifier { name: "C" };

protocol.write_struct_begin(&struct_ident)?;
if let Some(value) = self.off.as_ref() {
protocol.write_faststr_field(1, (value).clone())?;
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
}

fn decode<T: ::pilota::thrift::TInputProtocol>(
protocol: &mut T,
) -> ::std::result::Result<Self, ::pilota::thrift::DecodeError> {
#[allow(unused_imports)]
use ::pilota::{thrift::TLengthProtocolExt, Buf};

let mut off = Some(::pilota::FastStr::from_static_str("off"));

let mut __pilota_decoding_field_id = None;

protocol.read_struct_begin()?;
if let Err(err) = (|| {
loop {
let field_ident = protocol.read_field_begin()?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
protocol.field_stop_len();
break;
} else {
protocol.field_begin_len(field_ident.field_type, field_ident.id);
}
__pilota_decoding_field_id = field_ident.id;
match field_ident.id {
Some(1)
if field_ident.field_type == ::pilota::thrift::TType::Binary =>
{
off = Some(protocol.read_faststr()?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
}

protocol.read_field_end()?;
protocol.field_end_len();
}
Ok::<_, ::pilota::thrift::DecodeError>(())
})() {
if let Some(field_id) = __pilota_decoding_field_id {
return Err(::pilota::thrift::DecodeError::new(
::pilota::thrift::DecodeErrorKind::WithContext(::std::boxed::Box::new(
err,
)),
format!("decode struct `C` field(#{}) failed", field_id),
));
} else {
return Err(err);
}
};
protocol.read_struct_end()?;

let data = Self { off };
Ok(data)
}

fn decode_async<'a, T: ::pilota::thrift::TAsyncInputProtocol>(
protocol: &'a mut T,
) -> ::std::pin::Pin<
::std::boxed::Box<
dyn ::std::future::Future<
Output = ::std::result::Result<Self, ::pilota::thrift::DecodeError>,
> + Send
+ 'a,
>,
> {
::std::boxed::Box::pin(async move {
let mut off = Some(::pilota::FastStr::from_static_str("off"));

let mut __pilota_decoding_field_id = None;

protocol.read_struct_begin().await?;
if let Err(err) = async {
loop {
let field_ident = protocol.read_field_begin().await?;
if field_ident.field_type == ::pilota::thrift::TType::Stop {
break;
} else {
}
__pilota_decoding_field_id = field_ident.id;
match field_ident.id {
Some(1)
if field_ident.field_type
== ::pilota::thrift::TType::Binary =>
{
off = Some(protocol.read_faststr().await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
}

protocol.read_field_end().await?;
}
Ok::<_, ::pilota::thrift::DecodeError>(())
}
.await
{
if let Some(field_id) = __pilota_decoding_field_id {
return Err(::pilota::thrift::DecodeError::new(
::pilota::thrift::DecodeErrorKind::WithContext(
::std::boxed::Box::new(err),
),
format!("decode struct `C` field(#{}) failed", field_id),
));
} else {
return Err(err);
}
};
protocol.read_struct_end().await?;

let data = Self { off };
Ok(data)
})
}

fn size<T: ::pilota::thrift::TLengthProtocol>(&self, protocol: &mut T) -> usize {
#[allow(unused_imports)]
use ::pilota::thrift::TLengthProtocolExt;
protocol.struct_begin_len(&::pilota::thrift::TStructIdentifier { name: "C" })
+ self
.off
.as_ref()
.map_or(0, |value| protocol.faststr_field_len(Some(1), value))
+ protocol.field_stop_len()
+ protocol.struct_end_len()
}
}
impl Default for A {
fn default() -> Self {
A {
Expand Down
4 changes: 4 additions & 0 deletions pilota-build/test_data/thrift/default_value.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ struct A {
7: optional double test_double = 1,
8: optional double test_double2 = 1.2,
9: optional string alias_str = A_S,
}

struct C {
1: string off = "off",
}
Loading
Loading