serialization - Ruby Yaml serialisation issue -
strange problem serialisation, doing wrong not sure is.
thanks in advance help.
input yaml document
--- ssl_user_clients: - first_name: donnald surname: duck email: donnald.duck@acme.corp country: fantasyland state: desert locality: disney org_name: acme corp
expected yaml document
--- 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
strange result of code:
--- 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
my ruby code:
#!/usr/bin/env ruby require "yaml" def generate_activation_code(size = 16) charset = %w{0 1 2 3 4 6 7 9 c d e f g h j k m n p q r t v w x y z b c d e f g h 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 file" puts hash puts puts 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 ) }
please may assist this has confounded me, did not expect see this.
some debug information.
hash 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"}}
the change detailed below fixed mangling of yaml, output has indentation issues.
great stuff, works expected there indentation issue output.
the following fixed mangling per advise given @infused (thanks) below:
for user in hash["ssl_user_clients"] if not user["password"] user["password"] = get_cleaned_password end end
correct output expected.
--- 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=
i had assumed following have ensured indentation correct.
to_yaml( :indent => 4, :useheader => true, :useversion => true ) }
you mangling hash in for user in hash["ssl_user_clients"]
loop, because user
contains hash , you're creating new element using user
key.
update passwords way:
for user in hash["ssl_user_clients"] user['password'] = get_cleaned_password end
now hash
have updated passwords each user, can verify puts hash.to_yaml
.
Comments
Post a Comment