Class: Qpid::Proton::Codec::Data
- Inherits:
-
Object
- Object
- Qpid::Proton::Codec::Data
- Includes:
- Util::Wrapper
- Defined in:
- lib/codec/data.rb
Overview
Raises TypeError for invalid conversions
Constant Summary collapse
- PROTON_METHOD_PREFIX =
"pn_data"
Instance Attribute Summary
Attributes included from Util::Wrapper
Class Method Summary collapse
- .finalize!(impl, free) ⇒ Object
-
.from_object(impl, x) ⇒ Object
Clear a pn_data_t* and convert a ruby object into it.
-
.to_object(impl) ⇒ Object?
Convert a pn_data_t* containing a single value to a ruby object.
Instance Method Summary collapse
-
#<<(x) ⇒ Object
Add an arbitrary data value using object=, return self.
- #array ⇒ Object
- #array=(a) ⇒ Object
-
#binary ⇒ String
If the current node is binary, returns its value.
-
#binary=(value) ⇒ Object
Puts a binary value.
-
#bool ⇒ Boolean
If the current node is a boolean, then it returns the value.
-
#bool=(value) ⇒ Object
Puts a boolean value.
-
#byte ⇒ Integer
If the current node is an byte, returns its value.
-
#byte=(value) ⇒ Object
Puts a byte value.
-
#char ⇒ Integer
If the current node is a character, returns its value.
-
#char=(value) ⇒ Object
Puts a character value.
- #code ⇒ Object
-
#decimal128 ⇒ Integer
If the current node is a decimal128, returns its value.
-
#decimal128=(value) ⇒ Object
Puts a decimal128 value.
-
#decimal32 ⇒ Integer
If the current node is a decimal32, returns its value.
-
#decimal32=(value) ⇒ Object
Puts a decimal32 value.
-
#decimal64 ⇒ Integer
If the current node is a decimal64, returns its value.
-
#decimal64=(value) ⇒ Object
Puts a decimal64 value.
-
#decode(encoded) ⇒ Object
Decodes the first value from supplied AMQP data and returns the number of bytes consumed.
- #described ⇒ Object
- #described=(d) ⇒ Object
-
#double ⇒ Float
If the current node is a double, returns its value.
-
#double=(value) ⇒ Object
Puts a double value.
-
#encode ⇒ Object
Returns a representation of the data encoded in AMQP format.
- #enter_exit ⇒ Object
- #expect(code) ⇒ Object
- #fill(a, count, what) ⇒ Object
-
#float ⇒ Float
If the current node is a float, returns its value.
-
#float=(value) ⇒ Object
Puts a float value.
-
#get ⇒ Object
Get the current value as a single object.
- #get_array ⇒ Object
-
#initialize(capacity = 16) ⇒ Data
constructor
A new instance of Data.
-
#int ⇒ Integer
If the current node is an integer, returns its value.
-
#int=(value) ⇒ Object
Puts an integer value.
- #list ⇒ Object
- #list=(a) ⇒ Object
-
#long ⇒ Integer
If the current node is a long, returns its value.
-
#long=(value) ⇒ Object
Puts a long value.
- #map ⇒ Object
- #map=(m) ⇒ Object
-
#next_object ⇒ Object
Move forward to the next value and return it.
-
#null ⇒ Object
Return nil if vallue is null, raise exception otherwise.
-
#null=(dummy = nil) ⇒ Object
Set the current value to null.
-
#null? ⇒ Boolean
Checks if the current node is null.
-
#object ⇒ Object
Gets the current node, based on how it was encoded.
-
#object=(object) ⇒ Object
Puts an arbitrary object type.
-
#put(value, type_code) ⇒ Object
Puts a new value with the given type into the current node.
-
#short ⇒ Integer
If the current node is a short, returns its value.
-
#short=(value) ⇒ Object
Puts a short value.
-
#string ⇒ String
If the current node is a string, returns its value.
-
#string=(value) ⇒ Object
Puts a UTF-8 encoded string value.
-
#symbol ⇒ Symbol
If the current node is a symbol, returns its value.
-
#symbol=(value) ⇒ Object
Puts a symbolic value.
-
#timestamp ⇒ Integer
If the current node is a timestamp, returns its value.
-
#timestamp=(value) ⇒ Object
Puts a timestamp value.
- #type ⇒ Object
-
#ubyte ⇒ Integer
If the current node is an unsigned byte, returns its value.
-
#ubyte=(value) ⇒ Object
Puts an unsigned byte value.
-
#uint ⇒ Integer
If the current node is an unsigned int, returns its value.
-
#uint=(value) ⇒ Object
Puts an unsigned integer value.
-
#ulong ⇒ Integer
If the current node is an unsigned long, returns its value.
-
#ulong=(value) ⇒ Object
Puts an unsigned long value.
-
#ushort ⇒ Integer
If the current node is an unsigned short, returns its value.
-
#ushort=(value) ⇒ Object
Puts an unsigned short value.
-
#uuid ⇒ String
If the current value is a
UUID
, returns its value. -
#uuid=(value) ⇒ Object
Puts a
UUID
value.
Methods included from Util::Wrapper
included, #inspect, registry, #to_s
Constructor Details
#initialize(capacity) ⇒ Data #instance(impl) ⇒ Data
Returns a new instance of Data.
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/codec/data.rb', line 57 def initialize(capacity = 16) if capacity.is_a?(Integer) @impl = Cproton.pn_data(capacity.to_i) @free = true else # Assume non-integer capacity is a SWIG::pn_data_t* @impl = capacity @free = false end # destructor ObjectSpace.define_finalizer(self, self.class.finalize!(@impl, @free)) end |
Class Method Details
.finalize!(impl, free) ⇒ Object
72 73 74 75 76 |
# File 'lib/codec/data.rb', line 72 def self.finalize!(impl, free) proc { Cproton.pn_data_free(impl) if free } end |
.from_object(impl, x) ⇒ Object
Clear a pn_data_t* and convert a ruby object into it. If x==nil leave it empty.
46 47 48 49 50 51 |
# File 'lib/codec/data.rb', line 46 def self.from_object(impl, x) d = Data.new(impl) d.clear d.object = x if x nil end |
.to_object(impl) ⇒ Object?
Convert a pn_data_t* containing a single value to a ruby object.
36 37 38 39 40 41 42 |
# File 'lib/codec/data.rb', line 36 def self.to_object(impl) if (Cproton.pn_data_size(impl) > 0) d = Data.new(impl) d.rewind d.next_object end end |
Instance Method Details
#<<(x) ⇒ Object
Add an arbitrary data value using object=, return self
219 |
# File 'lib/codec/data.rb', line 219 def <<(x) self.object=x; self; end |
#array ⇒ Object
156 157 158 159 160 161 162 163 164 165 |
# File 'lib/codec/data.rb', line 156 def array return list if code == Cproton::PN_LIST expect Cproton::PN_ARRAY count, d, t = get_array enter_exit do desc = next_object if d a = Types::UniformArray.new(t, nil, desc) fill(a, count, "array") end end |
#array=(a) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/codec/data.rb', line 167 def array=(a) t = a.type if a.respond_to? :type d = a.descriptor if a.respond_to? :descriptor if (h = a.instance_variable_get(:@proton_array_header)) t ||= h.type d ||= h.descriptor end raise TypeError, "no type when converting #{a.class} to an array" unless t put_array(!d.nil?, t.code) m = Mapping[t] enter_exit do self << d unless d.nil? a.each { |e| m.put(self, e); } end end |
#binary ⇒ String
If the current node is binary, returns its value. Otherwise, it returns an empty string (“”).
597 598 599 |
# File 'lib/codec/data.rb', line 597 def binary Qpid::Proton::Types::BinaryString.new(Cproton.pn_data_get_binary(@impl)) end |
#binary=(value) ⇒ Object
Puts a binary value.
A binary string is encoded as an ASCII 8-bit string value. This is in contranst to other strings, which are treated as UTF-8 encoded.
586 587 588 |
# File 'lib/codec/data.rb', line 586 def binary=(value) check(Cproton.pn_data_put_binary(@impl, value)) end |
#bool ⇒ Boolean
If the current node is a boolean, then it returns the value. Otherwise, it returns false.
256 257 258 |
# File 'lib/codec/data.rb', line 256 def bool Cproton.pn_data_get_bool(@impl) end |
#bool=(value) ⇒ Object
Puts a boolean value.
247 248 249 |
# File 'lib/codec/data.rb', line 247 def bool=(value) check(Cproton.pn_data_put_bool(@impl, value)) end |
#byte ⇒ Integer
If the current node is an byte, returns its value. Otherwise, it returns 0.
290 291 292 |
# File 'lib/codec/data.rb', line 290 def byte Cproton.pn_data_get_byte(@impl) end |
#byte=(value) ⇒ Object
Puts a byte value.
281 282 283 |
# File 'lib/codec/data.rb', line 281 def byte=(value) check(Cproton.pn_data_put_byte(@impl, value)) end |
#char ⇒ Integer
If the current node is a character, returns its value. Otherwise, returns 0.
378 379 380 |
# File 'lib/codec/data.rb', line 378 def char Cproton.pn_data_get_char(@impl) end |
#char=(value) ⇒ Object
Puts a character value.
369 370 371 |
# File 'lib/codec/data.rb', line 369 def char=(value) check(Cproton.pn_data_put_char(@impl, value)) end |
#decimal128 ⇒ Integer
If the current node is a decimal128, returns its value. Otherwise, returns 0.
519 520 521 522 523 |
# File 'lib/codec/data.rb', line 519 def decimal128 value = "" Cproton.pn_data_get_decimal128(@impl).each{|val| value += ("%02x" % val)} value.to_i(16) end |
#decimal128=(value) ⇒ Object
Puts a decimal128 value.
506 507 508 509 510 511 512 |
# File 'lib/codec/data.rb', line 506 def decimal128=(value) raise TypeError, "invalid decimal128 value: #{value}" if value.nil? value = value.to_s(16).rjust(32, "0") bytes = [] value.scan(/(..)/) {|v| bytes << v[0].to_i(16)} check(Cproton.pn_data_put_decimal128(@impl, bytes)) end |
#decimal32 ⇒ Integer
If the current node is a decimal32, returns its value. Otherwise, returns 0.
481 482 483 |
# File 'lib/codec/data.rb', line 481 def decimal32 Cproton.pn_data_get_decimal32(@impl) end |
#decimal32=(value) ⇒ Object
Puts a decimal32 value.
472 473 474 |
# File 'lib/codec/data.rb', line 472 def decimal32=(value) check(Cproton.pn_data_put_decimal32(@impl, value)) end |
#decimal64 ⇒ Integer
If the current node is a decimal64, returns its value. Otherwise, it returns 0.
498 499 500 |
# File 'lib/codec/data.rb', line 498 def decimal64 Cproton.pn_data_get_decimal64(@impl) end |
#decimal64=(value) ⇒ Object
Puts a decimal64 value.
489 490 491 |
# File 'lib/codec/data.rb', line 489 def decimal64=(value) check(Cproton.pn_data_put_decimal64(@impl, value)) end |
#decode(encoded) ⇒ Object
Decodes the first value from supplied AMQP data and returns the number of bytes consumed.
110 111 112 |
# File 'lib/codec/data.rb', line 110 def decode(encoded) check(Cproton.pn_data_decode(@impl, encoded, encoded.length)) end |
#described ⇒ Object
127 128 129 130 |
# File 'lib/codec/data.rb', line 127 def described expect Cproton::PN_DESCRIBED enter_exit { Types::Described.new(self.next_object, self.next_object) } end |
#described=(d) ⇒ Object
132 133 134 135 |
# File 'lib/codec/data.rb', line 132 def described= d put_described enter_exit { self << d.descriptor << d.value } end |
#double ⇒ Float
If the current node is a double, returns its value. Otherwise, returns 0.
464 465 466 |
# File 'lib/codec/data.rb', line 464 def double Cproton.pn_data_get_double(@impl) end |
#double=(value) ⇒ Object
Puts a double value.
455 456 457 |
# File 'lib/codec/data.rb', line 455 def double=(value) check(Cproton.pn_data_put_double(@impl, value)) end |
#encode ⇒ Object
Returns a representation of the data encoded in AMQP format.
92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/codec/data.rb', line 92 def encode buffer = "\0"*1024 loop do cd = Cproton.pn_data_encode(@impl, buffer, buffer.length) if cd == Cproton::PN_OVERFLOW buffer *= 2 elsif cd >= 0 return buffer[0...cd] else check(cd) end end end |
#enter_exit ⇒ Object
80 81 82 83 84 85 |
# File 'lib/codec/data.rb', line 80 def enter_exit() enter yield self ensure exit end |
#expect(code) ⇒ Object
121 122 123 124 125 |
# File 'lib/codec/data.rb', line 121 def expect(code) unless code == self.code raise TypeError, "expected #{Cproton.pn_type_name(code)}, got #{Cproton.pn_type_name(self.code)}" end end |
#fill(a, count, what) ⇒ Object
137 138 139 140 141 |
# File 'lib/codec/data.rb', line 137 def fill(a, count, what) a << self.object while self.next raise TypeError, "#{what} expected #{count} elements, got #{a.size}" unless a.size == count a end |
#float ⇒ Float
If the current node is a float, returns its value. Otherwise, returns 0.
447 448 449 |
# File 'lib/codec/data.rb', line 447 def float Cproton.pn_data_get_float(@impl) end |
#float=(value) ⇒ Object
Puts a float value.
438 439 440 |
# File 'lib/codec/data.rb', line 438 def float=(value) check(Cproton.pn_data_put_float(@impl, value)) end |
#get ⇒ Object
Get the current value as a single object.
648 649 650 |
# File 'lib/codec/data.rb', line 648 def get type.get(self); end |
#get_array ⇒ Object
119 |
# File 'lib/codec/data.rb', line 119 def get_array() [Cproton.pn_data_get_array(@impl), array_described?, array_type]; end |
#int ⇒ Integer
If the current node is an integer, returns its value. Otherwise, returns 0.
361 362 363 |
# File 'lib/codec/data.rb', line 361 def int Cproton.pn_data_get_int(@impl) end |
#int=(value) ⇒ Object
Puts an integer value.
Options
-
value - the integer value
352 353 354 |
# File 'lib/codec/data.rb', line 352 def int=(value) check(Cproton.pn_data_put_int(@impl, value)) end |
#list ⇒ Object
143 144 145 146 147 148 149 |
# File 'lib/codec/data.rb', line 143 def list return array if code == Cproton::PN_ARRAY expect Cproton::PN_LIST count = get_list a = [] enter_exit { fill(a, count, __method__) } end |
#list=(a) ⇒ Object
151 152 153 154 |
# File 'lib/codec/data.rb', line 151 def list=(a) put_list enter_exit { a.each { |x| self << x } } end |
#long ⇒ Integer
If the current node is a long, returns its value. Otherwise, returns 0.
412 413 414 |
# File 'lib/codec/data.rb', line 412 def long Cproton.pn_data_get_long(@impl) end |
#long=(value) ⇒ Object
Puts a long value.
405 406 407 |
# File 'lib/codec/data.rb', line 405 def long=(value) check(Cproton.pn_data_put_long(@impl, value)) end |
#map ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/codec/data.rb', line 183 def map expect Cproton::PN_MAP count = self.get_map raise TypeError, "invalid map, total of keys and values is odd" if count.odd? enter_exit do m = {} m[object] = next_object while self.next raise TypeError, "map expected #{count/2} entries, got #{m.size}" unless m.size == count/2 m end end |
#map=(m) ⇒ Object
195 196 197 198 |
# File 'lib/codec/data.rb', line 195 def map= m put_map enter_exit { m.each_pair { |k,v| self << k << v } } end |
#next_object ⇒ Object
Move forward to the next value and return it
222 223 224 225 |
# File 'lib/codec/data.rb', line 222 def next_object self.next or raise TypeError, "not enough data" self.object end |
#null ⇒ Object
Return nil if vallue is null, raise exception otherwise.
201 |
# File 'lib/codec/data.rb', line 201 def null() raise TypeError, "expected null, got #{type || 'empty'}" unless null?; end |
#null=(dummy = nil) ⇒ Object
Set the current value to null
204 |
# File 'lib/codec/data.rb', line 204 def null=(dummy=nil) check(Cproton.pn_data_put_null(@impl)); end |
#null? ⇒ Boolean
Checks if the current node is null.
239 240 241 |
# File 'lib/codec/data.rb', line 239 def null? Cproton.pn_data_is_null(@impl) end |
#object ⇒ Object
Gets the current node, based on how it was encoded.
231 232 233 |
# File 'lib/codec/data.rb', line 231 def object self.type.get(self) if self.type end |
#object=(object) ⇒ Object
Puts an arbitrary object type.
The Data instance will determine which AMQP type is appropriate and will use that to encode the object.
213 214 215 216 |
# File 'lib/codec/data.rb', line 213 def object=(object) Mapping.for_class(object.class).put(self, object) object end |
#put(value, type_code) ⇒ Object
Puts a new value with the given type into the current node.
659 660 661 |
# File 'lib/codec/data.rb', line 659 def put(value, type_code); type_code.put(self, value); end |
#short ⇒ Integer
If the current node is a short, returns its value. Otherwise, returns a 0.
324 325 326 |
# File 'lib/codec/data.rb', line 324 def short Cproton.pn_data_get_short(@impl) end |
#short=(value) ⇒ Object
Puts a short value.
315 316 317 |
# File 'lib/codec/data.rb', line 315 def short=(value) check(Cproton.pn_data_put_short(@impl, value)) end |
#string ⇒ String
If the current node is a string, returns its value. Otherwise, it returns an empty string (“”).
620 621 622 |
# File 'lib/codec/data.rb', line 620 def string Qpid::Proton::Types::UTFString.new(Cproton.pn_data_get_string(@impl)) end |
#string=(value) ⇒ Object
Puts a UTF-8 encoded string value.
NOTE: A nil value is stored as an empty string rather than as a nil.
609 610 611 |
# File 'lib/codec/data.rb', line 609 def string=(value) check(Cproton.pn_data_put_string(@impl, value)) end |
#symbol ⇒ Symbol
If the current node is a symbol, returns its value. Otherwise, it returns an empty string (“”).
637 638 639 |
# File 'lib/codec/data.rb', line 637 def symbol Cproton.pn_data_get_symbol(@impl).to_sym end |
#symbol=(value) ⇒ Object
Puts a symbolic value.
628 629 630 |
# File 'lib/codec/data.rb', line 628 def symbol=(value) check(Cproton.pn_data_put_symbol(@impl, value.to_s)) end |
#timestamp ⇒ Integer
If the current node is a timestamp, returns its value. Otherwise, returns 0.
430 431 432 |
# File 'lib/codec/data.rb', line 430 def Cproton.(@impl) end |
#timestamp=(value) ⇒ Object
Puts a timestamp value.
420 421 422 423 |
# File 'lib/codec/data.rb', line 420 def (value) value = value.to_i if (!value.nil? && value.is_a?(Time)) check(Cproton.(@impl, value)) end |
#type ⇒ Object
89 |
# File 'lib/codec/data.rb', line 89 def type() Mapping.for_code(Cproton.pn_data_type(@impl)); end |
#ubyte ⇒ Integer
If the current node is an unsigned byte, returns its value. Otherwise, it returns 0.
273 274 275 |
# File 'lib/codec/data.rb', line 273 def ubyte Cproton.pn_data_get_ubyte(@impl) end |
#ubyte=(value) ⇒ Object
Puts an unsigned byte value.
264 265 266 |
# File 'lib/codec/data.rb', line 264 def ubyte=(value) check(Cproton.pn_data_put_ubyte(@impl, value)) end |
#uint ⇒ Integer
If the current node is an unsigned int, returns its value. Otherwise, returns 0.
343 344 345 |
# File 'lib/codec/data.rb', line 343 def uint Cproton.pn_data_get_uint(@impl) end |
#uint=(value) ⇒ Object
Puts an unsigned integer value.
332 333 334 335 336 |
# File 'lib/codec/data.rb', line 332 def uint=(value) raise TypeError if value.nil? raise RangeError, "invalid uint: #{value}" if value < 0 check(Cproton.pn_data_put_uint(@impl, value)) end |
#ulong ⇒ Integer
If the current node is an unsigned long, returns its value. Otherwise, returns 0.
397 398 399 |
# File 'lib/codec/data.rb', line 397 def ulong Cproton.pn_data_get_ulong(@impl) end |
#ulong=(value) ⇒ Object
Puts an unsigned long value.
386 387 388 389 390 |
# File 'lib/codec/data.rb', line 386 def ulong=(value) raise TypeError if value.nil? raise RangeError, "invalid ulong: #{value}" if value < 0 check(Cproton.pn_data_put_ulong(@impl, value)) end |
#ushort ⇒ Integer
If the current node is an unsigned short, returns its value. Otherwise, it returns 0.
307 308 309 |
# File 'lib/codec/data.rb', line 307 def ushort Cproton.pn_data_get_ushort(@impl) end |
#ushort=(value) ⇒ Object
Puts an unsigned short value.
298 299 300 |
# File 'lib/codec/data.rb', line 298 def ushort=(value) check(Cproton.pn_data_put_ushort(@impl, value)) end |
#uuid ⇒ String
If the current value is a UUID
, returns its value. Otherwise, it returns nil.
571 572 573 574 575 |
# File 'lib/codec/data.rb', line 571 def uuid value = "" Cproton.pn_data_get_uuid(@impl).each{|val| value += ("%02x" % val)} value.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-") end |
#uuid=(value) ⇒ Object
Puts a UUID
value.
The UUID is expected to be in the format of a string or else a 128-bit integer value.
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/codec/data.rb', line 544 def uuid=(value) raise ::ArgumentError, "invalid uuid: #{value}" if value.nil? # if the uuid that was submitted was numeric value, then translated # it into a hex string, otherwise assume it was a string represtation # and attempt to decode it if value.is_a? Numeric value = "%032x" % value else raise ::ArgumentError, "invalid uuid: #{value}" if !valid_uuid?(value) value = (value[0, 8] + value[9, 4] + value[14, 4] + value[19, 4] + value[24, 12]) end bytes = [] value.scan(/(..)/) {|v| bytes << v[0].to_i(16)} check(Cproton.pn_data_put_uuid(@impl, bytes)) end |