The Data
class provides an interface for decoding, extracting,
creating, and encoding arbitrary AMQP data. A Data
object
contains a tree of AMQP values. Leaf nodes in this tree correspond to
scalars in the AMQP type system such as INT or STRING. Interior nodes in
this tree correspond to compound values in the AMQP type system such as
LIST,MAP, ARRAY, or
DESCRIBED. The root node of the tree is the
Data
object itself and can have an arbitrary number of
children.
A Data
object maintains the notion of the current sibling node
and a current parent node. Siblings are ordered within their parent. Values
are accessed and/or added by using the next, prev, enter, and exit methods to navigate to the desired
location in the tree and using the supplied variety of mutator and accessor
methods to access or add a value of the desired type.
The mutator methods will always add a value after the current node in the tree. If the current node has a next sibling the mutator method will overwrite the value on this node. If there is no current node or the current node has no next sibling then one will be added. The accessor methods always set the added/modified node to the current node. The accessor methods read the value of the current node and do not change which node is current.
The following types of scalar values are supported:
:NULL
:BOOL
:UBYTE
:USHORT
:SHORT
:UINT
:INT
:ULONG
:LONG
:FLOAT
:DOUBLE
:BINARY
:STRING
:SYMBOL
The following types of compound values are supported:
:DESCRIBED
:ARRAY
:LIST
:MAP
Creates a new instance with the specified capacity.
capacity - the capacity
# File lib/qpid_proton/data.rb, line 84 def initialize(capacity = 16) if (!capacity.nil?) && (capacity.is_a?(Fixnum) || capacity.is_a?(Bignum)) @data = Cproton.pn_data(capacity) @free = true else @data = capacity @free = false end # destructor ObjectSpace.define_finalizer(self, self.class.finalize!(@data)) end
Returns the typename for the specified type.
# returns "null" name = Qpid::Proton::Data.type_name(:NULL)
# File lib/qpid_proton/data.rb, line 736 def self.type_name(key) @@type_name[key] end
If the current node is an array, returns a tuple of the element count, a boolean indicating whether the array is described, and the type of each element. Otherwise it returns +(0, false, nil).
Array data can be accessed by entering the array.
# get the details of thecurrent array count, described, array_type = @data.array # enter the node data.enter # get the next node data.next puts "Descriptor: #{data.symbol}" if described (0...count).each do @data.next puts "Element: #{@data.string}" end
# File lib/qpid_proton/data.rb, line 318 def array count = Cproton.pn_data_get_array(@data) described = Cproton.pn_data_is_array_described(@data) array_type = Cproton.pn_data_get_array_type(@data) if array_type == -1 array_type = nil else array_type = Data.type_name(array_type) end [count, described, array_type] end
If the current node is binary, returns its value. Otherwise, it returns an empty string ("").
# File lib/qpid_proton/data.rb, line 686 def binary Cproton.pn_data_get_binary(@data) end
Puts a binary value.
value - the binary value
# File lib/qpid_proton/data.rb, line 680 def binary=(value) check(Cproton.pn_data_put_binary(@data, value)) end
If the current node is a boolean, then it returns the value. Otherwise, it returns false.
# File lib/qpid_proton/data.rb, line 385 def bool Cproton.pn_data_get_bool(@data) end
Puts a boolean value.
value - the boolean value
# File lib/qpid_proton/data.rb, line 379 def bool=(value) check(Cproton.pn_data_put_bool(@data, value)) end
If the current node is an byte, returns its value. Otherwise, it returns 0.
# File lib/qpid_proton/data.rb, line 415 def byte Cproton.pn_data_get_byte(@data) end
Puts a byte value.
value - the byte value
# File lib/qpid_proton/data.rb, line 409 def byte=(value) check(Cproton.pn_data_put_byte(@data, value)) end
If the current node is a character, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 492 def char Cproton.pn_data_get_char(@data) end
Puts a character value.
value - the character value
# File lib/qpid_proton/data.rb, line 486 def char=(value) check(Cproton.pn_data_put_char(@data, value)) end
Clears the object.
# File lib/qpid_proton/data.rb, line 106 def clear Cproton.pn_data_clear(@data) end
If the current node is a decimal128, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 618 def decimal128 value = "" Cproton.pn_data_get_decimal128(@data).each{|val| value += ("%02x" % val)} value.to_i(16) end
Puts a decimal128 value.
value - the decimal128 value
# File lib/qpid_proton/data.rb, line 608 def decimal128=(value) raise TypeError, "invalid decimal128 value: #{value}" if value.nil? value = value.to_s(16).rjust(32, "0") bytes = [] value.scan(%r(..)/) {|v| bytes << v[0].to_i(16)} check(Cproton.pn_data_put_decimal128(@data, bytes)) end
If the current node is a decimal32, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 584 def decimal32 Cproton.pn_data_get_decimal32(@data) end
Puts a decimal32 value.
value - the decimal32 value
# File lib/qpid_proton/data.rb, line 578 def decimal32=(value) check(Cproton.pn_data_put_decimal32(@data, value)) end
If the current node is a decimal64, returns its value. Otherwise, it returns 0.
# File lib/qpid_proton/data.rb, line 599 def decimal64 Cproton.pn_data_get_decimal64(@data) end
Puts a decimal64 value.
value - the decimal64 value
# File lib/qpid_proton/data.rb, line 593 def decimal64=(value) check(Cproton.pn_data_put_decimal64(@data, value)) end
Decodes the first value from supplied AMQP data and returns the number of bytes consumed.
encoded - the encoded data
# File lib/qpid_proton/data.rb, line 177 def decode(encoded) check(Cproton.pn_data_decode(@data, encoded)) end
Checks if the current node is a described value.
The described and value may be accessed by entering the described value.
if @data.described? @data.enter puts "The symbol is #{@data.symbol}" puts "The value is #{@data.string}" end
# File lib/qpid_proton/data.rb, line 360 def described? Cproton.pn_data_is_described(@data) end
If the current node is a double, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 569 def double Cproton.pn_data_get_double(@data) end
Puts a double value.
value - the double value
# File lib/qpid_proton/data.rb, line 563 def double=(value) check(Cproton.pn_data_put_double(@data, value)) end
Returns a representation of the data encoded. in AMQP format.
# File lib/qpid_proton/data.rb, line 156 def encode size = 1024 loop do (cd, enc) = Cproton.pn_data_encode(@data, size) if cd == Cproton::PN_OVERFLOW size *= 2 elsif cd >= 0 return enc else check(cd) end end end
Sets the parent node to the current node and clears the current node.
Clearing the current node sets it before the first child.
# File lib/qpid_proton/data.rb, line 139 def enter Cproton.pn_data_enter(@data) end
Sets the current node to the parent node and the parent node to its own parent.
# File lib/qpid_proton/data.rb, line 145 def exit Cproton.pn_data_exit(@data) end
If the current node is a float, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 554 def float Cproton.pn_data_get_float(@data) end
Puts a float value.
value - the float value
# File lib/qpid_proton/data.rb, line 548 def float=(value) check(Cproton.pn_data_put_float(@data, value)) end
If the current node is an integer, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 477 def int Cproton.pn_data_get_int(@data) end
Puts an integer value.
value - the integer value
# File lib/qpid_proton/data.rb, line 471 def int=(value) check(Cproton.pn_data_put_int(@data, value)) end
If the current node is a list, this returns the number of elements. Otherwise, it returns zero.
List elements can be accessed by entering the list.
count = @data.list @data.enter (0...count).each type = @data.next puts "Value: #{@data.string}" if type == :STRING # ... process other node types end
# File lib/qpid_proton/data.rb, line 214 def list Cproton.pn_data_get_list(@data) end
If the current node is a long, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 523 def long Cproton.pn_data_get_long(@data) end
Puts a long value.
value - the long value
# File lib/qpid_proton/data.rb, line 518 def long=(value) check(Cproton.pn_data_put_long(@data, value)) end
If the current node is a map, this returns the number of child elements. Otherwise, it returns zero.
Key/value pairs can be accessed by entering the map.
count = @data.map @data.enter (0...count).each do type = @data.next puts "Key=#{@data.string}" if type == :STRING # ... process other key types type = @data.next puts "Value=#{@data.string}" if type == :STRING # ... process other value types end @data.exit
# File lib/qpid_proton/data.rb, line 254 def map Cproton.pn_data_get_map(@data) end
Advances the current node to tits next sibling and returns its types.
If there is no next sibling the current node remains unchanged and nil is returned.
# File lib/qpid_proton/data.rb, line 122 def next found = Cproton.pn_data_next(@data) return found ? found : nil end
Returns the type of the current node.
# File lib/qpid_proton/data.rb, line 150 def node_type dtype = Cproton.pn_data_type(@data) return (dtype == -1) ? nil : dtype end
Puts a null value.
# File lib/qpid_proton/data.rb, line 365 def null check(Cproton.pn_data_put_null(@data)) end
Checks if the current node is null.
# File lib/qpid_proton/data.rb, line 370 def null? Cproton.pn_data_is_null(@data) end
Advances the current node to its previous sibling and returns its type.
If there is no previous sibling then the current node remains unchanged and nil is return.
# File lib/qpid_proton/data.rb, line 131 def prev found = Cproton.pn_data_prev(@data) return found ? found : nil end
Puts an array value.
Elements may be filled by entering the array node and putting the element values. The values must all be of the specified array element type.
If an array is described then the first child value of the array is the descriptor and may be of any type.
described - specifies whether the array is described
element_type - the type of the array elements
# create an array of integer values data = Qpid::Proton::Data.new data.put_array(false, :INT) data.enter data.int = 1 data.int = 2 data.int = 3 data.exit # create an array of double values data.put_array(true, :DOUBLE) data.enter data.symbol = "array-descriptor" data.double = 1.1 data.double = 1.2 data.double = 1.3 data.exit
# File lib/qpid_proton/data.rb, line 292 def put_array(described, element_type) check(Cproton.pn_data_put_array(@data, described, Data.type_value(element_type))) end
Puts a described value.
A described node has two children, the descriptor and the value. These are specified by entering the node and putting the desired values.
data = Qpid::Proton::Data.new data.put_described data.enter data.symbol = "value-descriptor" data.string = "the value" data.exit
# File lib/qpid_proton/data.rb, line 345 def put_described check(Cproton.pn_data_put_described(@data)) end
Puts a list value.
Elements may be filled by entering the list node and putting element values.
data = Qpid::Proton::Data.new data.put_list data.enter data.int = 1 data.int = 2 data.int = 3 data.exit
# File lib/qpid_proton/data.rb, line 196 def put_list check(Cproton.pn_data_put_list(@data)) end
Puts a map value.
Elements may be filled by entering the map node and putting alternating key/value pairs.
data = Qpid::Proton::Data.new data.put_map data.enter data.string = "key" data.string = "value" data.exit
# File lib/qpid_proton/data.rb, line 232 def put_map check(Cproton.pn_data_put_map(@data)) end
Clears the current node and sets the parent to the root node.
Clearing the current node sets it before the first node, calling next will advance to the first node.
# File lib/qpid_proton/data.rb, line 114 def rewind Cproton.pn_data_rewind(@data) end
If the current node is a short, returns its value. Otherwise, returns a 0.
# File lib/qpid_proton/data.rb, line 445 def short Cproton.pn_data_get_short(@data) end
Puts a short value.
value - the short value
# File lib/qpid_proton/data.rb, line 439 def short=(value) check(Cproton.pn_data_put_short(@data, value)) end
If the current node is a string, returns its value. Otherwise, it returns an empty string ("").
# File lib/qpid_proton/data.rb, line 703 def string Cproton.pn_data_get_string(@data) end
Puts a unicode string value.
NOTE: A nil value is stored as an empty string rather than as a nil.
value - the unicode string value
# File lib/qpid_proton/data.rb, line 697 def string=(value) check(Cproton.pn_data_put_string(@data, value)) end
If the current node is a symbol, returns its value. Otherwise, it returns an empty string ("").
# File lib/qpid_proton/data.rb, line 718 def symbol Cproton.pn_data_get_symbol(@data) end
Puts a symbolic value.
value - the symbol name
# File lib/qpid_proton/data.rb, line 712 def symbol=(value) check(Cproton.pn_data_put_symbol(@data, value)) end
If the current node is a timestamp, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 539 def timestamp Cproton.pn_data_get_timestamp(@data) end
Puts a timestamp value.
value - the timestamp value
# File lib/qpid_proton/data.rb, line 532 def timestamp=(value) value = value.to_i if (!value.nil? && value.is_a?(Time)) check(Cproton.pn_data_put_timestamp(@data, value)) end
If the current node is an unsigned byte, returns its value. Otherwise, it reutrns 0.
# File lib/qpid_proton/data.rb, line 400 def ubyte Cproton.pn_data_get_ubyte(@data) end
Puts an unsigned byte value.
value - the unsigned byte value
# File lib/qpid_proton/data.rb, line 394 def ubyte=(value) check(Cproton.pn_data_put_ubyte(@data, value)) end
If the current node is an unsigned int, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 462 def uint Cproton.pn_data_get_uint(@data) end
Puts an unsigned integer value.
value - the unsigned integer value
# File lib/qpid_proton/data.rb, line 454 def uint=(value) raise TypeError if value.nil? raise RangeError, "invalid uint: #{value}" if value < 0 check(Cproton.pn_data_put_uint(@data, value)) end
If the current node is an unsigned long, returns its value. Otherwise, returns 0.
# File lib/qpid_proton/data.rb, line 509 def ulong Cproton.pn_data_get_ulong(@data) end
Puts an unsigned long value.
value - the unsigned long value
# File lib/qpid_proton/data.rb, line 501 def ulong=(value) raise TypeError if value.nil? raise RangeError, "invalid ulong: #{value}" if value < 0 check(Cproton.pn_data_put_ulong(@data, value)) end
If the current node is an unsigned short, returns its value. Otherwise, it returns 0.
# File lib/qpid_proton/data.rb, line 430 def ushort Cproton.pn_data_get_ushort(@data) end
Puts an unsigned short value.
value - the unsigned short value
# File lib/qpid_proton/data.rb, line 424 def ushort=(value) check(Cproton.pn_data_put_ushort(@data, value)) end
If the current value is a UUID
, returns its value. Otherwise,
it returns nil.
# File lib/qpid_proton/data.rb, line 669 def uuid value = "" Cproton.pn_data_get_uuid(@data).each{|val| value += ("%02x" % val)} value.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-") end
Puts a UUID
value.
The UUID is expected to be in the format of a string or else a 128-bit integer value.
value - the UUID
# set a uuid value from a string value require 'securerandom' @data.uuid = SecureRandom.uuid # or @data.uuid = "fd0289a5-8eec-4a08-9283-81d02c9d2fff" # set a uuid value from a 128-bit value @data.uuid = 0 # sets to 00000000-0000-0000-0000-000000000000
# File lib/qpid_proton/data.rb, line 645 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(%r(..)/) {|v| bytes << v[0].to_i(16)} check(Cproton.pn_data_put_uuid(@data, bytes)) end