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 40
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 45
def deserialize node, type=nil
  type_attr = node['type']

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

  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 raise
    end
  else
    type = type.split(':', 2)[1] if type =~ /:/
    if type =~ /^ArrayOf/
      type = DEMANGLED_ARRAY_TYPES[$'] || $'
      return node.children.select(&:element?).map { |c| deserialize c, type }
    end
    type = type.split(':', 2)[1] if type =~ /:/

    klass = @loader.get(type) or raise "no such type '#{type}'"
    case klass.kind
    when :data
      traverse_data node, klass
    when :enum
      node.content
    when :managed
      leaf_managed node, klass
    when :opaque
      leaf_opaque node, klass
    else raise
    end
  end
end
leaf_binary(node) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 141
def leaf_binary node
  node.content.unpack('m')[0]
end
leaf_date(node) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 137
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 146
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 127
def leaf_managed node, klass
  type_attr = node['type']
  klass = @loader.get(type_attr) if type_attr
  klass.new(@conn, node.content)
end
leaf_opaque(node, klass) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 133
def leaf_opaque node, klass
  klass.new(@conn, node.content)
end
traverse_data(node, klass) click to toggle source
# File lib/rbvmomi/deserialization.rb, line 95
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