Skip to content

Commit

Permalink
fix: convert enum default value to int correctly (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
Millione authored Apr 9, 2024
1 parent 0f00b20 commit ecaf838
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 4 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.11.1"
version = "0.11.2"
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 @@ -616,7 +616,7 @@ impl Context {
CodegenTy::I8 => "i8",
_ => unreachable!(),
};
(format!("({stream} as {target})").into(), true)
(format!("({stream}.inner() as {target})").into(), true)
}
_ => panic!("invalid convert {:?} to {:?}", ident_ty, target),
}
Expand Down
22 changes: 22 additions & 0 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ pub mod default_value {
a: Some(false),
test_b: Some(B::READ),
test_b2: Some(B::WRITE),
test_b3: Some((B::READ.inner() as i8)),
map: Some({
let mut map = ::pilota::AHashMap::with_capacity(1);
map.insert(
Expand All @@ -284,6 +285,8 @@ pub mod default_value {

pub test_b2: ::std::option::Option<B>,

pub test_b3: ::std::option::Option<i8>,

pub map:
::std::option::Option<::pilota::AHashMap<::pilota::FastStr, ::pilota::FastStr>>,

Expand Down Expand Up @@ -314,6 +317,9 @@ pub mod default_value {
if let Some(value) = self.test_b2.as_ref() {
protocol.write_i32_field(5, (value).inner())?;
}
if let Some(value) = self.test_b3.as_ref() {
protocol.write_i8_field(5, *value)?;
}
if let Some(value) = self.map.as_ref() {
protocol.write_map_field(
6,
Expand Down Expand Up @@ -355,6 +361,7 @@ pub mod default_value {
let mut a = Some(false);
let mut test_b = Some(B::READ);
let mut test_b2 = Some(B::WRITE);
let mut test_b3 = Some((B::READ.inner() as i8));
let mut map = None;
let mut test_double = Some(1f64);
let mut test_double2 = Some(1.2f64);
Expand Down Expand Up @@ -393,6 +400,9 @@ pub mod default_value {
Some(5) if field_ident.field_type == ::pilota::thrift::TType::I32 => {
test_b2 = Some(::pilota::thrift::Message::decode(protocol)?);
}
Some(5) if field_ident.field_type == ::pilota::thrift::TType::I8 => {
test_b3 = Some(protocol.read_i8()?);
}
Some(6) if field_ident.field_type == ::pilota::thrift::TType::Map => {
map = Some({
let map_ident = protocol.read_map_begin()?;
Expand Down Expand Up @@ -460,6 +470,7 @@ pub mod default_value {
a,
test_b,
test_b2,
test_b3,
map,
test_double,
test_double2,
Expand All @@ -484,6 +495,7 @@ pub mod default_value {
let mut a = Some(false);
let mut test_b = Some(B::READ);
let mut test_b2 = Some(B::WRITE);
let mut test_b3 = Some((B::READ.inner() as i8));
let mut map = None;
let mut test_double = Some(1f64);
let mut test_double2 = Some(1.2f64);
Expand Down Expand Up @@ -534,6 +546,11 @@ pub mod default_value {
.await?,
);
}
Some(5)
if field_ident.field_type == ::pilota::thrift::TType::I8 =>
{
test_b3 = Some(protocol.read_i8().await?);
}
Some(6)
if field_ident.field_type == ::pilota::thrift::TType::Map =>
{
Expand Down Expand Up @@ -608,6 +625,7 @@ pub mod default_value {
a,
test_b,
test_b2,
test_b3,
map,
test_double,
test_double2,
Expand Down Expand Up @@ -635,6 +653,10 @@ pub mod default_value {
.test_b2
.as_ref()
.map_or(0, |value| protocol.i32_field_len(Some(5), (value).inner()))
+ self
.test_b3
.as_ref()
.map_or(0, |value| protocol.i8_field_len(Some(5), *value))
+ self.map.as_ref().map_or(0, |value| {
protocol.map_field_len(
Some(6),
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 @@ -11,6 +11,7 @@ struct A {
3: optional bool a = false,
4: optional B test_b = B.Read,
5: optional B test_b2 = 2,
5: optional i8 test_b3 = B.Read,
6: optional map<string, string> map = {"hello": "world"},
7: optional double test_double = 1,
8: optional double test_double2 = 1.2,
Expand Down
2 changes: 1 addition & 1 deletion pilota-build/test_data/thrift/enum_test.thrift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
enum Index {
A = 0x01,
B = 0x10,
}
}
22 changes: 22 additions & 0 deletions pilota-build/test_data/thrift/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub mod multi {
a: Some(false),
test_b: Some(B::READ),
test_b2: Some(B::WRITE),
test_b3: Some((B::READ.inner() as i8)),
map: Some({
let mut map = ::pilota::AHashMap::with_capacity(1);
map.insert(
Expand All @@ -37,6 +38,8 @@ pub mod multi {

pub test_b2: ::std::option::Option<B>,

pub test_b3: ::std::option::Option<i8>,

pub map:
::std::option::Option<::pilota::AHashMap<::pilota::FastStr, ::pilota::FastStr>>,

Expand Down Expand Up @@ -67,6 +70,9 @@ pub mod multi {
if let Some(value) = self.test_b2.as_ref() {
protocol.write_i32_field(5, (value).inner())?;
}
if let Some(value) = self.test_b3.as_ref() {
protocol.write_i8_field(5, *value)?;
}
if let Some(value) = self.map.as_ref() {
protocol.write_map_field(
6,
Expand Down Expand Up @@ -108,6 +114,7 @@ pub mod multi {
let mut a = Some(false);
let mut test_b = Some(B::READ);
let mut test_b2 = Some(B::WRITE);
let mut test_b3 = Some((B::READ.inner() as i8));
let mut map = None;
let mut test_double = Some(1f64);
let mut test_double2 = Some(1.2f64);
Expand Down Expand Up @@ -146,6 +153,9 @@ pub mod multi {
Some(5) if field_ident.field_type == ::pilota::thrift::TType::I32 => {
test_b2 = Some(::pilota::thrift::Message::decode(protocol)?);
}
Some(5) if field_ident.field_type == ::pilota::thrift::TType::I8 => {
test_b3 = Some(protocol.read_i8()?);
}
Some(6) if field_ident.field_type == ::pilota::thrift::TType::Map => {
map = Some({
let map_ident = protocol.read_map_begin()?;
Expand Down Expand Up @@ -213,6 +223,7 @@ pub mod multi {
a,
test_b,
test_b2,
test_b3,
map,
test_double,
test_double2,
Expand All @@ -237,6 +248,7 @@ pub mod multi {
let mut a = Some(false);
let mut test_b = Some(B::READ);
let mut test_b2 = Some(B::WRITE);
let mut test_b3 = Some((B::READ.inner() as i8));
let mut map = None;
let mut test_double = Some(1f64);
let mut test_double2 = Some(1.2f64);
Expand Down Expand Up @@ -287,6 +299,11 @@ pub mod multi {
.await?,
);
}
Some(5)
if field_ident.field_type == ::pilota::thrift::TType::I8 =>
{
test_b3 = Some(protocol.read_i8().await?);
}
Some(6)
if field_ident.field_type == ::pilota::thrift::TType::Map =>
{
Expand Down Expand Up @@ -361,6 +378,7 @@ pub mod multi {
a,
test_b,
test_b2,
test_b3,
map,
test_double,
test_double2,
Expand Down Expand Up @@ -388,6 +406,10 @@ pub mod multi {
.test_b2
.as_ref()
.map_or(0, |value| protocol.i32_field_len(Some(5), (value).inner()))
+ self
.test_b3
.as_ref()
.map_or(0, |value| protocol.i8_field_len(Some(5), *value))
+ self.map.as_ref().map_or(0, |value| {
protocol.map_field_len(
Some(6),
Expand Down

0 comments on commit ecaf838

Please sign in to comment.