Module: AMQP::Client::Table Private

Defined in:
lib/amqp/client/table.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Encode and decode an AMQP table to/from hash, only used internally

Class Method Summary collapse

Class Method Details

.decode(bytes) ⇒ Hash<String, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decodes an AMQP table into a hash

Returns:

  • (Hash<String, Object>)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/amqp/client/table.rb', line 27

def self.decode(bytes)
  hash = {}
  pos = 0
  while pos < bytes.bytesize
    key_len = bytes.getbyte(pos)
    pos += 1
    key = bytes.byteslice(pos, key_len).force_encoding("utf-8")
    pos += key_len
    len, value = decode_field(bytes, pos)
    pos += len + 1
    hash[key] = value
  end
  hash
end

.decode_field(bytes, pos) ⇒ Array<Integer, Object>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decodes a single value

Returns:

  • (Array<Integer, Object>)

    Bytes read and the parsed value



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/amqp/client/table.rb', line 92

def self.decode_field(bytes, pos)
  type = bytes[pos]
  pos += 1
  case type
  when "S"
    len = bytes.byteslice(pos, 4).unpack1("L>")
    pos += 4
    [4 + len, bytes.byteslice(pos, len).force_encoding("utf-8")]
  when "F"
    len = bytes.byteslice(pos, 4).unpack1("L>")
    pos += 4
    [4 + len, decode(bytes.byteslice(pos, len))]
  when "A"
    len = bytes.byteslice(pos, 4).unpack1("L>")
    pos += 4
    array_end = pos + len
    a = []
    while pos < array_end
      length, value = decode_field(bytes, pos)
      pos += length + 1
      a << value
    end
    [4 + len, a]
  when "t"
    [1, bytes.getbyte(pos) == 1]
  when "b"
    [1, bytes.byteslice(pos, 1).unpack1("c")]
  when "B"
    [1, bytes.byteslice(pos, 1).unpack1("C")]
  when "s"
    [2, bytes.byteslice(pos, 2).unpack1("s")]
  when "u"
    [2, bytes.byteslice(pos, 2).unpack1("S")]
  when "I"
    [4, bytes.byteslice(pos, 4).unpack1("l>")]
  when "i"
    [4, bytes.byteslice(pos, 4).unpack1("L>")]
  when "l"
    [8, bytes.byteslice(pos, 8).unpack1("q>")]
  when "f"
    [4, bytes.byteslice(pos, 4).unpack1("g")]
  when "d"
    [8, bytes.byteslice(pos, 8).unpack1("G")]
  when "D"
    scale = bytes.getbyte(pos)
    pos += 1
    value = bytes.byteslice(pos, 4).unpack1("L>")
    d = value / (10**scale)
    [5, d]
  when "x"
    len = bytes.byteslice(pos, 4).unpack1("L>")
    [4 + len, bytes.byteslice(pos, len)]
  when "T"
    [8, Time.at(bytes.byteslice(pos, 8).unpack1("Q>"))]
  when "V"
    [0, nil]
  else raise ArgumentError, "unsupported table field type: #{type}"
  end
end

.encode(hash) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encodes a hash into a byte array

Parameters:

  • hash (Hash)

Returns:

  • (String)

    Byte array



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/amqp/client/table.rb', line 11

def self.encode(hash)
  return "" if hash.nil? || hash.empty?

  arr = []
  fmt = String.new
  hash.each do |k, value|
    key = k.to_s
    arr.push(key.bytesize, key)
    fmt << "Ca*"
    encode_field(value, arr, fmt)
  end
  arr.pack(fmt)
end

.encode_field(value, arr, fmt) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Encoding a single value in a table

Returns:

  • (nil)


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
81
82
83
84
85
86
87
# File 'lib/amqp/client/table.rb', line 45

def self.encode_field(value, arr, fmt)
  case value
  when Integer
    if value > 2**31
      arr.push("l", value)
      fmt << "aq>"
    else
      arr.push("I", value)
      fmt << "al>"
    end
  when Float
    arr.push("d", value)
    fmt << "aG"
  when String
    arr.push("S", value.bytesize, value)
    fmt << "aL>a*"
  when Time
    arr.push("T", value.to_i)
    fmt << "aQ>"
  when Array
    value_arr = []
    value_fmt = String.new
    value.each { |e| encode_field(e, value_arr, value_fmt) }
    bytes = value_arr.pack(value_fmt)
    arr.push("A", bytes.bytesize, bytes)
    fmt << "aL>a*"
  when Hash
    bytes = Table.encode(value)
    arr.push("F", bytes.bytesize, bytes)
    fmt << "aL>a*"
  when true
    arr.push("t", 1)
    fmt << "aC"
  when false
    arr.push("t", 0)
    fmt << "aC"
  when nil
    arr << "V"
    fmt << "a"
  else raise ArgumentError, "unsupported table field type: #{value.class}"
  end
  nil
end