-
Notifications
You must be signed in to change notification settings - Fork 22
Attempt to solve issue with Duplicating composite instance names imported from different namespaces #95
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
base: main
Are you sure you want to change the base?
Attempt to solve issue with Duplicating composite instance names imported from different namespaces #95
Changes from all commits
ff63529
45e841d
1144aa5
6de19e6
9818077
a210c2c
e39c0ba
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| use starknet::core::types::contract::{AbiEntry, AbiEvent, SierraClass, TypedAbiEvent}; | ||
| use std::collections::HashMap; | ||
|
|
||
| use crate::tokens::{Array, Composite, CompositeType, CoreBasic, Function, Token}; | ||
| use crate::tokens::{ | ||
| extract_type_path_with_depth, Array, Composite, CompositeType, CoreBasic, Function, Token, | ||
| }; | ||
| use crate::{CainomeResult, Error}; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Default)] | ||
|
|
@@ -71,8 +73,38 @@ impl AbiParser { | |
| Self::collect_entry_token(entry, &mut token_candidates)?; | ||
| } | ||
|
|
||
| // In theory this is not enough, as after deepening collisions are still possible | ||
| // Although possible in theory should not be a problem in practice. | ||
| let mut token_name_occurence: HashMap<String, usize> = HashMap::new(); | ||
| for (_, tokens) in &token_candidates { | ||
| for token in tokens { | ||
| let name = token.type_name(); | ||
| let val = token_name_occurence.entry(name).or_insert(0); | ||
| *val += 1; | ||
| } | ||
| } | ||
|
|
||
| let tokens = Self::filter_struct_enum_tokens(token_candidates); | ||
|
|
||
| let mut additional_aliases: HashMap<String, String> = HashMap::new(); | ||
| for (_, t) in &tokens { | ||
| let path = t.type_path(); | ||
| // Crotch to handle cases when type with the same typename are used | ||
| // in same contract. For example: | ||
| // enum Event { | ||
| // Event1(namespace1::Event), | ||
| // Event2(namespace2::Event) | ||
| // } | ||
| // When name that occures several times is spotted we register a type alias | ||
| // (only if there is none). Will apply those later. | ||
|
Comment on lines
+92
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, you should also provide the output of your example. You could actually refactor this logic into a function (including the one looping on the tokens to get the name occurence count). Like so, if the proposition of making this depth dynamic sounds good to you, we could avoid this extra loop when the user doesn't want cainome to auto-resolve the conflicts. You could like so provide the example + the detail of the behavior in the function documentation. 👍 |
||
| if token_name_occurence.get(&t.type_name()) > Some(&1) | ||
| && !type_aliases.contains_key(&path) | ||
| { | ||
| let alias = extract_type_path_with_depth(&path, 1); | ||
| additional_aliases.insert(path.into(), alias); | ||
| } | ||
| } | ||
|
|
||
| let mut structs = vec![]; | ||
| let mut enums = vec![]; | ||
| // This is not memory efficient, but | ||
|
|
@@ -86,6 +118,11 @@ impl AbiParser { | |
| t.apply_alias(type_path, alias); | ||
| } | ||
|
|
||
| // NOTE: it's important that user defined aliases were applied first | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Interesting note, but you didn't explain why. :) |
||
| for (type_path, alias) in &additional_aliases { | ||
| t.apply_alias(&type_path, &alias); | ||
| } | ||
|
|
||
| if let Token::Composite(ref c) = t { | ||
| all_composites.insert(c.type_path_no_generic(), c.clone()); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment could be re-written to be more instructive on why this loop is done.