34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/ruby_vim_sdk/soap/serializer.rb', line 34
def serialize(value, info, default_namespace = @default_namespace)
return unless VmomiSupport.is_child_version(@version, info.version)
if value.nil?
return if info.optional?
raise "Field #{info.name} is not optional"
elsif value.kind_of?(Array) && value.empty?
if info.type == Object
raise "Cannot assign empty 'untyped' array to an Any" unless value.class.ancestors.include?(TypedArray)
elsif info.optional?
return
else
raise "Field #{info.name} is not optional"
end
end
if @outermost_attributes
attribute = @outermost_attributes
@outermost_attributes = nil
else
attribute = ""
end
current_default_namespace = default_namespace
current_tag_namespace = VmomiSupport.wsdl_namespace(info.version)
if current_tag_namespace != default_namespace
attribute += " xmlns=\"#{current_tag_namespace}\""
current_default_namespace = current_tag_namespace
end
if value.kind_of?(Vmodl::ManagedObject)
if info.type == Object
namespace_attribute, qualified_name = qualified_name(Vmodl::ManagedObject, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
end
_, name = VmomiSupport.qualified_wsdl_name(value.class)
attribute += " type=\"#{name}\""
@writer << "<#{info.wsdl_name}#{attribute}>#{value.__mo_id__.to_xs}</#{info.wsdl_name}>"
elsif value.kind_of?(Vmodl::DataObject)
if value.kind_of?(Vmodl::MethodFault)
localized_method_fault = Vmodl::LocalizedMethodFault.new
localized_method_fault.fault = value
localized_method_fault.localized_message = value.msg
unless info.type == Object
info = info.dup
info.type = Vmodl::LocalizedMethodFault
end
serialize_data_object(localized_method_fault, info, attribute, current_default_namespace)
else
serialize_data_object(value, info, attribute, current_default_namespace)
end
elsif value.kind_of?(Array)
if info.type == Object
if value.class.respond_to?(:item)
item_type = value.class.item
else
item_type = value.first.class
end
if DYNAMIC_TYPES.include?(item_type)
tag = "string"
type = String::TypedArray
elsif item_type.ancestors.include?(Vmodl::ManagedObject)
tag = "ManagedObjectReference"
type = Vmodl::ManagedObject::TypedArray
else
tag = VmomiSupport.wsdl_name(item_type)
type = item_type::TypedArray
end
namespace_attribute, qualified_name = qualified_name(type, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
@writer << "<#{info.wsdl_name}#{attribute}>"
item_info = info.dup
item_info.wsdl_name = tag
item_info.type = value.class
value.each do |child|
serialize(child, item_info, current_default_namespace)
end
@writer << "</#{info.wsdl_name}>"
else
item_info = info.dup
item_info.type = info.type.item
value.each do |v|
serialize(v, item_info, default_namespace)
end
end
elsif value.kind_of?(Class)
if info.type == Object
attribute += " #{@xsi_prefix}type=\"#{@xsd_prefix}string\""
end
@writer << "<#{info.wsdl_name}#{attribute}>#{VmomiSupport.wsdl_name(value)}</#{info.wsdl_name}>"
elsif value.kind_of?(Vmodl::MethodName)
if info.type == Object
attribute += " #{@xsi_prefix}type=\"#{@xsd_prefix}string\""
end
@writer << "<#{info.wsdl_name}#{attribute}>#{value}</#{info.wsdl_name}>"
elsif value.kind_of?(Time)
if info.type == Object
namespace_attribute, qualified_name = qualified_name(value.class, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
end
@writer << "<#{info.wsdl_name}#{attribute}>#{value.utc.iso8601}</#{info.wsdl_name}>"
elsif value.kind_of?(SoapBinary) || info.type == SoapBinary
if info.type == Object
namespace_attribute, qualified_name = qualified_name(value.class, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
end
@writer << "<#{info.wsdl_name}#{attribute}>#{Base64.encode64(value)}</#{info.wsdl_name}>"
elsif value.kind_of?(TrueClass) || value.kind_of?(FalseClass) || value.kind_of?(SoapBoolean)
if info.type == Object
namespace_attribute, qualified_name = qualified_name(value.class, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
end
@writer << "<#{info.wsdl_name}#{attribute}>#{value ? "true" : "false"}</#{info.wsdl_name}>"
else
if info.type == Object
if value.kind_of?(Vmodl::PropertyPath)
attribute += " #{@xsi_prefix}type=\"#{@xsd_prefix}string\""
else
namespace_attribute, qualified_name = qualified_name(value.class, current_default_namespace)
attribute += "#{namespace_attribute} #{@xsi_prefix}type=\"#{qualified_name}\""
end
end
value = value.to_s unless value.kind_of?(String)
@writer << "<#{info.wsdl_name}#{attribute}>#{value.to_xs}</#{info.wsdl_name}>"
end
end
|