Skip to content

Commit

Permalink
chained dot value issue in functions (for record/list parameter types) (
Browse files Browse the repository at this point in the history
#1428)

* fixed chained dot value referencing issue inside functions for list/record parameter types

* fmt + clippy fixes

* tests fixed
  • Loading branch information
Heulitig authored Oct 27, 2023
1 parent 2980af8 commit ec87a65
Show file tree
Hide file tree
Showing 3 changed files with 242 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fastn-js/src/to_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1034,6 +1034,17 @@ impl ExpressionGenerator {
};

if node.operator().get_variable_identifier_read().is_some() && !no_getter {
let chain_dot_operator_count = value.matches('.').count();
// When there are chained dot operator value
// like person.name, person.meta.address
if chain_dot_operator_count > 1 {
return format!(
"fastn_utils.getter({})",
get_chained_getter_string(value.as_str())
);
}

// When there is no chained dot operator value
format!("fastn_utils.getter({})", value)
} else {
value
Expand Down Expand Up @@ -1108,6 +1119,24 @@ impl ExpressionGenerator {
}
}

pub fn get_chained_getter_string(value: &str) -> String {
let chain_dot_operator_count = value.matches('.').count();
if chain_dot_operator_count > 1 {
if let Some((variable, key)) = value.rsplit_once('.') {
// Ignore values which are already resolved with get()
if key.contains("get") {
return value.to_string();
}
return format!(
"fastn_utils.getterByKey({}, \"{}\")",
get_chained_getter_string(variable),
key.replace('-', "_") // record fields are stored in snake case
);
}
}
value.to_string()
}

#[cfg(test)]
#[track_caller]
pub fn e(f: fastn_js::Ast, s: &str) {
Expand Down
55 changes: 55 additions & 0 deletions ftd/t/js/69-chained-dot-value-in-functions.ftd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
-- record Person:
caption name:
integer age:
Metadata meta:

-- record Metadata:
string address:
string phone-number:

-- string list places: Bangalore, Mumbai, Chennai, Kolkata

-- Person list people:

-- Person: Sam Ather
age: 30

-- Person.meta:
address: Sam Ather City at Some Other House
phone-number: +987-654321

-- Person: $r

-- end: people

-- Metadata meta:
address: Sam City in Some House
phone-number: +1234-56789

-- Person r: Sam Wan
age: 23
meta: $meta

-- ftd.text: $some-details(person = $r, places = $places, date = 27th October)

-- ftd.text: $more-details(p = $r)

-- ftd.text: $first-person-details(people = $people)

-- string more-details(p):
Person p:

"Person " + p.name + " lives at " + p.meta.address + ". His contact number is " + p.meta.phone-number

-- string some-details(person, places):
Person person:
string list places:
string date:

"This person named " + person.name + " has first visited " + places.0 + " on " + date

-- string first-person-details(people):
Person list people:

"First Person is " + people.0.name + " lives at " + people.0.meta.address + ". His contact number is " + people.0.meta.phone-number

158 changes: 158 additions & 0 deletions ftd/t/js/69-chained-dot-value-in-functions.html

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

0 comments on commit ec87a65

Please sign in to comment.