<commit_before>require 'trivial_soap'
require 'time'
require 'linguistics'
Linguistics.use :en

module RbVmomi

Typed = Struct.new(:type, :value)

class NiceHash < Hash
  def initialize
    super do |h,k|
      if k2 = h.keys.find { |x| x.to_s.en.plural.to_sym == k }
        case v = h[k2]
        when Array then v
        when nil then []
        else [v]
        end
      else
        nil
      end
    end
  end

  def method_missing sym, *args
    if sym.to_s =~ /=$/
      super unless args.size == 1
      self[$`.to_sym] = args.first
    else
      super unless args.empty?
      self[sym]
    end
  end

  def self.[] *a
    new.tap { |h| h.merge! super }
  end
end

def self.type name
  return unless name
  name = name.to_s
  return [type($')] if name =~ /^ArrayOf/

  if name =~ /^xsd:/
    XSD.type $'
  elsif %w(String).member? name
    XSD.type name
  else
    VIM.type name
  end
end

module XSD
  def self.type name
    case name
    when 'anyType'
      nil
    when 'boolean'
      nil
    when "string", "String"
      String
    when "int", "long", "short", "byte"
      Integer
    else fail "no such xsd type #{name.inspect}"
    end
  end

  def self.method_missing sym, arg
    RbVmomi::Typed.new "xsd:#{sym}", arg
  end
end

class Soap < TrivialSoap
  NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance'

  def serviceInstance
    VIM::ServiceInstance self, 'ServiceInstance'
  end

  def propertyCollector
    @propertyCollector ||= serviceInstance.RetrieveServiceContent!.propertyCollector
  end

  def call method, desc, o
    fail unless o.is_a? Hash
    fail unless desc.is_a? Hash
    resp = request 'urn:vim25/4.0' do |xml|
      xml.tag! method, :xmlns => 'urn:vim25' do
        yield xml if block_given?
        obj2xml xml, '_this', 'ManagedObject', o['_this']
        desc['params'].each do |d|
          k = d['name'].to_sym
          next unless o.member? k
          obj2xml xml, d['name'], d['wsdl_type'], o[k]
        end
      end
    end
    if resp.at('faultcode')
      fail "#{resp.at('faultcode').text}: #{resp.at('faultstring').text}"
    else
      if desc['result']['wsdl_type']
        xml2obj resp.children.first, desc['result']['wsdl_type']
      else
        nil
      end
    end
  end

  def xml2obj xml, type<commit_msg>handle array results<commit_after>require 'trivial_soap'
require 'time'
require 'linguistics'
Linguistics.use :en

module RbVmomi

Typed = Struct.new(:type, :value)

class NiceHash < Hash
  def initialize
    super do |h,k|
      if k2 = h.keys.find { |x| x.to_s.en.plural.to_sym == k }
        case v = h[k2]
        when Array then v
        when nil then []
        else [v]
        end
      else
        nil
      end
    end
  end

  def method_missing sym, *args
    if sym.to_s =~ /=$/
      super unless args.size == 1
      self[$`.to_sym] = args.first
    else
      super unless args.empty?
      self[sym]
    end
  end

  def self.[] *a
    new.tap { |h| h.merge! super }
  end
end

def self.type name
  return unless name
  name = name.to_s
  return [type($')] if name =~ /^ArrayOf/

  if name =~ /^xsd:/
    XSD.type $'
  elsif %w(String).member? name
    XSD.type name
  else
    VIM.type name
  end
end

module XSD
  def self.type name
    case name
    when 'anyType'
      nil
    when 'boolean'
      nil
    when "string", "String"
      String
    when "int", "long", "short", "byte"
      Integer
    else fail "no such xsd type #{name.inspect}"
    end
  end

  def self.method_missing sym, arg
    RbVmomi::Typed.new "xsd:#{sym}", arg
  end
end

class Soap < TrivialSoap
  NS_XSI = 'http://www.w3.org/2001/XMLSchema-instance'

  def serviceInstance
    VIM::ServiceInstance self, 'ServiceInstance'
  end

  def propertyCollector
    @propertyCollector ||= serviceInstance.RetrieveServiceContent!.propertyCollector
  end

  def call method, desc, o
    fail unless o.is_a? Hash
    fail unless desc.is_a? Hash
    resp = request 'urn:vim25/4.0' do |xml|
      xml.tag! method, :xmlns => 'urn:vim25' do
        yield xml if block_given?
        obj2xml xml, '_this', 'ManagedObject', o['_this']
        desc['params'].each do |d|
          k = d['name'].to_sym
          next unless o.member? k
          obj2xml xml, d['name'], d['wsdl_type'], o[k]
        end
      end
    end
    if resp.at('faultcode')
      fail "#{resp.at('faultcode').text}: #{resp.at('faultstring').text}"
    else
      if rtype = desc['result']['wsdl_type']
        if rtype =~ /^ArrayOf/
          xml2obj resp, rtype
        else
          xml2obj resp.children.first, rtype
        end
      else
        nil
      end
    end
  end

  def xml2obj xml, type
{'fraction_non_alphanumeric': 0.11953478354512169, 'fraction_numerical': 0.00796898556967478, 'mean_word_length': 2.1939477303988997, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 23}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3460691', 'n_tokens_mistral': 1673, 'n_tokens_neox': 1617, 'n_words': 462}