如何比较yaml文件中的键?

发布于 2024-11-14 02:46:42 字数 95 浏览 1 评论 0原文

有两个 Ruby on Rails 国际化 yaml 文件。一份文件完整,另一份文件缺少密钥。如何比较两个 yaml 文件并查看第二个文件中缺少的键?有没有工具可以做到这一点?

There are two ruby on rails internationalization yaml files. One file is complete and another one is with missing keys. How can I compare two yaml files and see missing keys in second file? Are there any tools for doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老街孤人 2024-11-21 02:46:42

假设 file1 是正确的版本,file2 是缺少键的版本:

def compare_yaml_hash(cf1, cf2, context = [])
  cf1.each do |key, value|

    unless cf2.key?(key)
      puts "Missing key : #{key} in path #{context.join(".")}" 
      next
    end

    value2 = cf2[key]
    if (value.class != value2.class)
      puts "Key value type mismatch : #{key} in path #{context.join(".")}" 
      next
    end

    if value.is_a?(Hash)
      compare_yaml_hash(value, value2, (context + [key]))  
      next
    end
      
    if (value != value2)
      puts "Key value mismatch : #{key} in path #{context.join(".")}" 
    end    
  end
end

现在

compare_yaml_hash(YAML.load_file("file1"), YAML.load_file("file2"))

限制:如果您的 YAML 文件包含数组,则当前的实现应该扩展为支持数组。

Assuming file1 is the proper version and file2 is the version with missing keys:

def compare_yaml_hash(cf1, cf2, context = [])
  cf1.each do |key, value|

    unless cf2.key?(key)
      puts "Missing key : #{key} in path #{context.join(".")}" 
      next
    end

    value2 = cf2[key]
    if (value.class != value2.class)
      puts "Key value type mismatch : #{key} in path #{context.join(".")}" 
      next
    end

    if value.is_a?(Hash)
      compare_yaml_hash(value, value2, (context + [key]))  
      next
    end
      
    if (value != value2)
      puts "Key value mismatch : #{key} in path #{context.join(".")}" 
    end    
  end
end

Now

compare_yaml_hash(YAML.load_file("file1"), YAML.load_file("file2"))

Limitation: Current implementation should be extended to support arrays if your YAML file contains arrays.

喜你已久 2024-11-21 02:46:42

我找不到一个快速的工具来做到这一点。我决定为此编写我的自己的工具。

您可以使用 cabal 安装它:

$ cabal update
$ cabal install yamlkeysdiff

然后您可以比较两个文件这样:

$ yamlkeysdiff file1.yml file2.yml
> missing key in file2
< missing key in file1

您还可以比较文件的两个子集:

$ yamlkeysdiff "file1.yml#key:subkey" "file2.yml#otherkey"

它的行为与 diff 完全相同,您可以这样做:

$ yamlkeysdiff file1.yml file2.yml && echo Is the same || echo Is different

I couldn't find a fast tool to do that. I decided to write my own tool for this.

You can install it with cabal:

$ cabal update
$ cabal install yamlkeysdiff

Then you can diff two files this way:

$ yamlkeysdiff file1.yml file2.yml
> missing key in file2
< missing key in file1

You can also compare two subsets of the files:

$ yamlkeysdiff "file1.yml#key:subkey" "file2.yml#otherkey"

It behaves exactly like diff, you can do this:

$ yamlkeysdiff file1.yml file2.yml && echo Is the same || echo Is different
热风软妹 2024-11-21 02:46:42

differz 比较两个 YAML 文件并打印第二个文件中缺少的键。

differz show file1.yml file2.yml

它既作为一个库又作为一个命令行工具。您可以使用 gem install Differencez 安装后者。

differz compares two YAML files and prints missing keys in the second file.

differz show file1.yml file2.yml

It comes both as a library and as a command-line tool. You can install the latter with gem install differz.

一影成城 2024-11-21 02:46:42

我想提取差异以进行处理,而这里的代码片段只是打印内容。所以我的版本返回带有 diff 的哈希值。它的结构反映了原始哈希值,但值是差异的描述。

def deep_hash_diff(hash1, hash2, hash1_name = 'Hash 1', hash2_name = 'Hash 2')
  diff = {}
  (hash1.keys - hash2.keys).each do |key1|
    diff[key1] = "Present in #{hash1_name}, but not in #{hash2_name}"
  end
  (hash2.keys - hash1.keys).each do |key2|
    diff[key2] = "Present in #{hash2_name}, but not in #{hash1_name}"
  end

  (hash1.keys & hash2.keys).each do |common_key|
    if hash1[common_key].is_a?(Hash)
      if hash2[common_key].is_a?(Hash)
        res = deep_hash_diff(hash1[common_key], hash2[common_key], hash1_name, hash2_name)
        diff[common_key] = res if res.present?
      else
        diff[common_key] = "#{hash1_name} has nested hash, but #{hash2_name} just value #{hash2[common_key]}"
      end
    elsif hash2[common_key].is_a?(Hash)
      diff[common_key] = "#{hash2_name} has nested hash, but #{hash1_name} just value #{hash1[common_key]}"
    end
  end
  diff
end

然后我几乎将它用作:

res = deep_hash_diff(YAML.load_file("en.yml"), YAML.load_file("spa.yml"), 'English translation', 'Spanish translation')
puts(res.to_yaml) # for nicer output

I wanted to extract the diff to have something to work with, and the snippet here just prints stuff. So my version returns a hash with diff. It's structure mirrors the original hashes, but the values are descriptions of the differences.

def deep_hash_diff(hash1, hash2, hash1_name = 'Hash 1', hash2_name = 'Hash 2')
  diff = {}
  (hash1.keys - hash2.keys).each do |key1|
    diff[key1] = "Present in #{hash1_name}, but not in #{hash2_name}"
  end
  (hash2.keys - hash1.keys).each do |key2|
    diff[key2] = "Present in #{hash2_name}, but not in #{hash1_name}"
  end

  (hash1.keys & hash2.keys).each do |common_key|
    if hash1[common_key].is_a?(Hash)
      if hash2[common_key].is_a?(Hash)
        res = deep_hash_diff(hash1[common_key], hash2[common_key], hash1_name, hash2_name)
        diff[common_key] = res if res.present?
      else
        diff[common_key] = "#{hash1_name} has nested hash, but #{hash2_name} just value #{hash2[common_key]}"
      end
    elsif hash2[common_key].is_a?(Hash)
      diff[common_key] = "#{hash2_name} has nested hash, but #{hash1_name} just value #{hash1[common_key]}"
    end
  end
  diff
end

I've then used it pretty much as:

res = deep_hash_diff(YAML.load_file("en.yml"), YAML.load_file("spa.yml"), 'English translation', 'Spanish translation')
puts(res.to_yaml) # for nicer output
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文