Class: RbVmomi::NewDeserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rbvmomi/deserialization.rb

Constant Summary collapse

NS_XSI =
'http://www.w3.org/2001/XMLSchema-instance'
DEMANGLED_ARRAY_TYPES =
{
  'AnyType' => 'xsd:anyType',
  'DateTime' => 'xsd:dateTime',
}
BUILTIN_TYPE_ACTIONS =
{
  'xsd:string' => :string,
  'xsd:boolean' => :boolean,
  'xsd:byte' => :int,
  'xsd:short' => :int,
  'xsd:int' => :int,
  'xsd:long' => :int,
  'xsd:float' => :float,
  'xsd:dateTime' => :date,
  'PropertyPath' => :string,
  'MethodName' => :string,
  'TypeName' => :string,
  'xsd:base64Binary' => :binary,
  'KeyValue' => :keyvalue,
}

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ NewDeserializer



41
42
43
44
# File 'lib/rbvmomi/deserialization.rb', line 41

def initialize conn
  @conn = conn
  @loader = conn.class.loader
end

Instance Method Details

#deserialize(node, type = nil) ⇒ Object



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
# File 'lib/rbvmomi/deserialization.rb', line 46

def deserialize node, type=nil
  type_attr = node['type']

  # Work around for 1.5.x which doesn't populate node['type']
  # XXX what changed
  if node.attributes['type'] and not type_attr
    type_attr = node.attributes['type'].value
  end

  type = type_attr if type_attr

  if action = BUILTIN_TYPE_ACTIONS[type]
    case action
    when :string
      node.content
    when :boolean
      node.content == '1' || node.content == 'true'
    when :int
      node.content.to_i
    when :float
      node.content.to_f
    when :date
      leaf_date node
    when :binary
      leaf_binary node
    when :keyvalue
      leaf_keyvalue node
    else fail
    end
  else
    if type =~ /:/
      type = type.split(":", 2)[1]
    end
    if type =~ /^ArrayOf/
      type = DEMANGLED_ARRAY_TYPES[$'] || $'
      return node.children.select(&:element?).map { |c| deserialize c, type }
    end
    if type =~ /:/
      type = type.split(":", 2)[1]
    end

    klass = @loader.get(type) or fail "no such type '#{type}'"
    case klass.kind
    when :data
      traverse_data node, klass
    when :enum
      node.content
    when :managed
      leaf_managed node, klass
    else fail
    end
  end
end

#leaf_binary(node) ⇒ Object



143
144
145
# File 'lib/rbvmomi/deserialization.rb', line 143

def leaf_binary node
  node.content.unpack('m')[0]
end

#leaf_date(node) ⇒ Object



139
140
141
# File 'lib/rbvmomi/deserialization.rb', line 139

def leaf_date node
  Time.parse node.content
end

#leaf_keyvalue(node) ⇒ Object

XXX does the value need to be deserialized?



148
149
150
151
152
153
154
155
# File 'lib/rbvmomi/deserialization.rb', line 148

def leaf_keyvalue node
  h = {}
  node.children.each do |child|
    next unless child.element?
    h[child.name] = child.content
  end
  [h['key'], h['value']]
end

#leaf_managed(node, klass) ⇒ Object



133
134
135
136
137
# File 'lib/rbvmomi/deserialization.rb', line 133

def leaf_managed node, klass
  type_attr = node['type']
  klass = @loader.get(type_attr) if type_attr
  klass.new(@conn, node.content)
end

#traverse_data(node, klass) ⇒ Object



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
# File 'lib/rbvmomi/deserialization.rb', line 100

def traverse_data node, klass
  obj = klass.new nil
  props = obj.props
  children = node.children.select(&:element?)
  n = children.size
  i = 0

  klass.full_props_desc.each do |desc|
    name = desc['name']
    child_type = desc['wsdl_type']

    # Ignore unknown fields
    while child = children[i] and not klass.full_props_set.member? child.name
      i += 1
    end

    if desc['is-array']
      a = []
      while ((child = children[i]) && (child.name == name))
        child = children[i]
        a << deserialize(child, child_type)
        i += 1
      end
      props[name.to_sym] = a
    elsif ((child = children[i]) && (child.name == name))
      props[name.to_sym] = deserialize(child, child_type)
      i += 1
    end
  end

  obj
end