| 1 |
# Licensed to the Apache Software Foundation (ASF) under one |
| 2 |
# or more contributor license agreements. See the NOTICE file |
| 3 |
# distributed with this work for additional information |
| 4 |
# regarding copyright ownership. The ASF licenses this file |
| 5 |
# to you under the Apache License, Version 2.0 (the |
| 6 |
# "License"); you may not use this file except in compliance |
| 7 |
# with the License. You may obtain a copy of the License at |
| 8 |
# |
| 9 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
# |
| 11 |
# Unless required by applicable law or agreed to in writing, |
| 12 |
# software distributed under the License is distributed on an |
| 13 |
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 |
# KIND, either express or implied. See the License for the |
| 15 |
# specific language governing permissions and limitations |
| 16 |
# under the License. |
| 17 |
|
| 18 |
|
| 19 |
#-- |
| 20 |
# Patch the Array class to provide methods for adding its contents |
| 21 |
# to a Qpid::Proton::Codec::Data instance. |
| 22 |
#++ |
| 23 |
|
| 24 |
module Qpid::Proton |
| 25 |
module Types |
| 26 |
|
| 27 |
# @deprecated use {UniformArray} |
| 28 |
class ArrayHeader |
| 29 |
def initialize(type, descriptor = nil) |
| 30 |
Util::Deprecation.deprecated ArrayHeader, "UniformArray" |
| 31 |
@type, @descriptor = Codec::Mapping[type], descriptor |
| 32 |
end |
| 33 |
attr_reader :type, :descriptor |
| 34 |
def described?() [email protected]?; end |
| 35 |
def <=>(x) [@type, @descriptor] <=> [x.type, x.descriptor]; end |
| 36 |
include Comparable |
| 37 |
end |
| 38 |
|
| 39 |
# *Unsettled API* - An array that is converted to/from an AMQP array of uniform element type. |
| 40 |
# A plain ruby +::Array+ is converted to/from an AMQP list, which can contain mixed type elements. |
| 41 |
# If invalid elements are included, then {TypeError} will be raised when encoding to AMQP. |
| 42 |
class UniformArray < ::Array |
| 43 |
|
| 44 |
# Construct a uniform array, which will be converted to an AMQP array. |
| 45 |
# A plain ruby +::Array+ is converted to/from an AMQP list, containing mixed type elements. |
| 46 |
# |
| 47 |
# @param type [Type] Elements must be convertible to this AMQP type. |
| 48 |
# @param elements [Enumerator] Initial elements for the array |
| 49 |
# @param descriptor [Object] Optional array descriptor |
| 50 |
def initialize(type, elements=nil, descriptor=nil) |
| 51 |
@type, @descriptor = type, descriptor |
| 52 |
@proton_array_header = nil |
| 53 |
raise ArgumentError, "no type specified for array" if @type.nil? |
| 54 |
super elements if elements |
| 55 |
end |
| 56 |
|
| 57 |
# @deprecated backwards compatibility {UniformArray} |
| 58 |
def proton_array_header |
| 59 |
@proton_array_header ||= ArrayHeader.new(@type, @descriptor) # Deprecated |
| 60 |
end |
| 61 |
|
| 62 |
# @return [Type] Array elements must be convertible to this AMQP type |
| 63 |
attr_reader :type |
| 64 |
|
| 65 |
# @return [Object] Optional descriptor. |
| 66 |
attr_reader :descriptor |
| 67 |
|
| 68 |
def inspect() "#{self.class.name}<#{type}>#{super}"; end |
| 69 |
|
| 70 |
def <=>(x) |
| 71 |
ret = [@type, @descriptor] <=> [x.type, x.descriptor] |
| 72 |
ret == 0 ? super : ret |
| 73 |
end |
| 74 |
end |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
# {Array} is converted to/from an AMQP list, which is allowed to hold mixed-type elements. |
| 79 |
# Use {UniformArray} to convert/from an AMQP array with uniform element type. |
| 80 |
class ::Array |
| 81 |
# @deprecated use {UniformArray} |
| 82 |
def proton_array_header |
| 83 |
Qpid::Proton::Util::Deprecation.deprecated __method__, UniformArray |
| 84 |
@proton_array_header |
| 85 |
end |
| 86 |
|
| 87 |
# @deprecated use {UniformArray} |
| 88 |
def proton_array_header=(h) |
| 89 |
Qpid::Proton::Util::Deprecation.deprecated __method__, UniformArray |
| 90 |
@proton_array_header= h |
| 91 |
end |
| 92 |
|
| 93 |
# @deprecated use {UniformArray} |
| 94 |
def proton_described?() |
| 95 |
Qpid::Proton::Util::Deprecation.deprecated __method__, UniformArray |
| 96 |
@proton_array_header && @proton_array_header.described? |
| 97 |
end |
| 98 |
|
| 99 |
# @deprecated |
| 100 |
def proton_put(data) |
| 101 |
Qpid::Proton::Util::Deprecation.deprecated __method__, "Codec::Data#array=, Codec::Data#list=" |
| 102 |
raise TypeError, "nil data" unless data |
| 103 |
if @proton_array_header && @proton_array_header.type |
| 104 |
data.array = self |
| 105 |
else |
| 106 |
data.list = self |
| 107 |
end |
| 108 |
end |
| 109 |
|
| 110 |
# @deprecated |
| 111 |
def self.proton_get(data) |
| 112 |
Qpid::Proton::Util::Deprecation.deprecated __method__, "Codec::Data#list" |
| 113 |
data.list |
| 114 |
end |
| 115 |
end |
| 116 |
|