dorian-json-compare 0.0.3 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/bin/json-compare +49 -6
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2755a13feadb01ec042f0664d7a1c9a7d456e6a977a5abf0ec30048cdf277c4
|
4
|
+
data.tar.gz: cc893d3443d496e332541f45118a1003d2b7191d34bf1a4e24762f19c45c64ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b9309557fe2e8567ea4b8946d4d1b769890773239b6985710b41d1e1e8902f7e4bb65b5f28224afdbabe22dfd57491e2548a325a56b37580543c10702afb355
|
7
|
+
data.tar.gz: e3c9a3eb1756a69dc1db8f346a47157b15f230fd254572afd64ee3f1c18b5ccd25aae43bc034f5db1ed230fe3e2b24fea47d54346c1903c7f5d7f56e7eb870ff
|
data/bin/json-compare
CHANGED
@@ -1,12 +1,55 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
require "json"
|
5
|
+
require "dorian/arguments"
|
6
|
+
require "stringio"
|
7
|
+
|
8
|
+
def compare(hash1, hash2, current: "")
|
9
|
+
if hash1.is_a?(Array)
|
10
|
+
hash1.each.with_index { |_, i| compare(hash1[i], hash2[i], current:) }
|
11
|
+
elsif hash1.is_a?(String) || hash2.is_a?(String)
|
12
|
+
if !hash1.is_a?(String)
|
13
|
+
puts "key \"#{current}\" is a string in #{ARGV[1]}"
|
14
|
+
elsif !hash2.is_a?(String)
|
15
|
+
puts "key \"#{current}\" is a string in #{ARGV[0]}"
|
16
|
+
end
|
17
|
+
else
|
18
|
+
(hash1.keys + hash2.keys).uniq.each do |key|
|
19
|
+
full_key = [current, key].reject(&:empty?).join(".")
|
20
|
+
if !hash1.key?(key)
|
21
|
+
puts "missing key \"#{full_key}\" from #{ARGV[0]}"
|
22
|
+
elsif !hash2.key?(key)
|
23
|
+
puts "missing key \"#{full_key}\" from #{ARGV[1]}"
|
24
|
+
else
|
25
|
+
compare(hash1[key], hash2[key], current: full_key)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def with_captured_stdout(&block)
|
32
|
+
original_stdout = $stdout
|
33
|
+
$stdout = StringIO.new
|
34
|
+
block.call
|
35
|
+
$stdout.string
|
36
|
+
ensure
|
37
|
+
$stdout = original_stdout
|
7
38
|
end
|
8
39
|
|
9
|
-
|
10
|
-
|
11
|
-
|
40
|
+
parsed = Dorian::Arguments.parse(version: { alias: :v }, help: { alias: :h })
|
41
|
+
|
42
|
+
abort parsed.help if parsed.options.help
|
43
|
+
|
44
|
+
if parsed.options.version
|
45
|
+
abort File.read(File.expand_path("../../VERSION", __FILE__))
|
12
46
|
end
|
47
|
+
|
48
|
+
file1, file2 = parsed.files.map { |file| JSON.parse(File.read(file)) }
|
49
|
+
root1, root2 = parsed.arguments
|
50
|
+
file1 = file1[root1] if root1
|
51
|
+
file2 = file2[root2] if root2
|
52
|
+
|
53
|
+
output = with_captured_stdout { compare(file1, file2) }
|
54
|
+
|
55
|
+
abort output unless output.empty?
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dorian-json-compare
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dorian Marié
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-08-
|
11
|
+
date: 2024-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: json
|