fixed jay_merge_add
This commit is contained in:
+29
-2
@@ -84,10 +84,11 @@ def jay_merge_update(_old, _new):
|
||||
def jay_merge_add(_old, _new):
|
||||
for k, v in _new.items():
|
||||
if isinstance(v, dict):
|
||||
r = jay_merge_add(_old.get(k, {}), v)
|
||||
if k in _old:
|
||||
_old[k].update(v)
|
||||
_old[k].update(r)
|
||||
else:
|
||||
_old[k] = v
|
||||
_old[k] = r
|
||||
else:
|
||||
_old[k] = _new[k]
|
||||
return _old
|
||||
@@ -539,3 +540,29 @@ if __name__ == '__main__':
|
||||
result = jay_merge_full({}, {'add': {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': 'coffee'}}})
|
||||
assert result == {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': 'coffee'}}
|
||||
|
||||
result = jay_merge_full({}, {'add': {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': {'coffee': {'h': 'black'}}}}})
|
||||
assert result == {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': {'coffee': {'h': 'black'}}}}
|
||||
|
||||
result = jay_merge_full({'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': {'coffee': {'h': 'black'}}}}, {'add': {'c': {'f': {'coffee': {'j': 'sugar'}}}}})
|
||||
assert result == {'a': 'hamburger', 'b': 'fries', 'c': {'e': 'coke', 'f': {'coffee': {'h': 'black', 'j': 'sugar'}}}}
|
||||
|
||||
old = {'charging': {
|
||||
'batteryStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:34Z', 'cruisingRangeElectric_km': 192}},
|
||||
'chargingStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:34Z'}},
|
||||
'chargingSettings': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:30Z'}},
|
||||
'plugStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:29Z'}}}}
|
||||
update = {'charging': {
|
||||
'batteryStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:35:46Z', 'cruisingRangeElectric_km': 191}},
|
||||
'chargingStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:35:46Z'}}}}
|
||||
add = {'charging': {
|
||||
'batteryStatus': {'value': {'currentSOC_pct': 55}}},
|
||||
'fuelStatus': {'rangeStatus': {'value': {'primaryEngine': {'currentSOC_pct': 55}}}},
|
||||
'measurements': {'fuelLevelStatus': {'value': {'currentSOC_pct': 55}}}}
|
||||
|
||||
delete = {'charging': {
|
||||
'chargingSettings': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:30Z'}},
|
||||
'plugStatus': {'value': {'carCapturedTimestamp': '2024-10-19T05:34:29Z'}}}}
|
||||
|
||||
result = jay_merge_full(old, {'update':update, 'add':add, 'delete':delete})
|
||||
assert result == old
|
||||
|
||||
|
||||
Reference in New Issue
Block a user