fix recursive arrays
This commit is contained in:
parent
99e0c0db18
commit
ad4aaaff8a
@ -2,7 +2,7 @@ Array.class_eval do
|
|||||||
def to_sof_node(writer , level)
|
def to_sof_node(writer , level)
|
||||||
node = Sof::ArrayNode.new()
|
node = Sof::ArrayNode.new()
|
||||||
each do |object|
|
each do |object|
|
||||||
node.add writer.to_sof_node( object )
|
node.add writer.to_sof_node( object , level + 1)
|
||||||
end
|
end
|
||||||
node
|
node
|
||||||
end
|
end
|
||||||
|
@ -25,8 +25,8 @@ Hash.class_eval do
|
|||||||
def to_sof_node(writer , level)
|
def to_sof_node(writer , level)
|
||||||
node = Sof::HashNode.new()
|
node = Sof::HashNode.new()
|
||||||
each do |key , object|
|
each do |key , object|
|
||||||
k = writer.to_sof_node( key )
|
k = writer.to_sof_node( key ,level + 1)
|
||||||
v = writer.to_sof_node( object )
|
v = writer.to_sof_node( object ,level +1)
|
||||||
node.add(k , v)
|
node.add(k , v)
|
||||||
end
|
end
|
||||||
node
|
node
|
||||||
|
@ -6,22 +6,25 @@ module Sof
|
|||||||
end
|
end
|
||||||
|
|
||||||
def write
|
def write
|
||||||
node = to_sof_node(@members.root)
|
node = to_sof_node(@members.root , 0)
|
||||||
io = StringIO.new
|
io = StringIO.new
|
||||||
node.out( io , 0 )
|
node.out( io , 0 )
|
||||||
io.string
|
io.string
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_sof_node(object)
|
def to_sof_node(object , level)
|
||||||
if is_value?(object)
|
if is_value?(object)
|
||||||
return SimpleNode.new(object.to_sof())
|
return SimpleNode.new(object.to_sof())
|
||||||
end
|
end
|
||||||
occurence = @members.objects[object]
|
occurence = @members.objects[object]
|
||||||
raise "no object #{object}" unless occurence
|
raise "no object #{object}" unless occurence
|
||||||
|
if(level > occurence.level )
|
||||||
|
return SimpleNode.new("*#{occurence.number}")
|
||||||
|
end
|
||||||
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
|
if(object.respond_to? :to_sof_node) #mainly meant for arrays and hashes
|
||||||
object.to_sof_node(self , occurence.level)
|
object.to_sof_node(self , level + 1)
|
||||||
else
|
else
|
||||||
object_sof_node(object , occurence.level)
|
object_sof_node(object , level + 1 )
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -75,4 +75,10 @@ class BasicSof < MiniTest::Test
|
|||||||
@out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true })
|
@out = Sof::Writer.write({ one: [1 , ObjectWithAttributes.new] , two: true })
|
||||||
check "-:one: -1\n -#{OBJECT_STRING}\n-:two: true"
|
check "-:one: -1\n -#{OBJECT_STRING}\n-:two: true"
|
||||||
end
|
end
|
||||||
|
def test_array_recursive
|
||||||
|
ar = [true, 1 ]
|
||||||
|
ar << ar
|
||||||
|
@out = Sof::Writer.write(ar)
|
||||||
|
check "-true\n-1\n-*1"
|
||||||
|
end
|
||||||
end
|
end
|
Loading…
x
Reference in New Issue
Block a user