Class: Cql::Protocol::TypeConverter

Inherits:
Object
  • Object
show all
Includes:
Decoding, Encoding
Defined in:
lib/cql/protocol/type_converter.rb

Instance Method Summary collapse

Methods included from Encoding

#write_bytes, #write_consistency, #write_decimal, #write_double, #write_float, #write_int, #write_long, #write_long_string, #write_short, #write_short_bytes, #write_string, #write_string_list, #write_string_map, #write_uuid, #write_varint

Methods included from Decoding

#read_byte!, #read_bytes!, #read_consistency!, #read_decimal!, #read_double!, #read_float!, #read_inet!, #read_int!, #read_long!, #read_long_string!, #read_option!, #read_short!, #read_short_bytes!, #read_string!, #read_string_list!, #read_string_map!, #read_string_multimap!, #read_uuid!, #read_varint!

Constructor Details

#initializeTypeConverter

Returns a new instance of TypeConverter.



13
14
15
16
# File 'lib/cql/protocol/type_converter.rb', line 13

def initialize
  @from_bytes_converters = from_bytes_converters
  @to_bytes_converters = to_bytes_converters
end

Instance Method Details

#from_bytes(buffer, type, size_bytes = 4) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cql/protocol/type_converter.rb', line 18

def from_bytes(buffer, type, size_bytes=4)
  return nil if buffer.empty?
  case type
  when Array
    return nil unless read_size(buffer, size_bytes)
    case type.first
    when :list
      bytes_to_list(buffer, @from_bytes_converters[type[1]])
    when :map
      bytes_to_map(buffer, @from_bytes_converters[type[1]], @from_bytes_converters[type[2]])
    when :set
      bytes_to_set(buffer, @from_bytes_converters[type[1]])
    end
  else
    @from_bytes_converters[type].call(buffer, size_bytes)
  end
end

#to_bytes(io, type, value, size_bytes = 4) ⇒ Object



36
37
38
39
40
41
42
43
44
45
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
# File 'lib/cql/protocol/type_converter.rb', line 36

def to_bytes(io, type, value, size_bytes=4)
  case type
  when Array
    unless value.nil? || value.is_a?(Enumerable)
      raise InvalidValueError, 'Value for collection must be enumerable'
    end
    case type.first
    when :list, :set
      _, sub_type = type
      if value
        raw = ''
        write_short(raw, value.size)
        value.each do |element|
          to_bytes(raw, sub_type, element, 2)
        end
        write_bytes(io, raw)
      else
        nil_to_bytes(io, size_bytes)
      end
    when :map
      _, key_type, value_type = type
      if value
        raw = ''
        write_short(raw, value.size)
        value.each do |key, value|
          to_bytes(raw, key_type, key, 2)
          to_bytes(raw, value_type, value, 2)
        end
        write_bytes(io, raw)
      else
        nil_to_bytes(io, size_bytes)
      end
    else
      raise UnsupportedColumnTypeError, %(Unsupported column collection type: #{type.first})
    end
  else
    converter = @to_bytes_converters[type]
    unless converter
      raise UnsupportedColumnTypeError, %(Unsupported column type: #{type})
    end
    converter.call(io, value, size_bytes)
  end
rescue TypeError => e
  raise TypeError, %("#{value}" cannot be encoded as #{type.to_s.upcase}: #{e.message}), e.backtrace
end