YAML: Integers in YPATH

J

James F. Hranicky

It seems that when I do a YPATH select on a node that
contains an integer, I get nothing in return. However,
when the node is a string, the select works.

Here's the YAML file:

---------- x.yaml ------------

Domains :
my.domain.com :
Networks :
192.168.0.0-24 :
Hosts :
0 :
Name : net192-168
Ether : NONE
1 :
Name : route192-168
Ether : 00:00:00:00:00:01
Arch : gateway
x2 :
Name : myserv
Ether : 00:00:00:00:00:02
Arch : x86
OS : linux

---------- x.yaml ------------

Here's the ruby script

---------- x.rb ------------
y = File.open(ARGV.shift).read
node = ARGV.shift
p YAML.parse(y).select("/Domains/*/Networks/*/Hosts/#{node}").transform
---------- x.rb ------------

Here's the output:

% ruby /tmp/x.rb /tmp/x.yaml 1
[]
% ruby /tmp/x.rb /tmp/x.yaml x2
[{"Name"=>"myserv", "Arch"=>"x86", "OS"=>"linux", "Ether"=>2}]

Is there any way to get the select to work without changing the 1
to "1" or using !str every time?

Jim
 
J

James F. Hranicky

Name : myserv
Ether : 00:00:00:00:00:02
Arch : x86
OS : linux
[del]

% ruby /tmp/x.rb /tmp/x.yaml x2
[{"Name"=>"myserv", "Arch"=>"x86", "OS"=>"linux", "Ether"=>2}]

Drat -- I just noticed the ethernet address was interpreted as
2 as well. I don't suppose there's an easy way to just tell YAML
to not do any conversions, is there?

Jim
 
T

trans. (T. Onoma)

10, James F. Hranicky wrote:
| > Name : myserv
| > Ether : 00:00:00:00:00:02
| > Arch : x86
| > OS : linux
|
| [del]
|
| > % ruby /tmp/x.rb /tmp/x.yaml x2
| > [{"Name"=>"myserv", "Arch"=>"x86", "OS"=>"linux", "Ether"=>2}]
|
| Drat -- I just noticed the ethernet address was interpreted as
| 2 as well. I don't suppose there's an easy way to just tell YAML
| to not do any conversions, is there?

Looky, looky! Someone wants option #1 ;)

Sorry, inside joke from yaml mailing list --you can put quotes around them or
(maybe) preface with !str.

Ether : '00:00:00:00:00:02'

or

Ether : !str 00:00:00:00:00:02

T.
 
J

James F. Hranicky

Sorry, inside joke from yaml mailing list --you can put quotes around them
or (maybe) preface with !str.

Sure -- that's what I was hoping to avoid.

Thanks,
Jim
 
W

why the lucky stiff

--------------050001030503000202060201
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
% ruby /tmp/x.rb /tmp/x.yaml 1
[]
% ruby /tmp/x.rb /tmp/x.yaml x2
[{"Name"=>"myserv", "Arch"=>"x86", "OS"=>"linux", "Ether"=>2}]

Is there any way to get the select to work without changing the 1
to "1" or using !str every time?
I'm attaching one technique for turning off typing. It looks like you
found two bugs:
* YPath is trying to match against typed keys. (All data should be
untransformed in YAML::Syck::Nodes)
* A bug exists in YAML::Syck::Node#transform. Use
YAML::Syck::Node#select! for now, which transforms the results of a
YPath query.

_why


--------------050001030503000202060201
Content-Type: text/plain;
name="x.rb"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="x.rb"

require 'yaml'
class YAML::Syck::Node
alias _transform transform
def transform
case @kind
when :scalar
@value
when :seq
@value.collect do |v|
v.transform
end
when :map
hsh = {}
@value.each do |k, v|
hsh[v[0].transform] = v[1].transform
end
hsh
end
end
def at( seg )
if Hash === @value
k, v = @value.values.detect { |x| x[0].transform == seg }
v
elsif Array === @value and seg =~ /\A\d+\Z/ and @value[seg.to_i]
@value[seg.to_i]
end
end
end

y = File.open(ARGV.shift).read
node = ARGV.shift
p YAML.parse(y).select!("/Domains/*/Networks/*/Hosts/1")
p YAML.parse(y).transform

--------------050001030503000202060201--
 
J

James F. Hranicky

I'm attaching one technique for turning off typing. It looks like you
found two bugs:
* YPath is trying to match against typed keys. (All data should be
untransformed in YAML::Syck::Nodes)
* A bug exists in YAML::Syck::Node#transform. Use
YAML::Syck::Node#select! for now, which transforms the results of a
YPath query.

Awesome! Thanks,
Jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,159
Messages
2,570,881
Members
47,418
Latest member
NoellaXku

Latest Threads

Top