Skip to content

Commit

Permalink
feat: compatibility of bool and map default values (#261)
Browse files Browse the repository at this point in the history
* feat: support bool default value set by integer

* feat: support map default value use empty []
  • Loading branch information
Millione authored Jul 5, 2024
1 parent 1355fd8 commit e1b3ec2
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 3 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.12"
version = "0.11.13"
edition = "2021"
description = "Compile thrift and protobuf idl into rust code at compile-time."
documentation = "https://docs.rs/pilota-build"
Expand Down
8 changes: 8 additions & 0 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,10 @@ impl Context {
_ => panic!("invalid map type {:?}", map),
},
(Literal::Map(m), CodegenTy::Map(k_ty, v_ty)) => (mk_map(m, k_ty, v_ty)?, false),
(Literal::List(m), CodegenTy::Map(_, _) | CodegenTy::LazyStaticRef(_)) => {
assert!(m.is_empty());
("::pilota::AHashMap::new()".into(), false)
}
_ => self.lit_into_ty(lit, ty)?,
})
}
Expand Down Expand Up @@ -677,6 +681,10 @@ impl Context {
)
}
(Literal::Bool(b), CodegenTy::Bool) => (format! { "{b}" }.into(), true),
(Literal::Int(i), CodegenTy::Bool) => {
let b = *i != 0;
(format! { "{b}" }.into(), true)
}
(Literal::String(s), CodegenTy::Bytes) => {
let s = &**s;
(
Expand Down
93 changes: 93 additions & 0 deletions pilota-build/test_data/thrift/default_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,8 @@ pub mod default_value {
map
},
test_set: ::pilota::AHashSet::from([::pilota::OrderedFloat(1f64)]),
a2: Some(true),
map2: Some(::pilota::AHashMap::new()),
}
}
}
Expand Down Expand Up @@ -320,6 +322,11 @@ pub mod default_value {
pub test_map: ::pilota::AHashMap<::pilota::OrderedFloat<f64>, f64>,

pub test_set: ::pilota::AHashSet<::pilota::OrderedFloat<f64>>,

pub a2: ::std::option::Option<bool>,

pub map2:
::std::option::Option<::pilota::AHashMap<::pilota::FastStr, ::pilota::FastStr>>,
}
impl ::pilota::thrift::Message for A {
fn encode<T: ::pilota::thrift::TOutputProtocol>(
Expand Down Expand Up @@ -394,6 +401,25 @@ pub mod default_value {
::std::result::Result::Ok(())
},
)?;
if let Some(value) = self.a2.as_ref() {
__protocol.write_bool_field(13, *value)?;
}
if let Some(value) = self.map2.as_ref() {
__protocol.write_map_field(
14,
::pilota::thrift::TType::Binary,
::pilota::thrift::TType::Binary,
&value,
|__protocol, key| {
__protocol.write_faststr((key).clone())?;
::std::result::Result::Ok(())
},
|__protocol, val| {
__protocol.write_faststr((val).clone())?;
::std::result::Result::Ok(())
},
)?;
}
__protocol.write_field_stop()?;
__protocol.write_struct_end()?;
::std::result::Result::Ok(())
Expand All @@ -418,6 +444,8 @@ pub mod default_value {
let mut empty = ::pilota::Bytes::from_static("".as_bytes());
let mut test_map = None;
let mut test_set = None;
let mut a2 = Some(true);
let mut map2 = None;

let mut __pilota_decoding_field_id = None;

Expand Down Expand Up @@ -517,6 +545,23 @@ pub mod default_value {
val
});
}
Some(13) if field_ident.field_type == ::pilota::thrift::TType::Bool => {
a2 = Some(__protocol.read_bool()?);
}
Some(14) if field_ident.field_type == ::pilota::thrift::TType::Map => {
map2 = Some({
let map_ident = __protocol.read_map_begin()?;
let mut val = ::pilota::AHashMap::with_capacity(map_ident.size);
for _ in 0..map_ident.size {
val.insert(
__protocol.read_faststr()?,
__protocol.read_faststr()?,
);
}
__protocol.read_map_end()?;
val
});
}
_ => {
__protocol.skip(field_ident.field_type)?;
}
Expand Down Expand Up @@ -555,6 +600,9 @@ pub mod default_value {
});
let test_set = test_set
.unwrap_or_else(|| ::pilota::AHashSet::from([::pilota::OrderedFloat(1f64)]));
if map2.is_none() {
map2 = Some(::pilota::AHashMap::new());
}

let data = Self {
faststr,
Expand All @@ -570,6 +618,8 @@ pub mod default_value {
empty,
test_map,
test_set,
a2,
map2,
};
::std::result::Result::Ok(data)
}
Expand Down Expand Up @@ -598,6 +648,8 @@ pub mod default_value {
let mut empty = ::pilota::Bytes::from_static("".as_bytes());
let mut test_map = None;
let mut test_set = None;
let mut a2 = Some(true);
let mut map2 = None;

let mut __pilota_decoding_field_id = None;

Expand Down Expand Up @@ -725,6 +777,28 @@ pub mod default_value {
val
});
}
Some(13)
if field_ident.field_type == ::pilota::thrift::TType::Bool =>
{
a2 = Some(__protocol.read_bool().await?);
}
Some(14)
if field_ident.field_type == ::pilota::thrift::TType::Map =>
{
map2 = Some({
let map_ident = __protocol.read_map_begin().await?;
let mut val =
::pilota::AHashMap::with_capacity(map_ident.size);
for _ in 0..map_ident.size {
val.insert(
__protocol.read_faststr().await?,
__protocol.read_faststr().await?,
);
}
__protocol.read_map_end().await?;
val
});
}
_ => {
__protocol.skip(field_ident.field_type).await?;
}
Expand Down Expand Up @@ -765,6 +839,9 @@ pub mod default_value {
let test_set = test_set.unwrap_or_else(|| {
::pilota::AHashSet::from([::pilota::OrderedFloat(1f64)])
});
if map2.is_none() {
map2 = Some(::pilota::AHashMap::new());
}

let data = Self {
faststr,
Expand All @@ -780,6 +857,8 @@ pub mod default_value {
empty,
test_map,
test_set,
a2,
map2,
};
::std::result::Result::Ok(data)
})
Expand Down Expand Up @@ -842,6 +921,20 @@ pub mod default_value {
&self.test_set,
|__protocol, el| __protocol.double_len(el.0),
)
+ self
.a2
.as_ref()
.map_or(0, |value| __protocol.bool_field_len(Some(13), *value))
+ self.map2.as_ref().map_or(0, |value| {
__protocol.map_field_len(
Some(14),
::pilota::thrift::TType::Binary,
::pilota::thrift::TType::Binary,
value,
|__protocol, key| __protocol.faststr_len(key),
|__protocol, val| __protocol.faststr_len(val),
)
})
+ __protocol.field_stop_len()
+ __protocol.struct_end_len()
}
Expand Down
4 changes: 3 additions & 1 deletion pilota-build/test_data/thrift/default_value.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ struct A {
10: required binary empty = "",
11: required map<double, double> test_map = {1.0: 2.0},
12: required set<double> test_set = [1.0],
}
13: bool a2 = 3,
14: map<string, string> map2 = [],
}

struct C {
1: string off = "off",
Expand Down
Loading

0 comments on commit e1b3ec2

Please sign in to comment.