Add to TranslationRule REPLACE rule value_path del
If TranslationRule defined with REPLACE rule and value defined with value_path, delete value_path property data after replacing. bp deprecating-improvements Change-Id: I314ff90a10c86a11964200ca46187966507f32ef
This commit is contained in:
parent
72f8bc4abc
commit
9479c5bd94
@ -618,12 +618,12 @@ class TranslationRule(object):
|
|||||||
list-type values can be added to such properties. Using for other
|
list-type values can be added to such properties. Using for other
|
||||||
cases is prohibited and will be returned with error.
|
cases is prohibited and will be returned with error.
|
||||||
- REPLACE. This rule allows to replace some property value to another. Used
|
- REPLACE. This rule allows to replace some property value to another. Used
|
||||||
for all types of properties. Note, that if property has list type, then
|
for all types of properties. Note, that if property has list type,
|
||||||
value will be replaced for all elements of list, where it needed. If
|
then value will be replaced for all elements of list, where it
|
||||||
element in such property must be replaced by value of another element of
|
needed. If element in such property must be replaced by value of
|
||||||
this property, value_name must be defined.
|
another element of this property, value_name must be defined.
|
||||||
- DELETE. This rule allows to delete some property. If property has list
|
- DELETE. This rule allows to delete some property. If property has list
|
||||||
type, then deleting affects value in all list elements.
|
type, then deleting affects value in all list elements.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
RULE_KEYS = (ADD, REPLACE, DELETE) = ('Add', 'Replace', 'Delete')
|
RULE_KEYS = (ADD, REPLACE, DELETE) = ('Add', 'Replace', 'Delete')
|
||||||
@ -669,7 +669,7 @@ class TranslationRule(object):
|
|||||||
raise ValueError(_('Use value_name only for replacing list '
|
raise ValueError(_('Use value_name only for replacing list '
|
||||||
'elements.'))
|
'elements.'))
|
||||||
elif self.rule == self.ADD and not isinstance(self.value, list):
|
elif self.rule == self.ADD and not isinstance(self.value, list):
|
||||||
raise ValueError(_('value must be list type when rule is ADD.'))
|
raise ValueError(_('value must be list type when rule is Add.'))
|
||||||
|
|
||||||
def execute_rule(self):
|
def execute_rule(self):
|
||||||
(source_key, source_data) = self.get_data_from_source_path(
|
(source_key, source_data) = self.get_data_from_source_path(
|
||||||
@ -695,7 +695,7 @@ class TranslationRule(object):
|
|||||||
if isinstance(source_data, list):
|
if isinstance(source_data, list):
|
||||||
source_data.extend(value)
|
source_data.extend(value)
|
||||||
else:
|
else:
|
||||||
raise ValueError(_('ADD rule must be used only for '
|
raise ValueError(_('Add rule must be used only for '
|
||||||
'lists.'))
|
'lists.'))
|
||||||
elif self.rule == TranslationRule.REPLACE:
|
elif self.rule == TranslationRule.REPLACE:
|
||||||
if isinstance(source_data, list):
|
if isinstance(source_data, list):
|
||||||
@ -718,6 +718,10 @@ class TranslationRule(object):
|
|||||||
% dict(key=source_key,
|
% dict(key=source_key,
|
||||||
name=value_key))
|
name=value_key))
|
||||||
source_data[source_key] = value
|
source_data[source_key] = value
|
||||||
|
# If value defined with value_path, need to delete value_path
|
||||||
|
# property data after it's replacing.
|
||||||
|
if value_data and value_data.get(value_key):
|
||||||
|
del value_data[value_key]
|
||||||
elif self.rule == TranslationRule.DELETE:
|
elif self.rule == TranslationRule.DELETE:
|
||||||
if isinstance(source_data, list):
|
if isinstance(source_data, list):
|
||||||
for item in source_data:
|
for item in source_data:
|
||||||
|
@ -1877,7 +1877,7 @@ class TestTranslationRule(common.HeatTestCase):
|
|||||||
properties.TranslationRule.ADD,
|
properties.TranslationRule.ADD,
|
||||||
['any'],
|
['any'],
|
||||||
'value')
|
'value')
|
||||||
self.assertEqual('value must be list type when rule is ADD.',
|
self.assertEqual('value must be list type when rule is Add.',
|
||||||
six.text_type(exc))
|
six.text_type(exc))
|
||||||
|
|
||||||
def test_add_rule_exist(self):
|
def test_add_rule_exist(self):
|
||||||
@ -1969,7 +1969,7 @@ class TestTranslationRule(common.HeatTestCase):
|
|||||||
[props.get('bar')])
|
[props.get('bar')])
|
||||||
exc = self.assertRaises(ValueError, rule.execute_rule)
|
exc = self.assertRaises(ValueError, rule.execute_rule)
|
||||||
|
|
||||||
self.assertEqual('ADD rule must be used only for lists.',
|
self.assertEqual('Add rule must be used only for lists.',
|
||||||
six.text_type(exc))
|
six.text_type(exc))
|
||||||
|
|
||||||
def test_replace_rule_map_exist(self):
|
def test_replace_rule_map_exist(self):
|
||||||
@ -2110,6 +2110,7 @@ class TestTranslationRule(common.HeatTestCase):
|
|||||||
rule.execute_rule()
|
rule.execute_rule()
|
||||||
|
|
||||||
self.assertEqual('one', props.get('bar'))
|
self.assertEqual('one', props.get('bar'))
|
||||||
|
self.assertEqual('one', props.get('far'))
|
||||||
|
|
||||||
def test_replace_rule_str_value_path_error(self):
|
def test_replace_rule_str_value_path_error(self):
|
||||||
schema = {
|
schema = {
|
||||||
@ -2146,6 +2147,7 @@ class TestTranslationRule(common.HeatTestCase):
|
|||||||
rule.execute_rule()
|
rule.execute_rule()
|
||||||
|
|
||||||
self.assertEqual('one', props.get('bar'))
|
self.assertEqual('one', props.get('bar'))
|
||||||
|
self.assertIsNone(props.get('far'))
|
||||||
|
|
||||||
def test_replace_rule_str_invalid(self):
|
def test_replace_rule_str_invalid(self):
|
||||||
schema = {
|
schema = {
|
||||||
|
Loading…
Reference in New Issue
Block a user