yaml-search-diff 0.6.0 → 0.6.1
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/ysdiff +2 -1
- data/lib/yaml_search_diff.rb +60 -54
- 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: 0a964579c858c001a695e4e8980610a173e8a9ecda23314d6176ae4bc479b7f5
|
4
|
+
data.tar.gz: 3f433f709a403a3b763acd73e284fee83af3ab56ea6274fe5a49026c6a739c75
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1f90c9aade7f5d77e35fa7ae1b6b793f0289adb5efb6d23520c6f40754719565f24fdcc5e83f165d57b9e164e86a5b4c1f395c21f5163a12c331bd802bbe818
|
7
|
+
data.tar.gz: c9449015aa16aea33c8dacc2c4b0013d423b88ae9aaf45db7a21c16c49da21d78fbaa57a50caaf5a97ad0c08a74e72f9808c0d75c5b3517ec433bcb7d8433f3c
|
data/bin/ysdiff
CHANGED
@@ -17,7 +17,8 @@ invalid_files << file_1 unless yml_1.is_a?(Hash)
|
|
17
17
|
invalid_files << file_2 unless yml_2.is_a?(Hash)
|
18
18
|
|
19
19
|
if invalid_files.empty?
|
20
|
-
|
20
|
+
ysdiff = YamlSearchDiff.new
|
21
|
+
puts ysdiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
|
21
22
|
else
|
22
23
|
STDERR.puts "Couldn't parse following files as Hash: #{invalid_files.join(', ')}"
|
23
24
|
exit 1
|
data/lib/yaml_search_diff.rb
CHANGED
@@ -2,76 +2,82 @@ require 'yaml'
|
|
2
2
|
require 'diffy'
|
3
3
|
|
4
4
|
class YamlSearchDiff
|
5
|
-
class << self
|
6
|
-
def run(key:, yml_1:, yml_2:)
|
7
|
-
return "" unless yml_1.is_a?(Hash) && yml_2.is_a?(Hash)
|
8
5
|
|
9
|
-
|
10
|
-
|
6
|
+
def initialize
|
7
|
+
@scalar_scanner = Psych::ScalarScanner.new(Psych::ClassLoader.new)
|
8
|
+
end
|
11
9
|
|
12
|
-
|
13
|
-
|
14
|
-
YAML.dump(searched_2)
|
15
|
-
)
|
16
|
-
end
|
10
|
+
def run(key:, yml_1:, yml_2:)
|
11
|
+
return "" unless yml_1.is_a?(Hash) && yml_2.is_a?(Hash)
|
17
12
|
|
18
|
-
|
13
|
+
searched_1 = key.include?(':') ? search_dig(key, yml_1) : search(key, yml_1)
|
14
|
+
searched_2 = key.include?(':') ? search_dig(key, yml_2) : search(key, yml_2)
|
19
15
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
Diffy::Diff.new(
|
17
|
+
YAML.dump(searched_1),
|
18
|
+
YAML.dump(searched_2)
|
19
|
+
)
|
20
|
+
end
|
24
21
|
|
25
|
-
|
26
|
-
splitted_keys = key.split(':')
|
27
|
-
first_key = splitted_keys.first
|
22
|
+
private
|
28
23
|
|
29
|
-
|
30
|
-
|
24
|
+
def search(key, yml)
|
25
|
+
tokenized_key = @scalar_scanner.tokenize(key)
|
26
|
+
searched = catch(:has_key) { dfs(yml, tokenized_key) }
|
27
|
+
sort_yml(searched)
|
28
|
+
end
|
31
29
|
|
32
|
-
|
33
|
-
|
30
|
+
def search_dig(key, yml)
|
31
|
+
tokenized_keys = key.split(':').map {|k| @scalar_scanner.tokenize(k) }
|
32
|
+
return sort_yml(yml) if tokenized_keys.size.zero?
|
34
33
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
34
|
+
first_key = tokenized_keys.first
|
35
|
+
|
36
|
+
searched = catch(:has_key) { dfs(yml, first_key) }
|
37
|
+
digged = searched.dig(*tokenized_keys[1..])
|
38
|
+
|
39
|
+
sort_yml(digged)
|
40
|
+
end
|
41
|
+
|
42
|
+
def dfs(hash, key)
|
43
|
+
keys = hash.keys.sort_by(&:to_s)
|
44
|
+
keys.each do |k|
|
45
|
+
throw :has_key, hash[k] if k == key
|
46
|
+
dfs(hash[k], key) if hash[k].is_a?(Hash)
|
42
47
|
end
|
48
|
+
{}
|
49
|
+
end
|
43
50
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
end
|
51
|
+
def sort_yml(yml)
|
52
|
+
if yml.is_a?(Hash)
|
53
|
+
nested_sort_hash(yml)
|
54
|
+
elsif yml.is_a?(Array)
|
55
|
+
nested_sort_array(yml)
|
56
|
+
else
|
57
|
+
yml
|
52
58
|
end
|
59
|
+
end
|
53
60
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
end
|
61
|
+
def nested_sort_hash(hash)
|
62
|
+
hash.each do |k, v|
|
63
|
+
if v.is_a?(Hash)
|
64
|
+
hash[k] = nested_sort_hash(v)
|
65
|
+
elsif v.is_a?(Array)
|
66
|
+
hash[k] = nested_sort_array(v)
|
61
67
|
end
|
62
|
-
hash.sort_by { |k, v| k.to_s }.to_h
|
63
68
|
end
|
69
|
+
hash.sort_by { |k, v| k.to_s }.to_h
|
70
|
+
end
|
64
71
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
end
|
72
|
+
def nested_sort_array(array)
|
73
|
+
array.each_with_index do |v, i|
|
74
|
+
if v.is_a?(Hash)
|
75
|
+
array[i] = nested_sort_hash(v)
|
76
|
+
elsif v.is_a?(Array)
|
77
|
+
array[i] = nested_sort_array(v)
|
72
78
|
end
|
73
|
-
array.sort_by(&:to_s)
|
74
79
|
end
|
75
|
-
|
80
|
+
array.sort_by(&:to_s)
|
81
|
+
end
|
76
82
|
end
|
77
83
|
|
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.6.
|
4
|
+
version: 0.6.1
|
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-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: diffy
|