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

feat: support double type as map/set key #253

Merged
merged 1 commit into from
Jun 18, 2024
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
10 changes: 10 additions & 0 deletions Cargo.lock

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

10 changes: 9 additions & 1 deletion pilota-build/src/codegen/thrift/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ThriftBackend {
ty::I16 => "::pilota::thrift::TType::I16".into(),
ty::I32 => "::pilota::thrift::TType::I32".into(),
ty::I64 => "::pilota::thrift::TType::I64".into(),
ty::F64 => "::pilota::thrift::TType::Double".into(),
ty::F64 | ty::OrderedF64 => "::pilota::thrift::TType::Double".into(),
ty::Uuid => "::pilota::thrift::TType::Uuid".into(),
ty::Vec(_) => "::pilota::thrift::TType::List".into(),
ty::Set(_) => "::pilota::thrift::TType::Set".into(),
Expand Down Expand Up @@ -59,6 +59,7 @@ impl ThriftBackend {
ty::I32 => format!("protocol.write_i32(*{ident})?;").into(),
ty::I64 => format!("protocol.write_i64(*{ident})?;").into(),
ty::F64 => format!("protocol.write_double(*{ident})?;").into(),
ty::OrderedF64 => format!("protocol.write_double({ident}.0)?;").into(),
ty::Uuid => format!("protocol.write_uuid({ident})?;").into(),
ty::Vec(ty) => {
let el_ttype = self.ttype(ty);
Expand Down Expand Up @@ -133,6 +134,7 @@ impl ThriftBackend {
ty::I32 => format!("protocol.write_i32_field({id}, *{ident})?;").into(),
ty::I64 => format!("protocol.write_i64_field({id}, *{ident})?;").into(),
ty::F64 => format!("protocol.write_double_field({id}, *{ident})?;").into(),
ty::OrderedF64 => format!("protocol.write_double_field({id}, {ident}.0)?;").into(),
ty::Uuid => format!("protocol.write_uuid_field({id}, *{ident})?;").into(),
ty::Vec(ty) => {
let el_ttype = self.ttype(ty);
Expand Down Expand Up @@ -207,6 +209,7 @@ impl ThriftBackend {
ty::I32 => format!("protocol.i32_len(*{ident})").into(),
ty::I64 => format!("protocol.i64_len(*{ident})").into(),
ty::F64 => format!("protocol.double_len(*{ident})").into(),
ty::OrderedF64 => format!("protocol.double_len({ident}.0)").into(),
ty::Uuid => format!("protocol.uuid_len(*{ident})").into(),
ty::Vec(el) => {
let add_el = self.codegen_ty_size(el, "el".into());
Expand Down Expand Up @@ -263,6 +266,7 @@ impl ThriftBackend {
ty::I32 => format!("protocol.i32_field_len(Some({id}), *{ident})").into(),
ty::I64 => format!("protocol.i64_field_len(Some({id}), *{ident})").into(),
ty::F64 => format!("protocol.double_field_len(Some({id}), *{ident}) ").into(),
ty::OrderedF64 => format!("protocol.double_field_len(Some({id}), {ident}.0) ").into(),
ty::Uuid => format!("protocol.uuid_field_len(Some({id}), *{ident}) ").into(),
ty::Vec(el) => {
let add_el = self.codegen_ty_size(el, "el".into());
Expand Down Expand Up @@ -326,6 +330,10 @@ impl ThriftBackend {
ty::I32 => helper.codegen_read_i32(),
ty::I64 => helper.codegen_read_i64(),
ty::F64 => helper.codegen_read_double(),
ty::OrderedF64 => {
let read_double = helper.codegen_read_double();
format!("::pilota::OrderedFloat({read_double})").into()
}
ty::Uuid => helper.codegen_read_uuid(),
ty::Vec(ty) => {
let read_list_begin = helper.codegen_read_list_begin();
Expand Down
20 changes: 12 additions & 8 deletions pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ pub struct Context {
pub(crate) codegen_items: Arc<[DefId]>,
pub(crate) path_resolver: Arc<dyn PathResolver>,
pub(crate) mode: Arc<Mode>,
pub(crate) keep_unknown_fields: FxHashSet<DefId>,
pub location_map: FxHashMap<DefId, DefLocation>,
pub entry_map: HashMap<DefLocation, Vec<(DefId, DefLocation)>>,
pub plugin_gen: DashMap<DefLocation, String>,
pub(crate) keep_unknown_fields: Arc<FxHashSet<DefId>>,
pub location_map: Arc<FxHashMap<DefId, DefLocation>>,
pub entry_map: Arc<HashMap<DefLocation, Vec<(DefId, DefLocation)>>>,
pub plugin_gen: Arc<DashMap<DefLocation, String>>,
pub(crate) dedups: Vec<FastStr>,
pub(crate) common_crate_name: FastStr,
}
Expand Down Expand Up @@ -336,9 +336,9 @@ impl ContextBuilder {
Mode::SingleFile { .. } => Arc::new(DefaultPathResolver),
},
mode: Arc::new(self.mode),
keep_unknown_fields: self.keep_unknown_fields,
location_map: self.location_map,
entry_map: self.entry_map,
keep_unknown_fields: Arc::new(self.keep_unknown_fields),
location_map: Arc::new(self.location_map),
entry_map: Arc::new(self.entry_map),
plugin_gen: Default::default(),
dedups,
common_crate_name,
Expand Down Expand Up @@ -574,6 +574,10 @@ impl Context {
let f = f.parse::<f64>().unwrap();
(format! { "{f}f64" }.into(), true)
}
(Literal::Float(f), CodegenTy::OrderedF64) => {
let f = f.parse::<f64>().unwrap();
(format! { "::pilota::OrderedFloat({f}f64)" }.into(), true)
}
(
l,
CodegenTy::Adt(AdtDef {
Expand Down Expand Up @@ -741,7 +745,7 @@ impl Context {
}

if !self.change_case {
return self.node(def_id).unwrap().name().0.into();
return node.name().0.into();
}

match self.node(def_id).unwrap().kind {
Expand Down
11 changes: 11 additions & 0 deletions pilota-build/src/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub enum TyKind {
UInt64,
F32,
F64,
OrderedF64,
Uuid,
Vec(Arc<Ty>),
Set(Arc<Ty>),
Expand Down Expand Up @@ -68,6 +69,7 @@ pub enum CodegenTy {
UInt64,
F32,
F64,
OrderedF64,
Uuid,
Bytes,
LazyStaticRef(Arc<CodegenTy>),
Expand Down Expand Up @@ -111,6 +113,7 @@ impl CodegenTy {
CodegenTy::I32 => "i32".into(),
CodegenTy::I64 => "i64".into(),
CodegenTy::F64 => "f64".into(),
CodegenTy::OrderedF64 => "::pilota::OrderedFloat<f64>".into(),
CodegenTy::UInt32 => "u32".into(),
CodegenTy::UInt64 => "u64".into(),
CodegenTy::F32 => "f32".into(),
Expand Down Expand Up @@ -174,6 +177,7 @@ impl Display for CodegenTy {
CodegenTy::I32 => f.write_str("i32"),
CodegenTy::I64 => f.write_str("i64"),
CodegenTy::F64 => f.write_str("f64"),
CodegenTy::OrderedF64 => f.write_str("::pilota::OrderedFloat<f64>"),
CodegenTy::UInt32 => f.write_str("u32"),
CodegenTy::UInt64 => f.write_str("u64"),
CodegenTy::F32 => f.write_str("f32"),
Expand Down Expand Up @@ -297,6 +301,11 @@ pub trait TyTransformer {
CodegenTy::F64
}

#[inline]
fn ordered_f64(&self) -> CodegenTy {
CodegenTy::OrderedF64
}

#[inline]
fn f32(&self) -> CodegenTy {
CodegenTy::F32
Expand Down Expand Up @@ -356,6 +365,7 @@ pub trait TyTransformer {
I32 => self.i32(),
I64 => self.i64(),
F64 => self.f64(),
OrderedF64 => self.ordered_f64(),
Uuid => self.uuid(),
Vec(ty) => self.vec(ty),
Set(ty) => self.set(ty),
Expand Down Expand Up @@ -467,6 +477,7 @@ pub(crate) fn fold_ty<F: Folder>(f: &mut F, ty: &Ty) -> Ty {
I32 => TyKind::I32,
I64 => TyKind::I64,
F64 => TyKind::F64,
OrderedF64 => TyKind::OrderedF64,
Uuid => TyKind::Uuid,
Vec(ty) => TyKind::Vec(f.fold_ty(ty).into()),
Set(ty) => TyKind::Set(f.fold_ty(ty).into()),
Expand Down
35 changes: 33 additions & 2 deletions pilota-build/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,40 @@ impl Resolver {
ir::TyKind::F64 => ty::F64,
ir::TyKind::Uuid => ty::Uuid,
ir::TyKind::Vec(ty) => ty::Vec(Arc::from(self.lower_type(ty, false))),
ir::TyKind::Set(ty) => ty::Set(Arc::from(self.lower_type(ty, false))),
ir::TyKind::Set(ty) => ty::Set(Arc::from(self.lower_type_for_hash_key(ty, false))),
ir::TyKind::Map(k, v) => ty::Map(
Arc::from(self.lower_type(k, false)),
Arc::from(self.lower_type_for_hash_key(k, false)),
Arc::from(self.lower_type(v, false)),
),
ir::TyKind::Path(p) => ty::Path(self.lower_path(p, Namespace::Ty, is_args)),
ir::TyKind::UInt64 => ty::UInt64,
ir::TyKind::UInt32 => ty::UInt32,
ir::TyKind::F32 => ty::F32,
};
let tags_id = self.tags_id_counter.inc_one();

self.tags.insert(tags_id, ty.tags.clone());

Ty { kind, tags_id }
}

fn lower_type_for_hash_key(&mut self, ty: &ir::Ty, is_args: bool) -> Ty {
let kind = match &ty.kind {
ir::TyKind::String => ty::FastStr,
ir::TyKind::Void => ty::Void,
ir::TyKind::U8 => ty::U8,
ir::TyKind::Bool => ty::Bool,
ir::TyKind::Bytes => ty::Bytes,
ir::TyKind::I8 => ty::I8,
ir::TyKind::I16 => ty::I16,
ir::TyKind::I32 => ty::I32,
ir::TyKind::I64 => ty::I64,
ir::TyKind::F64 => ty::OrderedF64,
ir::TyKind::Uuid => ty::Uuid,
ir::TyKind::Vec(ty) => ty::Vec(Arc::from(self.lower_type_for_hash_key(ty, false))),
ir::TyKind::Set(ty) => ty::Set(Arc::from(self.lower_type_for_hash_key(ty, false))),
ir::TyKind::Map(k, v) => ty::Map(
Arc::from(self.lower_type_for_hash_key(k, false)),
Arc::from(self.lower_type(v, false)),
),
ir::TyKind::Path(p) => ty::Path(self.lower_path(p, Namespace::Ty, is_args)),
Expand Down
5 changes: 4 additions & 1 deletion pilota-build/src/test/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ mod tests {

let err = A::decode(&mut TBinaryProtocol::new(&mut data, false)).unwrap_err();

assert_eq!(err.to_string(), "Protocol(ProtocolException { kind: InvalidData, message: \"decode struct `A` field(#1) failed, caused by: decode struct `B` field(#1) failed, caused by: invalid ttype 100\" })")
assert_eq!(
err.to_string(),
"Protocol(ProtocolException { kind: InvalidData, message: \"decode struct `A` field(#1) failed, caused by: decode struct `B` field(#1) failed, caused by: invalid ttype 100\" })"
)
}
}
Loading
Loading