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

fixed chained dot value referencing in functions (for record/list parameter types) #1428

Merged
merged 3 commits into from
Oct 27, 2023
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
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.

Loading