Skip to content

Commit 49c7a83

Browse files
committed
implement set logic
1 parent 3599dab commit 49c7a83

File tree

6 files changed

+61
-39
lines changed

6 files changed

+61
-39
lines changed

src/mash/shell/ast/infix.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ def run(self, prev_result='', shell: BaseShell = None, lazy=False):
107107
line = ' '.join(quote_items([a, op, b]))
108108

109109
if op in literals.comparators:
110-
if not lazy:
111-
return Math.eval(line, shell.env)
112-
return a, op, b
110+
if lazy:
111+
return a, op, b
112+
return Math.eval(line, shell.env)
113113

114114
if op in '+-*/':
115115
# math

src/mash/shell/ast/set_definition.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from mash.shell.ast.term import Term
77
from mash.shell.base import BaseShell
88
from mash.shell.cmd2 import Mode
9+
from mash.shell.internals.helpers import enter_new_scope
10+
from mash.shell.function import InlineFunction
911

1012

1113
class SetDefinition(Node):
@@ -33,6 +35,11 @@ def run(self, prev_result='', shell: BaseShell = None, lazy=False):
3335
for item in self.items.values:
3436
key = shell.run_commands(item, '', lazy)[0]
3537
key = str(item)
38+
39+
if key.startswith('$'):
40+
# SMELL
41+
key = key[1:]
42+
3643
with shell.use_mode(Mode.COMPILE):
3744
results = shell.run_commands(item, '', not lazy)
3845

@@ -81,14 +88,18 @@ def apply(self, data: dict, shell: BaseShell = None):
8188
yield from (merge(row) for row in product(*columns))
8289
return
8390

84-
if 1:
85-
# TODO remove
86-
yield from product(*data)
87-
return
88-
for element in product(*data):
89-
result = shell.run_commands(self.condition, element)
91+
for element in product(*columns):
92+
with enter_new_scope(shell):
93+
for item in element:
94+
# shell.env.update(item)
95+
for k, v in item.items():
96+
f = InlineFunction(v, [], f'do_{k}' )
97+
shell.env[k] = f
98+
99+
result = shell.run_commands(self.condition, '', run=True)
100+
90101
if result:
91-
yield element
102+
yield merge(element)
92103

93104
def __repr__(self):
94105
if self.condition:

test/shell/test_parse.py

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -637,18 +637,4 @@ def test_parse_set_with_filter():
637637
assert isinstance(result, Map)
638638
assert isinstance(result.lhs, SetDefinition)
639639
assert isinstance(result.rhs, Terms)
640-
assert isinstance(result.rhs.values[0], NestedVariable)
641-
642-
643-
def test_parse_set_with_nested_filter():
644-
result = parse_line(r'{groups.members}')
645-
assert isinstance(result, SetDefinition)
646-
assert result.values[0] == 'groups.members'
647-
648-
result = parse_line('{ users | users.id == {groups.members}}')
649-
assert repr(result) == 'SetDefinition( users | users.id == groups.members )'
650-
assert isinstance(result, SetDefinition)
651-
assert isinstance(result.condition, BinaryExpression)
652-
assert result.condition.op == '=='
653-
assert isinstance(result.condition.lhs, Terms)
654-
assert isinstance(result.condition.rhs, SetDefinition)
640+
assert isinstance(result.rhs.values[0], NestedVariable)

test/shell/test_shell.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,40 @@ def test_shell_scope():
338338
if 0:
339339
with raises(NotImplementedError):
340340
run_command('( 1 )')
341+
342+
def test_set_definition():
343+
shell = Shell()
344+
shell.ignore_invalid_syntax = False
345+
346+
run_command('a <- range 3', shell=shell)
347+
run_command('b <- range 3', shell=shell)
348+
run_command('c <- { $a }', shell=shell)
349+
assert shell.env['c'] == [{'a': '0'}, {'a': '1'}, {'a': '2'}]
350+
351+
# TODO
352+
# run_command('c <- { a b }', shell=shell)
353+
# run_command('c <- { a b | a == b }', shell=shell)
354+
355+
def test_set_notation():
356+
shell = Shell()
357+
shell.ignore_invalid_syntax = False
358+
359+
run_command('x <- range 5', shell=shell)
360+
run_command('y <- { $x }', shell=shell)
361+
assert len(shell.env['y']) == 5
362+
assert shell.env['y'][0] == {'x': '0'}
363+
assert shell.env['y'][4] == {'x': '4'}
364+
365+
def test_set_with_condition():
366+
shell = Shell()
367+
shell.ignore_invalid_syntax = False
368+
369+
run_command('x <- range 5', shell=shell)
370+
run_command('y <- { $x | x > 2 }', shell=shell)
371+
assert len(shell.env['y']) == 2
372+
assert shell.env['y'][0] == {'x': '3'}
373+
374+
if 0:
375+
result = catch_output('{ users | users.id == {groups.members}}')
376+
result = catch_output('{ users groups | x.id == y.id }')
377+
result = catch_output('{ users groups } >>= $.users.id')

test/shell/test_shell_rest_client.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ def test_rest_client_filter_set():
132132
shell, _ = init()
133133
shell = shell.shell
134134

135-
result = catch_output('{users | .id < 1002} >>= get id', shell=shell)
135+
# TODO
136+
# result = catch_output('{users | .id < 1002} >>= get id', shell=shell)
136137
# TODO add assertions
137138
# assert '1000' in result
138139
# assert '1001' in result

test/shell/test_shell_variables.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -265,16 +265,3 @@ def test_variable_assignment_with_if_then():
265265

266266
run_command('a <- if "" then echo 10', shell=shell)
267267
assert shell.env['a'] == ''
268-
269-
def test_set_definition():
270-
shell = Shell()
271-
shell.ignore_invalid_syntax = False
272-
273-
run_command('a <- range 3', shell=shell)
274-
run_command('b <- range 3', shell=shell)
275-
run_command('c <- { $a }', shell=shell)
276-
assert shell.env['c'] == [{'$a': '0'}, {'$a': '1'}, {'$a': '2'}]
277-
278-
# TODO
279-
run_command('c <- { a b }', shell=shell)
280-
run_command('c <- { a b | a == b }', shell=shell)

0 commit comments

Comments
 (0)