Skip to content

Commit

Permalink
Fix: self reference for function
Browse files Browse the repository at this point in the history
  • Loading branch information
Arpita-Jaiswal committed Mar 15, 2023
1 parent b089dd2 commit 20b8159
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 32 deletions.
82 changes: 60 additions & 22 deletions ftd/src/executor/tdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl<'a> TDoc<'a> {
pub fn resolve_all_self_references(
name: String,
component_name: &str,
map: &ftd::Map<(String, Option<String>)>,
map: &ftd::Map<(String, Vec<String>)>,
) -> String {
let mut resolved_value = name;
loop {
Expand All @@ -33,24 +33,29 @@ impl<'a> TDoc<'a> {
pub(crate) fn resolve_self_referenced_values(
&mut self,
component_name: &str,
map: &ftd::Map<(String, Option<String>)>,
map: &ftd::Map<(String, Vec<String>)>,
) -> ftd::executor::Result<ftd::Map<String>> {
let mut resolved_map: ftd::Map<String> = Default::default();

for (k, (name, has_self_reference)) in map.iter() {
let name = TDoc::resolve_all_self_references(name.clone(), component_name, map);

if let Some(has_self_reference) = has_self_reference {
if !has_self_reference.is_empty() {
let values: ftd::Map<String> = has_self_reference
.iter()
.map(|v| {
(
v.to_string(),
TDoc::resolve_all_self_references(v.to_string(), component_name, map),
)
})
.collect();
let variable = match self.bag.get_mut(name.as_str()).unwrap() {
ftd::interpreter2::Thing::Variable(v) => v,
_ => unreachable!("Reference {} is not a valid variable", name.as_str()),
};
let value = TDoc::resolve_all_self_references(
has_self_reference.clone(),
component_name,
map,
);
variable.value.set_reference_or_clone(value.as_str());

set_reference_name(&mut variable.value, &values);
}

resolved_map.insert(k.to_string(), name);
Expand All @@ -69,7 +74,7 @@ impl<'a> TDoc<'a> {
inherited_variables: &mut ftd::VecMap<(String, Vec<usize>)>,
insert_null: bool,
) -> ftd::executor::Result<ftd::Map<String>> {
let mut map: ftd::Map<(String, Option<String>)> = Default::default();
let mut map: ftd::Map<(String, Vec<String>)> = Default::default();
for argument in arguments {
if let Some((k, v, has_self_reference)) = self.insert_local_variable(
component_name,
Expand Down Expand Up @@ -98,7 +103,7 @@ impl<'a> TDoc<'a> {
line_number: usize,
inherited_variables: &mut ftd::VecMap<(String, Vec<usize>)>,
insert_null: bool,
) -> ftd::executor::Result<Option<(String, String, Option<String>)>> {
) -> ftd::executor::Result<Option<(String, String, Vec<String>)>> {
let string_container = ftd::executor::utils::get_string_container(container);
let source = argument.to_sources();
let properties = ftd::interpreter2::utils::find_properties_by_source(
Expand All @@ -118,7 +123,7 @@ impl<'a> TDoc<'a> {
// TODO: Remove unwrap()
.unwrap()
{
return Ok(Some((name_in_component_definition, name, None)));
return Ok(Some((name_in_component_definition, name, vec![])));
}
}

Expand Down Expand Up @@ -159,15 +164,7 @@ impl<'a> TDoc<'a> {
return Ok(None);
}

let self_reference = {
let mut self_reference = None;
if let Some(name) = default.get_reference_or_clone().cloned() {
if name.starts_with(format!("{}.", component_name).as_str()) {
self_reference = Some(name);
}
}
self_reference
};
let self_reference = get_self_reference(&default, component_name);

match default.reference_name() {
Some(name) if conditions.is_empty() => {
Expand All @@ -185,7 +182,11 @@ impl<'a> TDoc<'a> {
);
}

return Ok(Some((name_in_component_definition, name.to_string(), None)));
return Ok(Some((
name_in_component_definition,
name.to_string(),
vec![],
)));
}
_ => {}
}
Expand Down Expand Up @@ -234,3 +235,40 @@ impl<'a> TDoc<'a> {
)))
}
}

fn get_self_reference(
default: &ftd::interpreter2::PropertyValue,
component_name: &str,
) -> Vec<String> {
match default {
ftd::interpreter2::PropertyValue::Reference { name, .. }
| ftd::interpreter2::PropertyValue::Clone { name, .. }
if name.starts_with(format!("{}.", component_name).as_str()) =>
{
vec![name.to_string()]
}
ftd::interpreter2::PropertyValue::FunctionCall(f) => {
let mut self_reference = vec![];
for arguments in f.values.values() {
self_reference.extend(get_self_reference(arguments, component_name));
}
self_reference
}
_ => vec![],
}
}

fn set_reference_name(default: &mut ftd::interpreter2::PropertyValue, values: &ftd::Map<String>) {
match default {
ftd::interpreter2::PropertyValue::Reference { name, .. }
| ftd::interpreter2::PropertyValue::Clone { name, .. } => {
*name = values.get(name).unwrap().to_string();
}
ftd::interpreter2::PropertyValue::FunctionCall(f) => {
for arguments in f.values.values_mut() {
set_reference_name(arguments, values);
}
}
_ => {}
}
}
5 changes: 3 additions & 2 deletions ftd/t/html/52-code-and-iframe.ftd
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

-- component value:
caption code:
string clean: $ftd.clean-code(a = $value.code, lang = ftd)

-- ftd.column:
$on-click$: $ftd.copy-to-clipboard(a = $value.code)
$on-click$: $ftd.copy-to-clipboard(a = $value.clean)

-- ftd.code:
lang: ftd
theme: fastn-theme.light
text: $ftd.clean-code(a = $value.code, lang = ftd)
text: $value.code

-- end: ftd.column

Expand Down
17 changes: 11 additions & 6 deletions ftd/t/html/52-code-and-iframe.html

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

2 changes: 1 addition & 1 deletion ftd/theme/fastn-theme.dark.tmTheme
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@
<key>foreground</key>
<string>#000000</string>
<key>background</key>
<string>#030388</string>
<string>#1a1a5c</string>
</dict>
</dict>
<dict>
Expand Down
2 changes: 1 addition & 1 deletion ftd/theme/fastn-theme.light.tmTheme
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<key>foreground</key>
<string>#ffffff</string>
<key>background</key>
<string>#ffffaf</string>
<string>#ffffd7</string>
</dict>
</dict>
<dict>
Expand Down

0 comments on commit 20b8159

Please sign in to comment.