yaml-search-diff 0.3.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 +11 -1
- data/lib/yaml_search_diff.rb +46 -11
- metadata +17 -3
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
@@ -12,4 +12,14 @@ 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
|
+
ysdiff = YamlSearchDiff.new
|
21
|
+
puts ysdiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
|
22
|
+
else
|
23
|
+
STDERR.puts "Couldn't parse following files as Hash: #{invalid_files.join(', ')}"
|
24
|
+
exit 1
|
25
|
+
end
|
data/lib/yaml_search_diff.rb
CHANGED
@@ -2,28 +2,63 @@ require 'yaml'
|
|
2
2
|
require 'diffy'
|
3
3
|
|
4
4
|
class YamlSearchDiff
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@scalar_scanner = Psych::ScalarScanner.new(Psych::ClassLoader.new)
|
8
|
+
end
|
9
|
+
|
10
|
+
def run(key:, yml_1:, yml_2:)
|
11
|
+
return "" unless yml_1.is_a?(Hash) && yml_2.is_a?(Hash)
|
12
|
+
|
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)
|
8
15
|
|
9
16
|
Diffy::Diff.new(
|
10
|
-
YAML.dump(
|
11
|
-
YAML.dump(
|
17
|
+
YAML.dump(searched_1),
|
18
|
+
YAML.dump(searched_2)
|
12
19
|
)
|
13
20
|
end
|
14
21
|
|
15
22
|
private
|
16
23
|
|
17
|
-
def
|
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
|
29
|
+
|
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?
|
33
|
+
|
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)
|
18
43
|
keys = hash.keys.sort_by(&:to_s)
|
19
44
|
keys.each do |k|
|
20
|
-
|
45
|
+
throw :has_key, hash[k] if k == key
|
21
46
|
dfs(hash[k], key) if hash[k].is_a?(Hash)
|
22
47
|
end
|
23
|
-
|
48
|
+
{}
|
49
|
+
end
|
50
|
+
|
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
|
58
|
+
end
|
24
59
|
end
|
25
60
|
|
26
|
-
def
|
61
|
+
def nested_sort_hash(hash)
|
27
62
|
hash.each do |k, v|
|
28
63
|
if v.is_a?(Hash)
|
29
64
|
hash[k] = nested_sort_hash(v)
|
@@ -31,10 +66,10 @@ class YamlSearchDiff
|
|
31
66
|
hash[k] = nested_sort_array(v)
|
32
67
|
end
|
33
68
|
end
|
34
|
-
hash.
|
69
|
+
hash.sort_by { |k, v| k.to_s }.to_h
|
35
70
|
end
|
36
71
|
|
37
|
-
def
|
72
|
+
def nested_sort_array(array)
|
38
73
|
array.each_with_index do |v, i|
|
39
74
|
if v.is_a?(Hash)
|
40
75
|
array[i] = nested_sort_hash(v)
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
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.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-
|
12
|
-
dependencies:
|
11
|
+
date: 2021-07-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: diffy
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
13
27
|
description: A commandline tool to showdiff of a specific key in yaml files.
|
14
28
|
email: summertree128@gmail.com
|
15
29
|
executables:
|