Kami commented on code in PR #1983: URL: https://github.com/apache/libcloud/pull/1983#discussion_r1581834280
########## libcloud/compute/drivers/kubevirt.py: ########## @@ -1231,3 +1719,151 @@ def ex_delete_service(self, namespace, service_name): except Exception: raise return result.status in VALID_RESPONSE_CODES + + +def _deep_merge_dict(source: dict, destination: dict) -> dict: + """ + Deep merge two dictionaries: source into destination. + For conflicts, prefer source's non-zero values over destination's. + (By non-zero, we mean that bool(value) is True.) + + Extended from https://stackoverflow.com/a/20666342, added zero value handling. + + Example:: + + >>> a = {"domain": {"devices": 0}, "volumes": [1, 2, 3], "network": {}} + >>> b = {"domain": {"machine": "non-exist-in-a", "devices": 1024}, "volumes": [4, 5, 6]} + >>> _deep_merge_dict(a, b) + {'domain': {'machine': 'non-exist-in-a', 'devices': 1024}, 'volumes': [1, 2, 3], 'network': {}} + + In the above example: + + - network: exists in source (a) but not in destination (b): add source (a)'s + - volumes: exists in both, both are non-zero: prefer source (a)'s + - devices: exists in both: source (a) is zero, destination (b) is non-zero: keep destination (b)'s + - machine: exists in destination (b) but not in source (a): reserve destination (b)'s + + :param source: RO: A dict to be merged into another. + Do not use circular dict (e.g. d = {}; d['d'] = d) as source, + otherwise a RecursionError will be raised. + :param destination: RW: A dict to be merged into. (the value will be modified). + + :return: dict: Updated destination. + """ + for key, value in source.items(): + if isinstance(value, dict): # recurse for dicts + node = destination.setdefault(key, {}) # get node or create one + _deep_merge_dict(value, node) + elif key not in destination: # not existing in destination: add it + destination[key] = value + elif value: # existing: update if source's value is non-zero + destination[key] = value + + return destination + + +def _memory_in_MB(memory): # type: (Union[str, int]) -> int Review Comment: Same here, some tests would this function would be good. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@libcloud.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org