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: function mappings + object naming #2651

Merged
merged 1 commit into from
Nov 7, 2024
Merged
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
118 changes: 92 additions & 26 deletions crates/dojo/bindgen/src/plugins/typescript/generator/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,23 @@
buffer.iter().position(|b| b.contains(fn_wrapper)).unwrap()
}

fn generate_system_function(&self, contract_name: &str, token: &Function) -> String {
fn generate_system_function(
&self,
namespace: &str,
contract_name: &str,
token: &Function,
) -> String {
format!(
"\tconst {contract_name}_{} = async ({}) => {{
\t\ttry {{
\t\t\treturn await provider.execute(
\t\t\t\taccount,
\t\t\t\tsnAccount,
\t\t\t\t{{
\t\t\t\t\tcontractName: \"{contract_name}\",
\t\t\t\t\tentryPoint: \"{}\",
\t\t\t\t\tentrypoint: \"{}\",
\t\t\t\t\tcalldata: [{}],
\t\t\t\t}}
\t\t\t\t}},
\t\t\t\t\"{namespace}\",
\t\t\t);
\t\t}} catch (error) {{
\t\t\tconsole.error(error);
Expand All @@ -51,14 +57,17 @@
}

fn format_function_inputs(&self, token: &Function) -> String {
let inputs = vec!["account: Account".to_owned()];
let inputs = vec!["snAccount: Account".to_owned()];
token
.inputs
.iter()
.fold(inputs, |mut acc, input| {
let prefix = match &input.1 {
Token::Composite(t) => {
if t.r#type == CompositeType::Enum {
if t.r#type == CompositeType::Enum
|| (t.r#type == CompositeType::Struct
&& !t.type_path.starts_with("core"))

Check warning on line 69 in crates/dojo/bindgen/src/plugins/typescript/generator/function.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/generator/function.rs#L67-L69

Added lines #L67 - L69 were not covered by tests
{
"models."
} else {
""
Expand Down Expand Up @@ -128,16 +137,17 @@
return_idx,
) {
if let Some(insert_pos) = buffer.get_first_before_pos(",", pos, return_idx) {
buffer.insert_at(
format!(
"\n\t\t\t{}: {}_{},",
token.name.to_case(Case::Camel),
contract_name,
token.name.to_case(Case::Camel)
),
insert_pos,
return_idx,
let gen = format!(
"\n\t\t\t{}: {}_{},",
token.name.to_case(Case::Camel),
contract_name,
token.name.to_case(Case::Camel)
);
// avoid duplicating identifiers. This can occur because some contracts keeps
// camel ans snake cased functions
if !buffer.has(&gen) {
buffer.insert_at(gen, insert_pos, return_idx);
}
return;
}
}
Expand Down Expand Up @@ -169,11 +179,21 @@
self.check_imports(buffer);
let contract_name = naming::get_name_from_tag(&contract.tag);
let idx = self.setup_function_wrapper_start(buffer);
self.append_function_body(
idx,
buffer,
self.generate_system_function(contract_name.as_str(), token),

// avoid duplicating function body that can occur because some contracts keeps camel and
// snake cased identifiers
let fn_body = self.generate_system_function(
&naming::get_namespace_from_tag(&contract.tag),
contract_name.as_str(),
token,
);
if let Some(fn_idx) = fn_body.chars().position(|c| c == '=') {
let fn_name = fn_body[0..fn_idx].to_string();
if !buffer.has(&fn_name) {
self.append_function_body(idx, buffer, fn_body);
}
}

Check warning on line 195 in crates/dojo/bindgen/src/plugins/typescript/generator/function.rs

View check run for this annotation

Codecov / codecov/patch

crates/dojo/bindgen/src/plugins/typescript/generator/function.rs#L195

Added line #L195 was not covered by tests

self.setup_function_wrapper_end(contract_name.as_str(), token, buffer);
Ok(String::new())
}
Expand Down Expand Up @@ -215,16 +235,17 @@
fn test_generate_system_function() {
let generator = TsFunctionGenerator {};
let function = create_change_theme_function();
let expected = "\tconst actions_changeTheme = async (account: Account, value: number) => \
{
let expected = "\tconst actions_changeTheme = async (snAccount: Account, value: number) \
=> {
\t\ttry {
\t\t\treturn await provider.execute(
\t\t\t\taccount,
\t\t\t\tsnAccount,
\t\t\t\t{
\t\t\t\t\tcontractName: \"actions\",
\t\t\t\t\tentryPoint: \"change_theme\",
\t\t\t\t\tentrypoint: \"change_theme\",
\t\t\t\t\tcalldata: [value],
\t\t\t\t}
\t\t\t\t},
\t\t\t\t\"onchain_dash\",
\t\t\t);
\t\t} catch (error) {
\t\t\tconsole.error(error);
Expand All @@ -236,6 +257,7 @@
assert_eq!(
expected,
generator.generate_system_function(
naming::get_namespace_from_tag(&contract.tag).as_str(),
naming::get_name_from_tag(&contract.tag).as_str(),
&function
)
Expand All @@ -246,15 +268,15 @@
fn test_format_function_inputs() {
let generator = TsFunctionGenerator {};
let function = create_change_theme_function();
let expected = "account: Account, value: number";
let expected = "snAccount: Account, value: number";
assert_eq!(expected, generator.format_function_inputs(&function))
}

#[test]
fn test_format_function_inputs_complex() {
let generator = TsFunctionGenerator {};
let function = create_change_theme_function();
let expected = "account: Account, value: number";
let expected = "snAccount: Account, value: number";
assert_eq!(expected, generator.format_function_inputs(&function))
}

Expand Down Expand Up @@ -325,6 +347,26 @@
assert_eq!(expected_3, buff[0]);
}

#[test]
fn test_setup_function_wrapper_end_does_not_duplicate_identifiers() {
let generator = TsFunctionGenerator {};
let mut buff = Buffer::new();

generator.setup_function_wrapper_end("actions", &create_change_theme_function(), &mut buff);

let expected = "\treturn {
\t\tactions: {
\t\t\tchangeTheme: actions_changeTheme,
\t\t},
\t};
}";
assert_eq!(1, buff.len());
assert_eq!(expected, buff[0]);
generator.setup_function_wrapper_end("actions", &create_change_theme_function(), &mut buff);
assert_eq!(1, buff.len());
assert_eq!(expected, buff[0]);
}

#[test]
fn test_it_generates_function() {
let generator = TsFunctionGenerator {};
Expand All @@ -340,6 +382,21 @@
assert_eq!(buffer.len(), 7);
}

#[test]
fn test_generate() {
let generator = TsFunctionGenerator {};
let mut buff = Buffer::new();
let function = create_change_theme_function();
let function_ca = create_change_theme_function_camelized();

let contract = create_dojo_contract();

let _ = generator.generate(&contract, &function, &mut buff);
assert_eq!(6, buff.len());
let _ = generator.generate(&contract, &function_ca, &mut buff);
assert_eq!(6, buff.len());
}

fn create_change_theme_function() -> Function {
create_test_function(
"change_theme",
Expand All @@ -349,6 +406,15 @@
)],
)
}
fn create_change_theme_function_camelized() -> Function {
create_test_function(
"changeTheme",
vec![(
"value".to_owned(),
Token::CoreBasic(CoreBasic { type_path: "core::integer::u8".to_owned() }),
)],
)
}

fn create_increate_global_counter_function() -> Function {
create_test_function("increase_global_counter", vec![])
Expand Down
Loading