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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/ysdiff +11 -1
  3. data/lib/yaml_search_diff.rb +46 -11
  4. metadata +17 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fe797299e547a76b27a00f2ec699e2e7c6b65914ebc9f933a695e0f9ecb5e0a1
4
- data.tar.gz: 3536d4575f5928597a901507065fc9c6e756ad5b8b08452b4dfae348175aeb1f
3
+ metadata.gz: 0a964579c858c001a695e4e8980610a173e8a9ecda23314d6176ae4bc479b7f5
4
+ data.tar.gz: 3f433f709a403a3b763acd73e284fee83af3ab56ea6274fe5a49026c6a739c75
5
5
  SHA512:
6
- metadata.gz: a7895768fbc626464c82234b09d4bb993211bbe6e5bb3bf3c9e96ac28e204e2ff2e04150d409dce400817c41678ab208908e93f306d18298b1120517bf41b568
7
- data.tar.gz: 04e8bdac6a64bb9fe2dbff84622b6c2a40daa7f66e758daff934817f33e1acad519758247c7ba310fb5e120a8e01ac84d436f71bb1462cc4b20bb1b8f82eeaf1
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
- puts YamlSearchDiff.run(key: key, yml_1: yml_1, yml_2: yml_2)
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
@@ -2,28 +2,63 @@ require 'yaml'
2
2
  require 'diffy'
3
3
 
4
4
  class YamlSearchDiff
5
- def self.run(key:, yml_1:, yml_2:)
6
- partial_1 = dfs(yml_1, key)
7
- partial_2 = dfs(yml_2, key)
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(nested_sort_hash(partial_1)),
11
- YAML.dump(nested_sort_hash(partial_2))
17
+ YAML.dump(searched_1),
18
+ YAML.dump(searched_2)
12
19
  )
13
20
  end
14
21
 
15
22
  private
16
23
 
17
- def self.dfs(hash, key)
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
- return hash[k] if k == key
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
- nil
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 self.nested_sort_hash(hash)
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.sort.to_h
69
+ hash.sort_by { |k, v| k.to_s }.to_h
35
70
  end
36
71
 
37
- def self.nested_sort_array(array)
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.3.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-02 00:00:00.000000000 Z
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: