From a0005db474528a7520964516e319b6a395972a7f Mon Sep 17 00:00:00 2001 From: Sergei Biriukov Date: Sat, 29 Apr 2023 13:18:16 +1000 Subject: [PATCH] add code implementing build value merge Signed-off-by: Sergei Biriukov --- podman_compose.py | 26 +++++++++++++++++++++++++- pytests/test_rec_merge_one_build.py | 2 +- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/podman_compose.py b/podman_compose.py index 59ee1dc..f01719d 100755 --- a/podman_compose.py +++ b/podman_compose.py @@ -1291,6 +1291,20 @@ def clone(value): return value.copy() if is_list(value) or is_dict(value) else value +def parse_build(build): + build_parsed = {} + if is_str(build): + build_parsed["context"] = build + elif is_dict(build): + if "context" in build: + build_parsed["context"] = build["context"] + if "dockerfile" in build: + build_parsed["dockerfile"] = build["dockerfile"] + else: + raise ValueError(f"invalid type of build value [{type(build)}]") + return build_parsed + + def rec_merge_one(target, source): """ update target from source recursively @@ -1299,11 +1313,21 @@ def rec_merge_one(target, source): for key, value in source.items(): if key in target: continue - target[key] = clone(value) + if key == "build": + target[key] = parse_build(value) + else: + target[key] = clone(value) done.add(key) for key, value in target.items(): if key in done: continue + if key == "build": + target_parsed = parse_build(value) + source_parsed = {} + if key in source: + source_parsed = parse_build(source[key]) + target["build"] = {**target_parsed, **source_parsed} + continue if key not in source: continue value2 = source[key] diff --git a/pytests/test_rec_merge_one_build.py b/pytests/test_rec_merge_one_build.py index a89619f..23efb40 100644 --- a/pytests/test_rec_merge_one_build.py +++ b/pytests/test_rec_merge_one_build.py @@ -73,4 +73,4 @@ def test_rec_merge_one_for_build_eception(): base = rec_merge_one(base, override) print("base: ", base_original) print("override: ", override) - print("expected: ", expected_exception) \ No newline at end of file + print("expected: ", expected_exception)