From ec87a654e908817e273ecdda50a1ea8e34391375 Mon Sep 17 00:00:00 2001 From: Rithik Seth <106665190+Heulitig@users.noreply.github.com> Date: Fri, 27 Oct 2023 13:54:21 +0530 Subject: [PATCH] chained dot value issue in functions (for record/list parameter types) (#1428) * fixed chained dot value referencing issue inside functions for list/record parameter types * fmt + clippy fixes * tests fixed --- fastn-js/src/to_js.rs | 29 ++++ .../js/69-chained-dot-value-in-functions.ftd | 55 ++++++ .../js/69-chained-dot-value-in-functions.html | 158 ++++++++++++++++++ 3 files changed, 242 insertions(+) create mode 100644 ftd/t/js/69-chained-dot-value-in-functions.ftd create mode 100644 ftd/t/js/69-chained-dot-value-in-functions.html diff --git a/fastn-js/src/to_js.rs b/fastn-js/src/to_js.rs index 470c771971..e31e3935ef 100644 --- a/fastn-js/src/to_js.rs +++ b/fastn-js/src/to_js.rs @@ -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 @@ -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) { diff --git a/ftd/t/js/69-chained-dot-value-in-functions.ftd b/ftd/t/js/69-chained-dot-value-in-functions.ftd new file mode 100644 index 0000000000..786ffb81d9 --- /dev/null +++ b/ftd/t/js/69-chained-dot-value-in-functions.ftd @@ -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 + diff --git a/ftd/t/js/69-chained-dot-value-in-functions.html b/ftd/t/js/69-chained-dot-value-in-functions.html new file mode 100644 index 0000000000..bd5811e428 --- /dev/null +++ b/ftd/t/js/69-chained-dot-value-in-functions.html @@ -0,0 +1,158 @@ + + + + + + + + + + + + + + +
This person named Sam Wan has first visited Bangalore on 27th October
Person Sam Wan lives at Sam City in Some House. His contact number is +1234-56789
First Person is Sam Ather lives at Sam Ather City at Some Other House. His contact number is +987-654321
+ +