[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

YAML: Integers in YPATH

James F. Hranicky

10/12/2004 5:10:00 PM

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


5 Answers

James F. Hranicky

10/12/2004 5:16:00 PM

0

On Tuesday 12 October 2004 13: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?

Jim


T. Onoma

10/12/2004 5:27:00 PM

0

On Tuesday 12 October 2004 01:16 pm, James F. Hranicky wrote:
| On Tuesday 12 October 2004 13: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.


James F. Hranicky

10/12/2004 5:37:00 PM

0


> 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


why the lucky stiff

10/12/2004 6:03:00 PM

0

James F. Hranicky wrote:

> % 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

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

James F. Hranicky

10/12/2004 7:03:00 PM

0

On Tuesday 12 October 2004 14:03, why the lucky stiff wrote:

> 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