class RbVmomi::Deserializer

Constants

BUILTIN_TYPE_ACTIONS
DEMANGLED_ARRAY_TYPES
NS_XSI

Public Class Methods

new(conn) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 41
def initialize conn
  @conn = conn
  @loader = conn.class.loader
end

Public Instance Methods

deserialize(node, type=nil) click to toggle source
# 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) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 142
def leaf_binary node
  node.content.unpack('m')[0]
end
leaf_date(node) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 138
def leaf_date node
  Time.parse node.content
end
leaf_keyvalue(node) click to toggle source

XXX does the value need to be deserialized?

# File lib/rbvmomi/deserialization.rb, line 147
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) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 132
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) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 100
def traverse_data node, klass
  obj = klass.new nil
  props = obj.props
  children = node.children.select(&:element?)
  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