yaml-search-diff 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/ysdiff +10 -1
- data/lib/yaml_search_diff.rb +50 -33
- 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: 5e354e9c4e84ded8557e8c2e81e2b2d593fecc4d2c43bb40860f61fff8d1984c
|
4
|
+
data.tar.gz: 991c2647aedd8c768e27b25bdd1c1666acd915a3f18d27f6caa1b4ff28aacd30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11f7371de2a93afb6a1bbdeb661b34bcac376e38d5ca41eb4b75dbed5a92d0d4ec30c6b73aa223348cdb7836161d4d9af452b255d0dd5e931e5eac4903526582
|
7
|
+
data.tar.gz: e8ac694b6e5c0b87c600b04e436583914a6cde2acbd0daf6e7cfd58c55da3128520864efcd73fbba520343d77ebc23fae23bf5ed26413615f3eca66f064ea71f
|
data/bin/ysdiff
CHANGED
@@ -12,4 +12,13 @@ file_2 = ARGV[2]
|
|
12
12
|
yml_1 = open(file_1) {|f| YAML.load(f)}
|
13
13
|
yml_2 = open(file_2) {|f| YAML.load(f)}
|
14
14
|
|
15
|
-
|
15
|
+
invalid_files = []
|
16
|
+
invalid_files << file_1 unless yml_1.is_a?(Hash)
|
17
|
+
invalid_files << file_2 unless yml_2.is_a?(Hash)
|
18
|
+
|
19
|
+
if invalid_files.empty?
|
20
|
+
puts YamlSearchDiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
|
21
|
+
else
|
22
|
+
STDERR.puts "Couldn't parse following files as Hash: #{invalid_files.join(', ')}"
|
23
|
+
exit 1
|
24
|
+
end
|
data/lib/yaml_search_diff.rb
CHANGED
@@ -2,47 +2,64 @@ require 'yaml'
|
|
2
2
|
require 'diffy'
|
3
3
|
|
4
4
|
class YamlSearchDiff
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
Diffy::Diff.new(
|
10
|
-
YAML.dump(nested_sort_hash(partial_1)),
|
11
|
-
YAML.dump(nested_sort_hash(partial_2))
|
12
|
-
)
|
13
|
-
end
|
5
|
+
class << self
|
6
|
+
def run(key:, yml_1:, yml_2:)
|
7
|
+
return "" unless yml_1.is_a?(Hash) && yml_2.is_a?(Hash)
|
14
8
|
|
15
|
-
|
9
|
+
searched_1 = catch(:has_key) { dfs(yml_1, key) }
|
10
|
+
searched_2 = catch(:has_key) { dfs(yml_2, key) }
|
16
11
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
12
|
+
sorted_1 = sort_yml(searched_1)
|
13
|
+
sorted_2 = sort_yml(searched_2)
|
14
|
+
|
15
|
+
Diffy::Diff.new(
|
16
|
+
YAML.dump(sorted_1),
|
17
|
+
YAML.dump(sorted_2)
|
18
|
+
)
|
24
19
|
end
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
hash[k]
|
21
|
+
private
|
22
|
+
|
23
|
+
def dfs(hash, key)
|
24
|
+
keys = hash.keys.sort_by(&:to_s)
|
25
|
+
keys.each do |k|
|
26
|
+
throw :has_key, hash[k] if k == key
|
27
|
+
dfs(hash[k], key) if hash[k].is_a?(Hash)
|
32
28
|
end
|
29
|
+
{}
|
33
30
|
end
|
34
|
-
hash.sort.to_h
|
35
|
-
end
|
36
31
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
32
|
+
def sort_yml(yml)
|
33
|
+
if yml.is_a?(Hash)
|
34
|
+
nested_sort_hash(yml)
|
35
|
+
elsif yml.is_a?(Array)
|
36
|
+
nested_sort_array(yml)
|
37
|
+
else
|
38
|
+
yml
|
43
39
|
end
|
44
40
|
end
|
45
|
-
|
46
|
-
|
41
|
+
|
42
|
+
def nested_sort_hash(hash)
|
43
|
+
hash.each do |k, v|
|
44
|
+
if v.is_a?(Hash)
|
45
|
+
hash[k] = nested_sort_hash(v)
|
46
|
+
elsif v.is_a?(Array)
|
47
|
+
hash[k] = nested_sort_array(v)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
hash.sort_by { |k, v| k.to_s }.to_h
|
51
|
+
end
|
52
|
+
|
53
|
+
def nested_sort_array(array)
|
54
|
+
array.each_with_index do |v, i|
|
55
|
+
if v.is_a?(Hash)
|
56
|
+
array[i] = nested_sort_hash(v)
|
57
|
+
elsif v.is_a?(Array)
|
58
|
+
array[i] = nested_sort_array(v)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
array.sort_by(&:to_s)
|
62
|
+
end
|
63
|
+
end
|
47
64
|
end
|
48
65
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaml-search-diff
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Natsuki Inoue
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-07-
|
11
|
+
date: 2021-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diffy
|