@@ -52,7 +52,7 @@ def _simplify_json_row(r, common_keys):
52
52
return r
53
53
54
54
55
- def compare (previous , current , show_unchanged = False ):
55
+ def compare (previous , current , show_unchanged = False , fields = None , ignorefields = None ):
56
56
result = {
57
57
"added" : [],
58
58
"removed" : [],
@@ -64,45 +64,60 @@ def compare(previous, current, show_unchanged=False):
64
64
previous_columns = set (next (iter (previous .values ())).keys ())
65
65
current_columns = set (next (iter (current .values ())).keys ())
66
66
ignore_columns = None
67
- if previous_columns != current_columns :
68
- result ["columns_added" ] = [
69
- c for c in current_columns if c not in previous_columns
70
- ]
71
- result ["columns_removed" ] = [
72
- c for c in previous_columns if c not in current_columns
73
- ]
74
- ignore_columns = current_columns .symmetric_difference (previous_columns )
67
+
68
+ # Apply fields/ignorefields filtering
69
+ if fields :
70
+ compare_columns = set (fields )
71
+ elif ignorefields :
72
+ compare_columns = previous_columns | current_columns
73
+ compare_columns = compare_columns - set (ignorefields )
74
+ else :
75
+ compare_columns = previous_columns | current_columns
76
+
77
+ # Adjust columns_added/removed based on compare_columns
78
+ result ["columns_added" ] = [c for c in current_columns if c not in previous_columns and c in compare_columns ]
79
+ result ["columns_removed" ] = [c for c in previous_columns if c not in current_columns and c in compare_columns ]
80
+ ignore_columns = (previous_columns | current_columns ) - compare_columns
81
+
75
82
# Have any rows been removed or added?
76
83
removed = [id for id in previous if id not in current ]
77
84
added = [id for id in current if id not in previous ]
78
- # How about changed?
79
85
removed_or_added = set (removed ) | set (added )
80
86
potential_changes = [id for id in current if id not in removed_or_added ]
81
- changed = [id for id in potential_changes if current [id ] != previous [id ]]
87
+ changed = [
88
+ id for id in potential_changes
89
+ if any (
90
+ (k in compare_columns ) and (current [id ].get (k ) != previous [id ].get (k ))
91
+ for k in compare_columns
92
+ )
93
+ ]
82
94
if added :
83
95
result ["added" ] = [current [id ] for id in added ]
84
96
if removed :
85
97
result ["removed" ] = [previous [id ] for id in removed ]
86
98
if changed :
87
99
for id in changed :
88
- diffs = list (diff (previous [id ], current [id ], ignore = ignore_columns ))
100
+ diffs = list (diff (
101
+ previous [id ], current [id ],
102
+ ignore = ignore_columns if ignore_columns else None
103
+ ))
89
104
if diffs :
90
105
changes = {
91
106
"key" : id ,
92
107
"changes" : {
93
- # field can be a list if id contained '.' - #7
94
108
field [0 ] if isinstance (field , list ) else field : [
95
109
prev_value ,
96
110
current_value ,
97
111
]
98
112
for _ , field , (prev_value , current_value ) in diffs
113
+ if (field [0 ] if isinstance (field , list ) else field ) in compare_columns
99
114
},
100
115
}
101
116
if show_unchanged :
102
117
changes ["unchanged" ] = {
103
118
field : value
104
119
for field , value in previous [id ].items ()
105
- if field not in changes ["changes" ] and field != "id"
120
+ if field not in changes ["changes" ] and field != "id" and field in compare_columns
106
121
}
107
122
result ["changed" ].append (changes )
108
123
return result
0 commit comments