blob: 69f368abeae0a5c6ce03b78046b1b39f6859a4f1 [file] [log] [blame]
# Copyright 2020 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Helper functions for Flutter LUCI configs."""
def _merge_dicts(a, b):
"""Returns a new dictionary from the result of merging a and b.
If matching values are both dicts or both lists, they will be merged (non-recursively).
Args:
a: first dict.
b: second dict (takes priority).
Returns:
Merged dict.
"""
new_dict = dict()
a = dict(a)
for k, av in a.items():
new_dict[k] = av
for k, bv in b.items():
av = a.get(k)
if type(av) == "dict" and type(bv) == "dict":
new_dict[k] = dict(av)
new_dict[k].update(bv)
elif type(av) == "list" and type(bv) == "list":
new_dict[k] = av + bv
else:
new_dict[k] = bv
return new_dict
def _merge_lists(a, b):
"""Return the result of merging two lists.
Args:
a: first list.
b: second list (takes priority).
Returns:
Merged list.
"""
merged = []
for item in b:
merged.append(item)
for item in a:
if item not in merged:
merged.append(item)
return merged
helpers = struct(merge_dicts = _merge_dicts, merge_lists = _merge_lists)