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

feat: the WF engine should resolves escaped variables - WF-53 #568

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
16 changes: 13 additions & 3 deletions src/ui/src/builder/BuilderTemplateInput.vue
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ const handleComplete = (selectedText) => {
const full = getPath(text);
if (full === null) return;
const keyword = full.at(-1);
const replaced = text.replace(new RegExp(keyword + "$"), selectedText);
const regexKeyword = keyword.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") + "$"; // escape the keyword to handle properly on a regex
const replaced = text.replace(new RegExp(regexKeyword), selectedText);

newValue = replaced + newValue.slice(selectionEnd);
emit("input", { target: { value: newValue } });
emit("update:value", newValue);
Expand Down Expand Up @@ -144,6 +146,13 @@ const getPath = (text) => {
return raw.split(".");
};

/**
* Escape a key to support the "." and "\" in a state variable
*/
const escapeVariable = (key) => {
return key.replace("\\", "\\\\").replace(".", "\\.");
};

const handleInput = (ev) => {
emit("input", ev);
emit("update:value", ev.target.value);
Expand All @@ -168,7 +177,7 @@ const showAutocomplete = () => {

const allOptions = Object.entries(_get(ss.getUserState(), path) ?? {}).map(
([key, val]) => ({
text: key,
text: escapeVariable(key),
type: typeToString(val),
}),
);
Expand Down Expand Up @@ -196,8 +205,9 @@ function closeAutocompletion() {
closeAutocompletionJob = setTimeout(() => {
autocompleteOptions.value = [];
closeAutocompletionJob = null;
}, 100);
}, 300);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I encountered cases where the handler reaction was too fast. We may have to change this behavior in the future to have a flag to show or hide the autocompletion.

}

function abortClosingAutocompletion() {
if (!closeAutocompletionJob) return;
clearTimeout(closeAutocompletionJob);
Expand Down
12 changes: 10 additions & 2 deletions src/ui/src/renderer/useEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ export function useEvaluator(wf: Core) {
s = "";
let level = 0;

for (let i = 0; i < expr.length; i++) {
let i = 0
while (i < expr.length) {
const c = expr.charAt(i);
if (c == ".") {
if (c == "\\") {
if (i + 1 < expr.length) {
s += expr.charAt(i + 1);
i++;
}
} else if (c == ".") {
if (level == 0) {
accessors.push(s);
s = "";
Expand All @@ -44,6 +50,8 @@ export function useEvaluator(wf: Core) {
} else {
s += c;
}

i++
}

if (s) {
Expand Down
26 changes: 17 additions & 9 deletions src/writer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ def calculated_property(self,

for p in path_list:
state_proxy = self._state_proxy
path_parts = p.split(".")
path_parts = parse_state_variable_expression(p)
for i, path_part in enumerate(path_parts):
if i == len(path_parts) - 1:
local_mutation = MutationSubscription('property', p, handler, self, property_name)
Expand Down Expand Up @@ -1515,28 +1515,36 @@ def parse_expression(self, expr: str, instance_path: Optional[InstancePath] = No
s = ""
level = 0

for c in expr:
if c == ".":
i = 0
while i < len(expr):
character = expr[i]
if character == "\\":
if i + 1 < len(expr):
s += expr[i + 1]
i += 1
elif character == ".":
if level == 0:
accessors.append(s)
s = ""
else:
s += c
elif c == "[":
s += character
elif character == "[":
if level == 0:
accessors.append(s)
s = ""
else:
s += c
s += character
level += 1
elif c == "]":
elif character == "]":
level -= 1
if level == 0:
s = str(self.evaluate_expression(s, instance_path))
else:
s += c
s += character
else:
s += c
s += character

i += 1

if s:
accessors.append(s)
Expand Down
8 changes: 5 additions & 3 deletions tests/backend/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
"counter": 4,
"_private": 3,
# Used as an example of something unserialisable yet pickable
"_private_unserialisable": np.array([[1+2j, 2, 3+3j]])
"_private_unserialisable": np.array([[1+2j, 2, 3+3j]]),
"a.b": 3
}

simple_dict = {"items": {
Expand Down Expand Up @@ -157,7 +158,8 @@ def test_apply_mutation_marker(self) -> None:
'+interests': ['lamps', 'cars'],
'+name': 'Robert',
'+state\\.with\\.dots': None,
'+utfࠀ': 23
'+utfࠀ': 23,
'+a\.b': 3
}

self.sp_simple_dict.apply_mutation_marker()
Expand Down Expand Up @@ -1135,6 +1137,7 @@ def test_evaluate_expression(self) -> None:
assert e.evaluate_expression("features.eyes", instance_path) == "green"
assert e.evaluate_expression("best_feature", instance_path) == "eyes"
assert e.evaluate_expression("features[best_feature]", instance_path) == "green"
assert e.evaluate_expression("a\.b", instance_path) == 3

def test_get_context_data_should_return_the_target_of_event(self) -> None:
"""
Expand Down Expand Up @@ -1186,7 +1189,6 @@ def test_get_context_data_should_return_the_repeater_position_and_the_target_ins
assert context.get("item") == "b"
assert context.get("value") == "B"


class TestSessionManager:

sm = SessionManager()
Expand Down
Loading