Skip to content

Commit

Permalink
fix: generate i8 for thrift byte (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Dec 4, 2023
1 parent 7b3e38a commit af4c38f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 13 deletions.
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.10"
version = "0.9.11"
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/parser/thrift/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl ThriftLower {
let kind = match &ty.0 {
thrift_parser::Ty::String => ir::TyKind::String,
thrift_parser::Ty::Void => ir::TyKind::Void,
thrift_parser::Ty::Byte => ir::TyKind::U8,
thrift_parser::Ty::Byte => ir::TyKind::I8,
thrift_parser::Ty::Bool => ir::TyKind::Bool,
thrift_parser::Ty::Binary => ir::TyKind::Bytes,
thrift_parser::Ty::I8 => ir::TyKind::I8,
Expand Down
24 changes: 22 additions & 2 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,15 @@ pub mod default_value {
fn default() -> Self {
C {
off: Some(::pilota::FastStr::from_static_str("off")),
test_byte: Some(0i8),
}
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Clone, PartialEq)]
pub struct C {
pub off: ::std::option::Option<::pilota::FastStr>,

pub test_byte: ::std::option::Option<i8>,
}
impl ::pilota::thrift::Message for C {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
Expand All @@ -112,6 +115,9 @@ pub mod default_value {
if let Some(value) = self.off.as_ref() {
protocol.write_faststr_field(1, (value).clone())?;
}
if let Some(value) = self.test_byte.as_ref() {
protocol.write_i8_field(2, *value)?;
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
Expand All @@ -124,6 +130,7 @@ pub mod default_value {
use ::pilota::{thrift::TLengthProtocolExt, Buf};

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

let mut __pilota_decoding_field_id = None;

Expand All @@ -144,6 +151,9 @@ pub mod default_value {
{
off = Some(protocol.read_faststr()?);
}
Some(2) if field_ident.field_type == ::pilota::thrift::TType::I8 => {
test_byte = Some(protocol.read_i8()?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
Expand All @@ -167,7 +177,7 @@ pub mod default_value {
};
protocol.read_struct_end()?;

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

Expand All @@ -183,6 +193,7 @@ pub mod default_value {
> {
::std::boxed::Box::pin(async move {
let mut off = Some(::pilota::FastStr::from_static_str("off"));
let mut test_byte = Some(0i8);

let mut __pilota_decoding_field_id = None;

Expand All @@ -202,6 +213,11 @@ pub mod default_value {
{
off = Some(protocol.read_faststr().await?);
}
Some(2)
if field_ident.field_type == ::pilota::thrift::TType::I8 =>
{
test_byte = Some(protocol.read_i8().await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
Expand All @@ -226,7 +242,7 @@ pub mod default_value {
};
protocol.read_struct_end().await?;

let data = Self { off };
let data = Self { off, test_byte };
Ok(data)
})
}
Expand All @@ -239,6 +255,10 @@ pub mod default_value {
.off
.as_ref()
.map_or(0, |value| protocol.faststr_field_len(Some(1), value))
+ self
.test_byte
.as_ref()
.map_or(0, |value| protocol.i8_field_len(Some(2), *value))
+ protocol.field_stop_len()
+ protocol.struct_end_len()
}
Expand Down
1 change: 1 addition & 0 deletions pilota-build/test_data/thrift/default_value.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ struct A {

struct C {
1: string off = "off",
2: optional byte test_byte = 0,
}
47 changes: 39 additions & 8 deletions pilota-build/test_data/thrift/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,12 +513,15 @@ pub mod multi {
fn default() -> Self {
C {
off: Some(::pilota::FastStr::from_static_str("off")),
test_byte: Some(0i8),
}
}
}
#[derive(PartialOrd, Hash, Eq, Ord, Debug, Clone, PartialEq)]
pub struct C {
pub off: ::std::option::Option<::pilota::FastStr>,

pub test_byte: ::std::option::Option<i8>,
}
impl ::pilota::thrift::Message for C {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
Expand All @@ -533,6 +536,9 @@ pub mod multi {
if let Some(value) = self.off.as_ref() {
protocol.write_faststr_field(1, (value).clone())?;
}
if let Some(value) = self.test_byte.as_ref() {
protocol.write_i8_field(2, *value)?;
}
protocol.write_field_stop()?;
protocol.write_struct_end()?;
Ok(())
Expand All @@ -545,6 +551,7 @@ pub mod multi {
use ::pilota::{thrift::TLengthProtocolExt, Buf};

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

let mut __pilota_decoding_field_id = None;

Expand All @@ -565,6 +572,9 @@ pub mod multi {
{
off = Some(protocol.read_faststr()?);
}
Some(2) if field_ident.field_type == ::pilota::thrift::TType::I8 => {
test_byte = Some(protocol.read_i8()?);
}
_ => {
protocol.skip(field_ident.field_type)?;
}
Expand All @@ -588,7 +598,7 @@ pub mod multi {
};
protocol.read_struct_end()?;

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

Expand All @@ -604,6 +614,7 @@ pub mod multi {
> {
::std::boxed::Box::pin(async move {
let mut off = Some(::pilota::FastStr::from_static_str("off"));
let mut test_byte = Some(0i8);

let mut __pilota_decoding_field_id = None;

Expand All @@ -623,6 +634,11 @@ pub mod multi {
{
off = Some(protocol.read_faststr().await?);
}
Some(2)
if field_ident.field_type == ::pilota::thrift::TType::I8 =>
{
test_byte = Some(protocol.read_i8().await?);
}
_ => {
protocol.skip(field_ident.field_type).await?;
}
Expand All @@ -647,7 +663,7 @@ pub mod multi {
};
protocol.read_struct_end().await?;

let data = Self { off };
let data = Self { off, test_byte };
Ok(data)
})
}
Expand All @@ -660,6 +676,10 @@ pub mod multi {
.off
.as_ref()
.map_or(0, |value| protocol.faststr_field_len(Some(1), value))
+ self
.test_byte
.as_ref()
.map_or(0, |value| protocol.i8_field_len(Some(2), *value))
+ protocol.field_stop_len()
+ protocol.struct_end_len()
}
Expand All @@ -673,6 +693,7 @@ pub mod multi {
A {
c: Some(super::default_value::C {
off: Some(::pilota::FastStr::from_static_str("off")),
test_byte: Default::default(),
}),
}
}
Expand Down Expand Up @@ -705,9 +726,7 @@ pub mod multi {
#[allow(unused_imports)]
use ::pilota::{thrift::TLengthProtocolExt, Buf};

let mut c = Some(super::default_value::C {
off: Some(::pilota::FastStr::from_static_str("off")),
});
let mut c = None;

let mut __pilota_decoding_field_id = None;

Expand Down Expand Up @@ -751,6 +770,13 @@ pub mod multi {
};
protocol.read_struct_end()?;

if c.is_none() {
c = Some(super::default_value::C {
off: Some(::pilota::FastStr::from_static_str("off")),
test_byte: Default::default(),
});
}

let data = Self { c };
Ok(data)
}
Expand All @@ -766,9 +792,7 @@ pub mod multi {
>,
> {
::std::boxed::Box::pin(async move {
let mut c = Some(super::default_value::C {
off: Some(::pilota::FastStr::from_static_str("off")),
});
let mut c = None;

let mut __pilota_decoding_field_id = None;

Expand Down Expand Up @@ -813,6 +837,13 @@ pub mod multi {
};
protocol.read_struct_end().await?;

if c.is_none() {
c = Some(super::default_value::C {
off: Some(::pilota::FastStr::from_static_str("off")),
test_byte: Default::default(),
});
}

let data = Self { c };
Ok(data)
})
Expand Down

0 comments on commit af4c38f

Please sign in to comment.