Merge "LHS assignment expressions didn't work with non-trivial indexation"

This commit is contained in:
Jenkins 2014-08-19 17:04:16 +00:00 committed by Gerrit Code Review
commit b7e42316fa
3 changed files with 34 additions and 8 deletions

View File

@ -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')

View File

@ -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:

View File

@ -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(