Skip to content

Commit

Permalink
support passing a hash for a key value array
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich Lane committed Feb 10, 2012
1 parent ee5a142 commit a1243a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
14 changes: 9 additions & 5 deletions lib/rbvmomi/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def call method, desc, this, params
# hic sunt dracones
def obj2xml xml, name, type, is_array, o, attrs={}
expected = type(type)
fail "expected array, got #{o.class.wsdl_name}" if is_array and not o.is_a? Array
fail "expected array, got #{o.class.wsdl_name}" if is_array and not (o.is_a? Array or (o.is_a? Hash and expected == BasicTypes::KeyValue))
case o
when Array, BasicTypes::KeyValue
if o.is_a? BasicTypes::KeyValue and expected != BasicTypes::KeyValue
fail "expected #{expected.wsdl_name}, got KeyValue"
elsif expected == BasicTypes::KeyValue and not is_array
xml.tag! name, attrs do
xml.tag! 'key', o[0]
xml.tag! 'value', o[1]
xml.tag! 'key', o[0].to_s
xml.tag! 'value', o[1].to_s
end
else
fail "expected #{expected.wsdl_name}, got array" unless is_array
Expand All @@ -148,8 +148,12 @@ def obj2xml xml, name, type, is_array, o, attrs={}
when BasicTypes::Enum
xml.tag! name, o.value.to_s, attrs
when Hash
fail "expected #{expected.wsdl_name}, got a hash" unless expected <= BasicTypes::DataObject
obj2xml xml, name, type, false, expected.new(o), attrs
if expected == BasicTypes::KeyValue and is_array
obj2xml xml, name, type, is_array, o.to_a, attrs
else
fail "expected #{expected.wsdl_name}, got a hash" unless expected <= BasicTypes::DataObject
obj2xml xml, name, type, false, expected.new(o), attrs
end
when true, false
fail "expected #{expected.wsdl_name}, got a boolean" unless [BasicTypes::Boolean, BasicTypes::AnyType].member? expected
attrs['xsi:type'] = 'xsd:boolean' if expected == BasicTypes::AnyType
Expand Down
12 changes: 12 additions & 0 deletions test/test_serialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,18 @@ def test_keyvalue
<key>a</key>
<value>b</value>
</root>
<root>
<key>c</key>
<value>d</value>
</root>
EOS

obj = { 'a' => 'b', :c => 'd' }
check <<-EOS, obj, 'KeyValue', true
<root>
<key>a</key>
<value>b</value>
</root>
<root>
<key>c</key>
<value>d</value>
Expand Down

0 comments on commit a1243a6

Please sign in to comment.