Class: KRPC::Types::TypeStore

Inherits:
Object
  • Object
show all
Defined in:
lib/krpc/types.rb

Class Method Summary collapse

Class Method Details

.[](type_string) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/krpc/types.rb', line 29

def [](type_string)
  return @cache[type_string] if @cache.include? type_string
  
  type =
    if PROTOBUF_VALUE_TYPES.include? type_string then ValueType.new(type_string)
    elsif type_string.start_with? "Class(" || type_string == "Class" then ClassType.new(type_string)
    elsif type_string.start_with? "Enum("  || type_string == "Enum"  then EnumType.new(type_string)
    elsif type_string.start_with? "List("  || type_string == "List"  then ListType.new(type_string)
    elsif type_string.start_with? "Dictionary(" || type_string == "Dictionary" then DictionaryType.new(type_string)
    elsif type_string.start_with? "Set("   || type_string == "Set"   then SetType.new(type_string)
    elsif type_string.start_with? "Tuple(" || type_string == "Tuple" then TupleType.new(type_string)
    else # A message type (eg. type_string = "KRPC.List" or "KRPC.Services")
      raise(ValueError, "\"#{type_string}\" is not a valid type string") unless /^[A-Za-z0-9_\.]+$/ =~ type_string
      if PROTOBUF_TO_MESSAGE_TYPE.has_key? type_string
        MessageType.new(type_string)
      else
        raise(ValueError, "\"#{type_string}\" is not a valid type string")
      end
    end

  @cache[type_string] = type
  type      
end

.coerce_to(value, type) ⇒ Object

Raises:



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
# File 'lib/krpc/types.rb', line 75

def coerce_to(value, type)
  return value if type.is_a?(EnumType) && value.class == Symbol # Enum handling
  return value if value.is_a?(type.ruby_type)
  # A NilClass can be coerced to a ClassType
  return nil if type.is_a?(ClassType) && value.nil?
  # Handle service' class instance
  if type.is_a?(ClassType) && value.is_a?(Gen::ClassBase) && 
     type.ruby_type == value.class
    return value
  end
  # -- Collection types --
  begin
    # coerce "list" to array
    if type.is_a?(ListType) && value.respond_to?(:map) && value.respond_to?(:to_a)
      return type.ruby_type.new(value.map{|x| coerce_to(x, type.value_type) }.to_a)
    end
    # coerce "tuple" to array + check elements count
    if type.is_a?(TupleType) && value.respond_to?(:map) && value.respond_to?(:to_a) && value.respond_to?(:size)
      raise ValueError if value.size != type.value_types.size
      return type.ruby_type.new(value.map.with_index{|x,i| coerce_to(x, type.value_types[i]) }.to_a)
    end
  rescue ValueError
    raise(ValueError, "Failed to coerce value #{value.to_s} of type #{value.class} to type #{type}")
  end
  # Numeric types
  if type.ruby_type == Float && ( value.kind_of?(Float) || value.to_s.numeric? )
    return value.to_f
  elsif type.ruby_type == Integer && ( value.kind_of?(Integer) || value.to_s.integer? )
    return value.to_i
  end
  # Convert value type to string
  if type.is_a?(ValueType) && type.ruby_type == String
    return value.to_s
  end
  raise(ValueError, "Failed to coerce value #{value.to_s} of type #{value.class} to type #{type}")
end

.get_parameter_type(pos, type, attrs) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/krpc/types.rb', line 53

def get_parameter_type(pos, type, attrs)
  type_attrs = Attributes.get_parameter_type_attrs(pos, attrs)
  type_attrs.each do |ta|
    begin
      return self[ta]
    rescue ValueError
    end
  end
  self[type]
end

.get_return_type(type, attrs) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/krpc/types.rb', line 64

def get_return_type(type, attrs)
  type_attrs = Attributes.get_return_type_attrs(attrs)
  type_attrs.each do |ta|
    begin
      return self[ta]
    rescue ValueError
    end
  end
  self[type]
end