diff_json 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8e7db1864f01af8a4b9358314fb1780e685f91a76a0e31a336b4d25c004631ff
4
- data.tar.gz: b7243d22d10d0f6e5319c0063eff1158b488fa9548113df53a2ccfe343ce5fbb
3
+ metadata.gz: '08af726b4ce91a316f8143a343d18ee82207d95afda7ccd08ad22f9f416cb913'
4
+ data.tar.gz: 39a56823c4a9ea78ba38f418a3242fc998ae394adaca7e72f09c7b86f07fe306
5
5
  SHA512:
6
- metadata.gz: 9212e41b30d1af3b8de20c1af4a74b4b1c48dffdacad635edbbf7d6096502329a1eef5a084b52a75afb421cf47bbc9f7a00a9d660b1e165227ddc9461e0e02e6
7
- data.tar.gz: eef4f38448a7c71c37dd36ed1e3cc832b6bdeee81bc9656b9c4d38fa6d94cc43e2c813313a78baf6ec72f5b7987f89664f5ba8e992e5c408f54f4d7bb0cc701d
6
+ metadata.gz: 93fb867871e15cd864b7558dfe7b6211ce5f1e59c4659c3d4f2f5efb0f2a37d8c041b2a041785d36ec375a7dddf2233030f014ccf1c31b7164985707ad81efbf
7
+ data.tar.gz: 56e4b5053dca4018508a458fb359075c2e8e3ad738b8ef929843a9430c2f84a7eca6d3377f71493e880f2156b303ab758985e71356d60f454d655bf1179d6f66
data/lib/diff_json.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'json'
2
+ require 'logger'
2
3
  require 'require_all'
3
4
  require_relative './diff_json/diff'
4
5
  require_rel './diff_json/output'
@@ -36,7 +36,10 @@ module DiffJson
36
36
  path_sort: :sorted,
37
37
  sub_diffs: {},
38
38
  track_array_moves: true,
39
- track_structure_updates: false
39
+ track_structure_updates: false,
40
+ replace_primitives_arrays: false,
41
+ logger: ::Logger.new(STDOUT),
42
+ log_level: :warn
40
43
  }.merge(opts)
41
44
  # Create map of both JSON objects
42
45
  @old_map = map_json(old_json, '', 0)
@@ -84,5 +87,18 @@ module DiffJson
84
87
  def sub_diffs
85
88
  return @sub_diffs
86
89
  end
90
+
91
+ def log_message(log_level, message)
92
+ log_levels = [
93
+ :debug,
94
+ :info,
95
+ :warn,
96
+ :error
97
+ ]
98
+
99
+ if (log_levels.index(log_level) || -1) >= (log_levels.index(@opts[:log_level]) || 0)
100
+ @opts[:logger].method(log_level).call((is_structure?(message) ? JSON.pretty_generate(message) : message))
101
+ end
102
+ end
87
103
  end
88
104
  end
@@ -18,7 +18,7 @@ module JsonDiffing
18
18
 
19
19
  diff_operations.merge!(element_operations)
20
20
  else
21
- diff_operations[base_path] = [{op: :replace, path: base_path, value: new_element}] unless old_element == new_element
21
+ diff_operations[base_path] = [{op: :replace, path: base_path, from: old_element, value: new_element}] unless old_element == new_element
22
22
  end
23
23
 
24
24
  return diff_operations
@@ -31,6 +31,13 @@ module JsonDiffing
31
31
  add_drop_operations = {}
32
32
  last_shared_index = (old_array.length - 1)
33
33
 
34
+ if @opts[:replace_primitives_arrays]
35
+ if @old_map[base_path][:array_type] == :primitives and @new_map[base_path][:array_type] == :primitives
36
+ diff_operations[base_path] = [{op: :replace, path: base_path, from: old_array, value: new_array}]
37
+ return diff_operations
38
+ end
39
+ end
40
+
34
41
  if @opts[:track_array_moves]
35
42
  old_array_map = old_array.each_with_index.map{|v,i| [i, v]}
36
43
  new_array_map = new_array.each_with_index.map{|v,i| [i, v]}
@@ -64,8 +71,9 @@ module JsonDiffing
64
71
  last_shared_index = new_array.length - 1
65
72
 
66
73
  old_array[(new_array.length)..(old_array.length - 1)].each_with_index do |value, i|
67
- element_path = "#{base_path}/#{(old_array.length + i)}"
68
- add_drop_operations[element_path] = [{op: :remove, path: element_path}]
74
+ element_index = (new_array.length + i)
75
+ element_path = "#{base_path}/#{element_index}"
76
+ add_drop_operations[element_path] = [{op: :remove, path: element_path, value: old_array[element_index]}]
69
77
 
70
78
  if @opts[:track_array_moves]
71
79
  element_move_search = possible_moves.select{|x| x[:from] == element_path}
@@ -48,9 +48,26 @@ module JsonMapping
48
48
  return gathered_paths
49
49
  end
50
50
 
51
+ def is_structure?(value)
52
+ return (value.is_a?(Array) or value.is_a?(Hash))
53
+ end
54
+
51
55
  def element_metadata(path, value, **overrides)
52
56
  hash_list = (value.is_a?(Array) ? value.map{|x| x.hash} : [])
53
- is_structure = (value.is_a?(Array) or value.is_a?(Hash))
57
+ is_structure = is_structure?(value)
58
+ array_type = nil
59
+
60
+ if is_structure and value.is_a?(Array)
61
+ structure_detection = value.map{|v| is_structure?(v)}.uniq
62
+
63
+ array_type = if structure_detection.empty?
64
+ :empty
65
+ elsif structure_detection.length > 1
66
+ :mixed
67
+ else
68
+ (structure_detection.first ? :structures : :primitives)
69
+ end
70
+ end
54
71
 
55
72
  return {
56
73
  :hash_list => hash_list,
@@ -60,6 +77,7 @@ module JsonMapping
60
77
  :length => (is_structure ? value.length : nil),
61
78
  :trailing_comma => false,
62
79
  :type => (is_structure ? value.class.name.downcase.to_sym : :primitive),
80
+ :array_type => array_type,
63
81
  :value => value
64
82
  }.merge(overrides)
65
83
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: diff_json
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh MacLachlan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-08-12 00:00:00.000000000 Z
11
+ date: 2021-06-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: require_all
@@ -24,8 +24,8 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
- description: Diffs two JSON objects and returns a left/right diff view, similar to
28
- the command line `diff` utility
27
+ description: Diffs two JSON objects and returns a JSON patch, or a left/right diff
28
+ view, similar to the command line `diff` utility
29
29
  email: josh.t.maclachlan@gmail.com
30
30
  executables: []
31
31
  extensions: []
@@ -39,7 +39,7 @@ files:
39
39
  - lib/diff_json/output/undefined_value.rb
40
40
  homepage: https://github.com/jtmaclachlan/diff_json
41
41
  licenses:
42
- - GPL-2
42
+ - GPL-2.0
43
43
  metadata: {}
44
44
  post_install_message:
45
45
  rdoc_options: []
@@ -56,10 +56,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
56
56
  - !ruby/object:Gem::Version
57
57
  version: '0'
58
58
  requirements: []
59
- rubyforge_project:
60
- rubygems_version: 2.7.6
59
+ rubygems_version: 3.0.3
61
60
  signing_key:
62
61
  specification_version: 4
63
- summary: Diffs two JSON objects and returns a left/right diff view, similar to the
64
- command line `diff` utility
62
+ summary: Diffs two JSON objects and returns a JSON patch, or a left/right diff view,
63
+ similar to the command line `diff` utility
65
64
  test_files: []