Skip to content

Commit

Permalink
Fix map key type
Browse files Browse the repository at this point in the history
  • Loading branch information
lok52 committed Mar 12, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent adfef22 commit 1f3367b
Showing 2 changed files with 24 additions and 3 deletions.
17 changes: 16 additions & 1 deletion actix-prost-build/src/conversions.rs
Original file line number Diff line number Diff line change
@@ -365,6 +365,21 @@ impl ConversionsGenerator {
Kind::Message(m) => Some(m),
_ => None,
}?;
let map_key_rust_type = match map_type.map_entry_key_field().kind() {
Kind::String => quote!(::prost::alloc::string::String),
Kind::Int32 => quote!(i32),
Kind::Int64 => quote!(i64),
Kind::Uint32 => quote!(u32),
Kind::Uint64 => quote!(u64),
Kind::Sint32 => quote!(i32),
Kind::Sint64 => quote!(i64),
Kind::Fixed32 => quote!(u32),
Kind::Fixed64 => quote!(u64),
Kind::Sfixed32 => quote!(i32),
Kind::Sfixed64 => quote!(i64),
Kind::Bool => quote!(bool),
_ => panic!("Map key type not supported"),
};
// TODO: Proto name might not be the same as Rust struct name
let rust_struct_name = self.messages.get(map_value_type.name())?.ident.clone();

@@ -379,7 +394,7 @@ impl ConversionsGenerator {
} else {
panic!("Type of map field is not a path")
};
let ty = quote!(#map_collection<String, #new_struct_name>);
let ty = quote!(#map_collection<#map_key_rust_type, #new_struct_name>);
let conversion = quote!(#convert::try_convert(from.#name)?);
Some((ty, conversion))
}
10 changes: 8 additions & 2 deletions tests/src/proto/conversions.rs
Original file line number Diff line number Diff line change
@@ -160,7 +160,10 @@ impl convert_trait::TryConvert<Nested> for NestedInternal {
}
#[derive(Debug)]
pub struct ConversionsRequestInternal {
pub map_field: ::std::collections::HashMap<String, MapValueInternal>,
pub map_field: ::std::collections::HashMap<
::prost::alloc::string::String,
MapValueInternal,
>,
pub query: ::prost::alloc::string::String,
pub addresses: std::collections::HashSet<ethers::types::Address>,
pub nested_enum: conversions_request::NestedEnum,
@@ -201,7 +204,10 @@ impl convert_trait::TryConvert<MapValueInternal> for MapValue {
pub struct ConversionsResponseInternal {
pub address: ethers::types::Address,
pub nested: ::core::option::Option<NestedInternal>,
pub map_field: ::std::collections::HashMap<String, MapValueInternal>,
pub map_field: ::std::collections::HashMap<
::prost::alloc::string::String,
MapValueInternal,
>,
}
impl convert_trait::TryConvert<ConversionsResponseInternal> for ConversionsResponse {
fn try_convert(from: ConversionsResponseInternal) -> Result<Self, String> {

0 comments on commit 1f3367b

Please sign in to comment.