move logic from rec_merge to normalize_service

Signed-off-by: Sergei Biriukov <svbiriukov@gmail.com>
This commit is contained in:
Sergei Biriukov
2023-05-06 15:56:15 +10:00
committed by Muayyad Alsadi
parent d1509468c3
commit 79bfad103c
4 changed files with 129 additions and 129 deletions

View File

@ -61,15 +61,6 @@ def is_list(list_object):
)
def is_list_of_str(list_of_str_object):
if is_list(list_of_str_object):
for element in list_of_str_object:
if not is_str(element):
return False
return True
return False
# identity filter
def filteri(a):
return filter(lambda i: i, a)
@ -1264,6 +1255,10 @@ def normalize_service(service, sub_dir=""):
service["build"] = context
else:
service["build"]["context"] = context
for key in ("command", "entrypoint"):
if key in service:
if is_str(service[key]):
service[key] = shlex.split(service[key])
for key in ("env_file", "security_opt", "volumes"):
if key not in service:
continue
@ -1300,20 +1295,6 @@ def clone(value):
return value.copy() if is_list(value) or is_dict(value) else value
def clone_shell_value(target, key, value):
if is_str(value):
target[key] = shlex.split(value)
else:
target[key] = clone(value)
def check_shell_value_type(key, value):
if not is_str(value) and not is_list_of_str(value):
raise ValueError(
f"can't merge value of [{key}]: must be a string or a list of strings"
)
def rec_merge_one(target, source):
"""
update target from source recursively
@ -1322,26 +1303,17 @@ def rec_merge_one(target, source):
for key, value in source.items():
if key in target:
continue
if key in ("command", "entrypoint"):
check_shell_value_type(key, value)
clone_shell_value(target, key, value)
else:
target[key] = clone(value)
target[key] = clone(value)
done.add(key)
for key, value in target.items():
if key in done:
continue
if key in ("command", "entrypoint"):
if key not in source:
check_shell_value_type(key, value)
clone_shell_value(target, key, value)
else:
check_shell_value_type(key, source[key])
clone_shell_value(target, key, source[key])
continue
if key not in source:
continue
value2 = source[key]
if key in ("command", "entrypoint"):
target[key] = clone(value2)
continue
if not isinstance(value2, type(value)):
value_type = type(value)
value2_type = type(value2)