LHS assignment expressions didn't work with non-trivial indexation
Most of assignment expressions involving indexation operator except for the most trivial one fail. For example the following failed: $myDict[$keyName]: value $myDict['A']['B']: value (even if $myDict['A'] is a dictionary) $myList[2 + 2]: value $myList[$index]: value This commit adds support for sub-expressions within LHS-expressions Change-Id: I4b72e85b5f9bda04c6156d5c8b07eb8f5358643d Closes-Bug: #1358545
This commit is contained in:
parent
2db878ba2a
commit
8a7b2cef50
@ -45,7 +45,14 @@ class LhsExpression(object):
|
||||
self._current_obj_name = None
|
||||
|
||||
def _create_context(self, root_context, murano_class):
|
||||
def _evaluate(thing):
|
||||
if isinstance(thing, yaql.expressions.Expression.Callable):
|
||||
thing.yaql_context = root_context
|
||||
thing = thing()
|
||||
return thing
|
||||
|
||||
def _get_value(src, key):
|
||||
key = _evaluate(key)
|
||||
if isinstance(src, types.DictionaryType):
|
||||
return src.get(key)
|
||||
elif isinstance(src, types.ListType) and isinstance(
|
||||
@ -60,6 +67,7 @@ class LhsExpression(object):
|
||||
raise TypeError()
|
||||
|
||||
def _set_value(src, key, value):
|
||||
key = _evaluate(key)
|
||||
if isinstance(src, types.DictionaryType):
|
||||
old_value = src.get(key, type_scheme.NoValue)
|
||||
src[key] = value
|
||||
@ -115,9 +123,11 @@ class LhsExpression(object):
|
||||
|
||||
@yaql.context.EvalArg("this", LhsExpression.Property)
|
||||
def indexation(this, index):
|
||||
index = _evaluate(index)
|
||||
|
||||
return LhsExpression.Property(
|
||||
lambda: _get_value(this.get(), index()),
|
||||
lambda value: _set_value(this.get(), index(), value))
|
||||
lambda: _get_value(this.get(), index),
|
||||
lambda value: _set_value(this.get(), index, value))
|
||||
|
||||
context = yaql.context.Context()
|
||||
context.register_function(get_context_data, '#get_context_data')
|
||||
|
@ -45,12 +45,23 @@ Workflow:
|
||||
- $x: $.getAttr(att1)
|
||||
- $y: $.getAttr(att2, ' Doe')
|
||||
- Return: $x + $y
|
||||
|
||||
testAssignment:
|
||||
Body:
|
||||
- $x: {}
|
||||
- $x.A: [1, 2]
|
||||
- $x.A[0]: 3
|
||||
- Return: $x
|
||||
- $result: {}
|
||||
- $result.Arr: [1, 2, [10, 11]]
|
||||
- $index: 1
|
||||
- $result.Arr[0]: 3
|
||||
- $result.Arr[$index - 1]: 5
|
||||
- $result.Arr[$index + 1][1]: 123
|
||||
- $result.Dict: {}
|
||||
- $result.Dict.Key1: V1
|
||||
- $keyName: Key2
|
||||
- $result.Dict[$keyName]: {}
|
||||
- $result.Dict[$keyName]['a_b']: V2
|
||||
- $result.Dict[$keyName][toUpper($keyName)]: V3
|
||||
- Return: $result
|
||||
|
||||
|
||||
testAssignByCopy:
|
||||
Arguments:
|
||||
|
@ -29,8 +29,13 @@ class TestAssignments(test_case.DslTestCase):
|
||||
|
||||
def test_assignment(self):
|
||||
self.assertEqual(
|
||||
{'A': [3, 2]},
|
||||
self._runner.testAssignment())
|
||||
{
|
||||
'Arr': [5, 2, [10, 123]],
|
||||
'Dict': {
|
||||
'Key1': 'V1',
|
||||
'Key2': {'KEY2': 'V3', 'a_b': 'V2'}
|
||||
}
|
||||
}, self._runner.testAssignment())
|
||||
|
||||
def test_assign_by_copy(self):
|
||||
self.assertEqual(
|
||||
|
Loading…
x
Reference in New Issue
Block a user