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

fix: bindgen nested types init generation #2580

Merged
merged 4 commits into from
Oct 29, 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
101 changes: 62 additions & 39 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ inherits = "release"
lto = "fat"

[workspace.dependencies]
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.1", features = [ "abigen-rs" ] }
cainome = { git = "https://github.com/cartridge-gg/cainome", tag = "v0.4.6", features = [ "abigen-rs" ] }
dojo-utils = { path = "crates/dojo-utils" }

# metrics
Expand Down Expand Up @@ -216,7 +216,7 @@ warp = "0.3"

# gRPC
prost = "0.12"
tonic = { version = "0.11", features = [ "tls", "tls-roots", "gzip" ] }
tonic = { version = "0.11", features = [ "gzip", "tls", "tls-roots" ] }
tonic-build = "0.11"
tonic-reflection = "0.11"
tonic-web = "0.11"
Expand Down
1 change: 1 addition & 0 deletions crates/dojo-bindgen/src/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl fmt::Display for BuiltinPlugins {
}
}

#[derive(Debug)]
pub struct Buffer(Vec<String>);
impl Buffer {
pub fn new() -> Self {
Expand Down
4 changes: 2 additions & 2 deletions crates/dojo-bindgen/src/plugins/typescript/generator/erc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ const ERC_TORII_TYPES: &str = "\n\t\tERC__Balance: ERC__Balance,\n\t\tERC__Token
ERC__Token,\n\t\tERC__Transfer: ERC__Transfer,";
const ERC_TORII_INIT: &str = "
\t\tERC__Balance: {
\t\t\tfieldorder: ['balance', 'type', 'tokenmetadata'],
\t\t\tfieldOrder: ['balance', 'type', 'tokenmetadata'],
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ohayo sensei! Fix inconsistent casing in fieldOrder array.

The field 'tokenmetadata' in the fieldOrder array uses lowercase, while the actual field is defined as 'tokenMetadata'. This inconsistency could cause field matching issues.

Apply this fix:

-       fieldOrder: ['balance', 'type', 'tokenmetadata'],
+       fieldOrder: ['balance', 'type', 'tokenMetadata'],
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
\t\t\tfieldOrder: ['balance', 'type', 'tokenmetadata'],
fieldOrder: ['balance', 'type', 'tokenMetadata'],

\t\t\tbalance: '',
\t\t\ttype: 'ERC20',
\t\t\ttokenMetadata: {
\t\t\t\tfieldorder: ['name', 'symbol', 'tokenId', 'decimals', 'contractAddress'],
\t\t\t\tfieldOrder: ['name', 'symbol', 'tokenId', 'decimals', 'contractAddress'],
\t\t\t\tname: '',
\t\t\t\tsymbol: '',
\t\t\t\ttokenId: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl TsFunctionGenerator {
}

fn setup_function_wrapper_start(&self, buffer: &mut Buffer) -> usize {
let fn_wrapper = "export async function setupWorld(provider: DojoProvider) {{\n";
let fn_wrapper = "export async function setupWorld(provider: DojoProvider) {\n";

if !buffer.has(fn_wrapper) {
buffer.push(fn_wrapper.to_owned());
Expand Down
5 changes: 3 additions & 2 deletions crates/dojo-bindgen/src/plugins/typescript/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,14 @@ impl From<&Composite> for JsDefaultValue {
JsDefaultValue(format!("{}.{}", value.type_name(), value.inners[0].name))
}
cainome::parser::tokens::CompositeType::Struct => JsDefaultValue(format!(
"{{ {} }}",
"{{ fieldOrder: [{}], {} }}",
value.inners.iter().map(|i| format!("'{}'", i.name)).collect::<Vec<_>>().join(", "),
value
.inners
.iter()
.map(|i| format!("{}: {},", i.name, JsDefaultValue::from(&i.token)))
.collect::<Vec<_>>()
.join("\n")
.join(" ")
Comment on lines +127 to +134
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Ohayo sensei! Test case needs updating to match new format.

The implementation now uses space-separated fields, but the test case test_generate_type_init still expects newline-separated fields. This inconsistency should be addressed.

Update the test case to match the new format:

     let expected = "{
\t\t\tfieldOrder: ['field1', 'field2', 'field3'],
-\t\t\tfield1: 0,
-\t\t\tfield2: 0,
-\t\t\tfield3: 0,
+\t\t\tfield1: 0, field2: 0, field3: 0,
\t\t}";

Committable suggestion was skipped due to low confidence.


🛠️ Refactor suggestion

Consider maintaining newline separation for better readability.

The change to space-separated fields makes the output more compact but potentially harder to read, especially for structs with many fields or complex nested types.

Consider keeping the newline separation for better readability:

-                    .join(" ")
+                    .join("\n\t\t\t")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
"{{ fieldOrder: [{}], {} }}",
value.inners.iter().map(|i| format!("'{}'", i.name)).collect::<Vec<_>>().join(", "),
value
.inners
.iter()
.map(|i| format!("{}: {},", i.name, JsDefaultValue::from(&i.token)))
.collect::<Vec<_>>()
.join("\n")
.join(" ")
"{{ fieldOrder: [{}], {} }}",
value.inners.iter().map(|i| format!("'{}'", i.name)).collect::<Vec<_>>().join(", "),
value
.inners
.iter()
.map(|i| format!("{}: {},", i.name, JsDefaultValue::from(&i.token)))
.collect::<Vec<_>>()
.join("\n\t\t\t")

)),
_ => JsDefaultValue::from(value.type_name().as_str()),
}
Expand Down
Loading
Loading