mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
Merge pull request #24142 from basepi/dictupdate24097
Optimize dictupdate.update and add #24097 functionality
This commit is contained in:
commit
a43465d235
@ -17,23 +17,42 @@ from salt.utils.serializers.yamlex \
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def update(dest, upd):
|
||||
for key, val in six.iteritems(upd):
|
||||
try:
|
||||
if isinstance(val, OrderedDict):
|
||||
klass = OrderedDict
|
||||
def update(dest, upd, recursive_update=True):
|
||||
'''
|
||||
Recursive version of the default dict.update
|
||||
|
||||
Merges upd recursively into dest
|
||||
|
||||
If recursive_update=False, will use the classic dict.update, or fall back
|
||||
on a manual merge (helpful for non-dict types like FunctionWrapper)
|
||||
'''
|
||||
if dest is None:
|
||||
return upd
|
||||
if recursive_update:
|
||||
for key, val in six.iteritems(upd):
|
||||
try:
|
||||
if isinstance(val, OrderedDict):
|
||||
valtype = OrderedDict
|
||||
else:
|
||||
valtype = dict
|
||||
dest_subkey = dest.get(key, None)
|
||||
except AttributeError:
|
||||
dest_subkey = None
|
||||
if isinstance(dest_subkey, collections.Mapping) \
|
||||
and isinstance(val, collections.Mapping):
|
||||
ret = update(dest_subkey, val)
|
||||
dest[key] = ret
|
||||
else:
|
||||
klass = dict
|
||||
dest_subkey = dest.get(key, klass())
|
||||
dest[key] = upd[key]
|
||||
return dest
|
||||
else:
|
||||
try:
|
||||
dest.update(upd)
|
||||
except AttributeError:
|
||||
dest_subkey = None
|
||||
if isinstance(dest_subkey, collections.Mapping) \
|
||||
and isinstance(val, collections.Mapping):
|
||||
ret = update(dest_subkey, val)
|
||||
dest[key] = ret
|
||||
elif key:
|
||||
dest[key] = upd[key]
|
||||
return dest
|
||||
# this mapping is not a dict
|
||||
for k in upd:
|
||||
dest[k] = upd[k]
|
||||
return dest
|
||||
|
||||
|
||||
def merge_list(obj_a, obj_b):
|
||||
@ -47,7 +66,7 @@ def merge_list(obj_a, obj_b):
|
||||
|
||||
|
||||
def merge_recurse(obj_a, obj_b):
|
||||
copied = copy.copy(obj_a)
|
||||
copied = copy.deepcopy(obj_a)
|
||||
return update(copied, obj_b)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user