【问题标题】:Ruby Yaml serialisation issueRuby Yaml 序列化问题
【发布时间】:2015-04-29 15:45:52
【问题描述】:

序列化的奇怪问题,可能是我做错了,但不确定是什么。

提前感谢您的帮助。

输入yaml文件

---
ssl_user_clients:
  - first_name: donnald
    surname: duck
    email: donnald.duck@acme.corp
    country: fantasyland
    state: desert
    locality: Disney
    org_name: Acme corp

预期的 yaml 文档

---
ssl_user_clients:
  - first_name: donnald
    surname: duck
    password: k)NzzC+&Dg?-RY|0
    email: donnald.duck@acme.corp
    country: fantasyland
    state: desert
    locality: Disney
    org_name: Acme corp

我的代码的奇怪结果:

---
ssl_user_clients:
- &1
  first_name: donnald
  surname: duck
  email: donnald.duck@acme.corp
  country: fantasyland
  state: desert
  locality: Disney
  org_name: Acme corp
*1:
  first_name: donnald
  surname: duck
  email: donnald.duck@acme.corp
  country: fantasyland
  state: desert
  locality: Disney
  org_name: Acme corp
  password: k)NzzC+&Dg?-RY|0

我的 Ruby 代码:

#!/usr/bin/env ruby

require "yaml"

def generate_activation_code(size = 16)
  charset = %w{0 1 2 3 4 6 7 9 A C D E F G H J K M N P Q R T V W X Y Z a b c d e f g h i j k m n o p q r s t u v w x y z ! @ $ % ^ & * ( ) _ ? ~ + - = / \ | < > { } [ ]}
  (0...size).map{ charset.to_a[rand(charset.size)] }.join
end


def get_cleaned_password
    password = generate_activation_code
    password.split().collect{|x| x.strip}.join()
end


hash = YAML.load(File.read(ARGV[0]))

puts "Hash from file"
puts hash
puts
puts


for user in hash["ssl_user_clients"]
    if not user["password"]
        new_password = get_cleaned_password
    end

    pass = { "password" => new_password }
    hash[user] = user.merge pass
end


puts hash.to_yaml

open(ARGV[0], File::TRUNC) {}
File.open(ARGV[0], 'w') {|f| f.write hash.to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true ) }

请有人帮忙,因为这让我很困惑,我真的没想到会看到这个。

一些调试信息。

Hash from file
{"ssl_user_clients"=>[{"first_name"=>"donnald", "surname"=>"duck", "email"=>"donnald.duck@acme.corp", "country"=>"fantasyland", "state"=>"desert", "locality"=>"Disney", "org_name"=>"Acme corp"}]}

Hash before hash.to_yaml
{"ssl_user_clients"=>[{"first_name"=>"donnald", 
                        "surname"=>"duck",
                        "email"=>"donnald.duck@acme.corp",       
                        "country"=>"fantasyland", 
                        "state"=>"desert", 
                        "locality"=>"Disney",
                        "org_name"=>"Acme corp"}], 
                      {"first_name"=>"donnald", 
                        "surname"=>"duck", 
                        "email"=>"donnald.duck@acme.corp",
                        "country"=>"fantasyland", 
                        "state"=>"desert", 
                        "locality"=>"Disney", 
                        "org_name"=>"Acme corp"}=>{"first_name"=>"donnald",
                        "surname"=>"duck", 
                        "email"=>"donnald.duck@acme.corp",
                        "country"=>"fantasyland", 
                        "state"=>"desert", 
                        "locality"=>"Disney", 
                        "org_name"=>"Acme corp", 
                        "password"=>"4?zkoff3^hmMC-<7"}}

如下详述的更改修复了 yaml 的损坏,但输出存在缩进问题。

好东西,这按预期工作,但是输出存在缩进问题。

以下内容根据@infused(感谢)给出的建议修复了以下问题:

for user in hash["ssl_user_clients"]
    if not user["password"]
        user["password"] = get_cleaned_password
    end
end

按预期正确输出。

---
ssl_user_clients:
  - first_name: donnald
    surname: duck
    email: donnald.duck@acme.corp
    country: fantasyland
    state: desert
    locality: Disney
    org_name: Acme corp
    password: =)%DoHHpyRrx|?v=

我假设以下内容可以确保缩进是正确的。

to_yaml( :Indent => 4, :UseHeader => true, :UseVersion => true ) }

【问题讨论】:

    标签: ruby serialization yaml


    【解决方案1】:

    您正在修改for user in hash["ssl_user_clients"] 循环中的哈希,因为user 包含一个哈希,并且您正在使用user 作为键创建一个新元素。

    以这种方式更新密码:

    for user in hash["ssl_user_clients"]
      user['password'] = get_cleaned_password
    end
    

    现在hash 将为每个用户更新密码,您可以使用puts hash.to_yaml 进行验证。

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 2011-10-16
      • 2010-12-10
      • 2013-08-21
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多