| 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 |
module Qpid::Proton |
| 19 |
# @private |
| 20 |
module Codec |
| 21 |
|
| 22 |
DataError = ::TypeError |
| 23 |
|
| 24 |
# @private wrapper for pn_data_t* |
| 25 |
# Raises TypeError for invalid conversions |
| 26 |
class Data |
| 27 |
|
| 28 |
# @private |
| 29 |
PROTON_METHOD_PREFIX = "pn_data" |
| 30 |
# @private |
| 31 |
include Util::Wrapper |
| 32 |
|
| 33 |
# @private |
| 34 |
# Convert a pn_data_t* containing a single value to a ruby object. |
| 35 |
# @return [Object, nil] The ruby value extracted from +impl+ or nil if impl is empty |
| 36 |
def self.to_object(impl) |
| 37 |
if (Cproton.pn_data_size(impl) > 0) |
| 38 |
d = Data.new(impl) |
| 39 |
d.rewind |
| 40 |
d.next_object |
| 41 |
end |
| 42 |
end |
| 43 |
|
| 44 |
# @private |
| 45 |
# Convert a pn_data_t* containing an AMQP "multiple" field to an Array or nil. |
| 46 |
# A "multiple" field can be encoded as an array or a single value - always return Array. |
| 47 |
# @return [Array, nil] The ruby Array extracted from +impl+ or nil if impl is empty |
| 48 |
def self.to_multiple(impl) |
| 49 |
o = self.to_object(impl) |
| 50 |
Array(o) if o |
| 51 |
end |
| 52 |
|
| 53 |
# @private |
| 54 |
# Clear a pn_data_t* and convert a ruby object into it. If x==nil leave it empty. |
| 55 |
def self.from_object(impl, x) |
| 56 |
d = Data.new(impl) |
| 57 |
d.clear |
| 58 |
d.object = x if x |
| 59 |
nil |
| 60 |
end |
| 61 |
|
| 62 |
# @overload initialize(capacity) |
| 63 |
# @param capacity [Integer] capacity for the new data instance. |
| 64 |
# @overload instance(impl) |
| 65 |
# @param impl [SWIG::pn_data_t*] wrap the C impl pointer. |
| 66 |
def initialize(capacity = 16) |
| 67 |
if capacity.is_a?(Integer) |
| 68 |
@impl = Cproton.pn_data(capacity.to_i) |
| 69 |
@free = true |
| 70 |
else |
| 71 |
# Assume non-integer capacity is a SWIG::pn_data_t* |
| 72 |
@impl = capacity |
| 73 |
@free = false |
| 74 |
end |
| 75 |
|
| 76 |
# destructor |
| 77 |
ObjectSpace.define_finalizer(self, self.class.finalize!(@impl, @free)) |
| 78 |
end |
| 79 |
|
| 80 |
# @private |
| 81 |
def self.finalize!(impl, free) |
| 82 |
proc { |
| 83 |
Cproton.pn_data_free(impl) if free |
| 84 |
} |
| 85 |
end |
| 86 |
|
| 87 |
proton_caller :clear, :rewind, :next, :prev, :enter, :exit |
| 88 |
|
| 89 |
def enter_exit() |
| 90 |
enter |
| 91 |
yield self |
| 92 |
ensure |
| 93 |
exit |
| 94 |
end |
| 95 |
|
| 96 |
def code() Cproton.pn_data_type(@impl); end |
| 97 |
|
| 98 |
def type() Mapping.for_code(Cproton.pn_data_type(@impl)); end |
| 99 |
|
| 100 |
# Returns a representation of the data encoded in AMQP format. |
| 101 |
def encode |
| 102 |
buffer = "\0"*1024 |
| 103 |
loop do |
| 104 |
cd = Cproton.pn_data_encode(@impl, buffer, buffer.length) |
| 105 |
if cd == Cproton::PN_OVERFLOW |
| 106 |
buffer *= 2 |
| 107 |
elsif cd >= 0 |
| 108 |
return buffer[0...cd] |
| 109 |
else |
| 110 |
check(cd) |
| 111 |
end |
| 112 |
end |
| 113 |
end |
| 114 |
|
| 115 |
# Decodes the first value from supplied AMQP data and returns the number |
| 116 |
# of bytes consumed. |
| 117 |
# |
| 118 |
# @param encoded [String] The encoded data. |
| 119 |
def decode(encoded) |
| 120 |
check(Cproton.pn_data_decode(@impl, encoded, encoded.length)) |
| 121 |
end |
| 122 |
|
| 123 |
proton_is :described, :array_described |
| 124 |
proton_caller :put_described |
| 125 |
proton_caller :put_list, :get_list, :put_map, :get_map |
| 126 |
proton_get :array_type |
| 127 |
proton_caller :put_array |
| 128 |
def get_array() [Cproton.pn_data_get_array(@impl), array_described?, array_type]; end |
| 129 |
|
| 130 |
def expect(code) |
| 131 |
unless code == self.code |
| 132 |
raise TypeError, "expected #{Cproton.pn_type_name(code)}, got #{Cproton.pn_type_name(self.code)}" |
| 133 |
end |
| 134 |
end |
| 135 |
|
| 136 |
def described |
| 137 |
expect Cproton::PN_DESCRIBED |
| 138 |
enter_exit { Types::Described.new(self.next_object, self.next_object) } |
| 139 |
end |
| 140 |
|
| 141 |
def described= d |
| 142 |
put_described |
| 143 |
enter_exit { self << d.descriptor << d.value } |
| 144 |
end |
| 145 |
|
| 146 |
def fill(a, count, what) |
| 147 |
a << self.object while self.next |
| 148 |
raise TypeError, "#{what} expected #{count} elements, got #{a.size}" unless a.size == count |
| 149 |
a |
| 150 |
end |
| 151 |
|
| 152 |
def list |
| 153 |
return array if code == Cproton::PN_ARRAY |
| 154 |
expect Cproton::PN_LIST |
| 155 |
count = get_list |
| 156 |
a = [] |
| 157 |
enter_exit { fill(a, count, __method__) } |
| 158 |
end |
| 159 |
|
| 160 |
def list=(a) |
| 161 |
put_list |
| 162 |
enter_exit { a.each { |x| self << x } } |
| 163 |
end |
| 164 |
|
| 165 |
def array |
| 166 |
return list if code == Cproton::PN_LIST |
| 167 |
expect Cproton::PN_ARRAY |
| 168 |
count, d, t = get_array |
| 169 |
enter_exit do |
| 170 |
desc = next_object if d |
| 171 |
a = Types::UniformArray.new(t, nil, desc) |
| 172 |
fill(a, count, "array") |
| 173 |
end |
| 174 |
end |
| 175 |
|
| 176 |
def array=(a) |
| 177 |
t = a.type if a.respond_to? :type |
| 178 |
d = a.descriptor if a.respond_to? :descriptor |
| 179 |
if (h = a.instance_variable_get(:@proton_array_header)) |
| 180 |
t ||= h.type |
| 181 |
d ||= h.descriptor |
| 182 |
end |
| 183 |
raise TypeError, "no type when converting #{a.class} to an array" unless t |
| 184 |
put_array(!d.nil?, t.code) |
| 185 |
m = Mapping[t] |
| 186 |
enter_exit do |
| 187 |
self << d unless d.nil? |
| 188 |
a.each { |e| m.put(self, e); } |
| 189 |
end |
| 190 |
end |
| 191 |
|
| 192 |
def map |
| 193 |
expect Cproton::PN_MAP |
| 194 |
count = self.get_map |
| 195 |
raise TypeError, "invalid map, total of keys and values is odd" if count.odd? |
| 196 |
enter_exit do |
| 197 |
m = {} |
| 198 |
m[object] = next_object while self.next |
| 199 |
raise TypeError, "map expected #{count/2} entries, got #{m.size}" unless m.size == count/2 |
| 200 |
m |
| 201 |
end |
| 202 |
end |
| 203 |
|
| 204 |
def map= m |
| 205 |
put_map |
| 206 |
enter_exit { m.each_pair { |k,v| self << k << v } } |
| 207 |
end |
| 208 |
|
| 209 |
# Return nil if vallue is null, raise exception otherwise. |
| 210 |
def null() raise TypeError, "expected null, got #{type || 'empty'}" unless null?; end |
| 211 |
|
| 212 |
# Set the current value to null |
| 213 |
def null=(dummy=nil) check(Cproton.pn_data_put_null(@impl)); end |
| 214 |
|
| 215 |
# Puts an arbitrary object type. |
| 216 |
# |
| 217 |
# The Data instance will determine which AMQP type is appropriate and will |
| 218 |
# use that to encode the object. |
| 219 |
# |
| 220 |
# @param object [Object] The value. |
| 221 |
# |
| 222 |
def object=(object) |
| 223 |
Mapping.for_class(object.class).put(self, object) |
| 224 |
object |
| 225 |
end |
| 226 |
|
| 227 |
# Add an arbitrary data value using object=, return self |
| 228 |
def <<(x) self.object=x; self; end |
| 229 |
|
| 230 |
# Move forward to the next value and return it |
| 231 |
def next_object |
| 232 |
self.next or raise TypeError, "not enough data" |
| 233 |
self.object |
| 234 |
end |
| 235 |
|
| 236 |
# Gets the current node, based on how it was encoded. |
| 237 |
# |
| 238 |
# @return [Object] The current node. |
| 239 |
# |
| 240 |
def object |
| 241 |
self.type.get(self) if self.type |
| 242 |
end |
| 243 |
|
| 244 |
# Checks if the current node is null. |
| 245 |
# |
| 246 |
# @return [Boolean] True if the node is null. |
| 247 |
# |
| 248 |
def null? |
| 249 |
Cproton.pn_data_is_null(@impl) |
| 250 |
end |
| 251 |
|
| 252 |
# Puts a boolean value. |
| 253 |
# |
| 254 |
# @param value [Boolean] The boolean value. |
| 255 |
# |
| 256 |
def bool=(value) |
| 257 |
check(Cproton.pn_data_put_bool(@impl, value)) |
| 258 |
end |
| 259 |
|
| 260 |
# If the current node is a boolean, then it returns the value. Otherwise, |
| 261 |
# it returns false. |
| 262 |
# |
| 263 |
# @return [Boolean] The boolean value. |
| 264 |
# |
| 265 |
def bool |
| 266 |
Cproton.pn_data_get_bool(@impl) |
| 267 |
end |
| 268 |
|
| 269 |
# Puts an unsigned byte value. |
| 270 |
# |
| 271 |
# @param value [Integer] The unsigned byte value. |
| 272 |
# |
| 273 |
def ubyte=(value) |
| 274 |
check(Cproton.pn_data_put_ubyte(@impl, value)) |
| 275 |
end |
| 276 |
|
| 277 |
# If the current node is an unsigned byte, returns its value. Otherwise, |
| 278 |
# it returns 0. |
| 279 |
# |
| 280 |
# @return [Integer] The unsigned byte value. |
| 281 |
# |
| 282 |
def ubyte |
| 283 |
Cproton.pn_data_get_ubyte(@impl) |
| 284 |
end |
| 285 |
|
| 286 |
# Puts a byte value. |
| 287 |
# |
| 288 |
# @param value [Integer] The byte value. |
| 289 |
# |
| 290 |
def byte=(value) |
| 291 |
check(Cproton.pn_data_put_byte(@impl, value)) |
| 292 |
end |
| 293 |
|
| 294 |
# If the current node is an byte, returns its value. Otherwise, |
| 295 |
# it returns 0. |
| 296 |
# |
| 297 |
# @return [Integer] The byte value. |
| 298 |
# |
| 299 |
def byte |
| 300 |
Cproton.pn_data_get_byte(@impl) |
| 301 |
end |
| 302 |
|
| 303 |
# Puts an unsigned short value. |
| 304 |
# |
| 305 |
# @param value [Integer] The unsigned short value |
| 306 |
# |
| 307 |
def ushort=(value) |
| 308 |
check(Cproton.pn_data_put_ushort(@impl, value)) |
| 309 |
end |
| 310 |
|
| 311 |
# If the current node is an unsigned short, returns its value. Otherwise, |
| 312 |
# it returns 0. |
| 313 |
# |
| 314 |
# @return [Integer] The unsigned short value. |
| 315 |
# |
| 316 |
def ushort |
| 317 |
Cproton.pn_data_get_ushort(@impl) |
| 318 |
end |
| 319 |
|
| 320 |
# Puts a short value. |
| 321 |
# |
| 322 |
# @param value [Integer] The short value. |
| 323 |
# |
| 324 |
def short=(value) |
| 325 |
check(Cproton.pn_data_put_short(@impl, value)) |
| 326 |
end |
| 327 |
|
| 328 |
# If the current node is a short, returns its value. Otherwise, |
| 329 |
# returns a 0. |
| 330 |
# |
| 331 |
# @return [Integer] The short value. |
| 332 |
# |
| 333 |
def short |
| 334 |
Cproton.pn_data_get_short(@impl) |
| 335 |
end |
| 336 |
|
| 337 |
# Puts an unsigned integer value. |
| 338 |
# |
| 339 |
# @param value [Integer] the unsigned integer value |
| 340 |
# |
| 341 |
def uint=(value) |
| 342 |
raise TypeError if value.nil? |
| 343 |
raise RangeError, "invalid uint: #{value}" if value < 0 |
| 344 |
check(Cproton.pn_data_put_uint(@impl, value)) |
| 345 |
end |
| 346 |
|
| 347 |
# If the current node is an unsigned int, returns its value. Otherwise, |
| 348 |
# returns 0. |
| 349 |
# |
| 350 |
# @return [Integer] The unsigned integer value. |
| 351 |
# |
| 352 |
def uint |
| 353 |
Cproton.pn_data_get_uint(@impl) |
| 354 |
end |
| 355 |
|
| 356 |
# Puts an integer value. |
| 357 |
# |
| 358 |
# ==== Options |
| 359 |
# |
| 360 |
# * value - the integer value |
| 361 |
def int=(value) |
| 362 |
check(Cproton.pn_data_put_int(@impl, value)) |
| 363 |
end |
| 364 |
|
| 365 |
# If the current node is an integer, returns its value. Otherwise, |
| 366 |
# returns 0. |
| 367 |
# |
| 368 |
# @return [Integer] The integer value. |
| 369 |
# |
| 370 |
def int |
| 371 |
Cproton.pn_data_get_int(@impl) |
| 372 |
end |
| 373 |
|
| 374 |
# Puts a character value. |
| 375 |
# |
| 376 |
# @param value [Integer] The character value. |
| 377 |
# |
| 378 |
def char=(value) |
| 379 |
check(Cproton.pn_data_put_char(@impl, value)) |
| 380 |
end |
| 381 |
|
| 382 |
# If the current node is a character, returns its value. Otherwise, |
| 383 |
# returns 0. |
| 384 |
# |
| 385 |
# @return [Integer] The character value. |
| 386 |
# |
| 387 |
def char |
| 388 |
Cproton.pn_data_get_char(@impl) |
| 389 |
end |
| 390 |
|
| 391 |
# Puts an unsigned long value. |
| 392 |
# |
| 393 |
# @param value [Integer] The unsigned long value. |
| 394 |
# |
| 395 |
def ulong=(value) |
| 396 |
raise TypeError if value.nil? |
| 397 |
raise RangeError, "invalid ulong: #{value}" if value < 0 |
| 398 |
check(Cproton.pn_data_put_ulong(@impl, value)) |
| 399 |
end |
| 400 |
|
| 401 |
# If the current node is an unsigned long, returns its value. Otherwise, |
| 402 |
# returns 0. |
| 403 |
# |
| 404 |
# @return [Integer] The unsigned long value. |
| 405 |
# |
| 406 |
def ulong |
| 407 |
Cproton.pn_data_get_ulong(@impl) |
| 408 |
end |
| 409 |
|
| 410 |
# Puts a long value. |
| 411 |
# |
| 412 |
# @param value [Integer] The long value. |
| 413 |
# |
| 414 |
def long=(value) |
| 415 |
check(Cproton.pn_data_put_long(@impl, value)) |
| 416 |
end |
| 417 |
|
| 418 |
# If the current node is a long, returns its value. Otherwise, returns 0. |
| 419 |
# |
| 420 |
# @return [Integer] The long value. |
| 421 |
def long |
| 422 |
Cproton.pn_data_get_long(@impl) |
| 423 |
end |
| 424 |
|
| 425 |
# Puts a timestamp value. |
| 426 |
# |
| 427 |
# @param value [Integer] The timestamp value. |
| 428 |
# |
| 429 |
def timestamp=(value) |
| 430 |
value = value.to_i if (!value.nil? && value.is_a?(Time)) |
| 431 |
check(Cproton.pn_data_put_timestamp(@impl, value)) |
| 432 |
end |
| 433 |
|
| 434 |
# If the current node is a timestamp, returns its value. Otherwise, |
| 435 |
# returns 0. |
| 436 |
# |
| 437 |
# @return [Integer] The timestamp value. |
| 438 |
# |
| 439 |
def timestamp |
| 440 |
Cproton.pn_data_get_timestamp(@impl) |
| 441 |
end |
| 442 |
|
| 443 |
# Puts a float value. |
| 444 |
# |
| 445 |
# @param value [Float] The floating point value. |
| 446 |
# |
| 447 |
def float=(value) |
| 448 |
check(Cproton.pn_data_put_float(@impl, value)) |
| 449 |
end |
| 450 |
|
| 451 |
# If the current node is a float, returns its value. Otherwise, |
| 452 |
# returns 0. |
| 453 |
# |
| 454 |
# @return [Float] The floating point value. |
| 455 |
# |
| 456 |
def float |
| 457 |
Cproton.pn_data_get_float(@impl) |
| 458 |
end |
| 459 |
|
| 460 |
# Puts a double value. |
| 461 |
# |
| 462 |
# @param value [Float] The double precision floating point value. |
| 463 |
# |
| 464 |
def double=(value) |
| 465 |
check(Cproton.pn_data_put_double(@impl, value)) |
| 466 |
end |
| 467 |
|
| 468 |
# If the current node is a double, returns its value. Otherwise, |
| 469 |
# returns 0. |
| 470 |
# |
| 471 |
# @return [Float] The double precision floating point value. |
| 472 |
# |
| 473 |
def double |
| 474 |
Cproton.pn_data_get_double(@impl) |
| 475 |
end |
| 476 |
|
| 477 |
# Puts a decimal32 value. |
| 478 |
# |
| 479 |
# @param value [Integer] The decimal32 value. |
| 480 |
# |
| 481 |
def decimal32=(value) |
| 482 |
check(Cproton.pn_data_put_decimal32(@impl, value)) |
| 483 |
end |
| 484 |
|
| 485 |
# If the current node is a decimal32, returns its value. Otherwise, |
| 486 |
# returns 0. |
| 487 |
# |
| 488 |
# @return [Integer] The decimal32 value. |
| 489 |
# |
| 490 |
def decimal32 |
| 491 |
Cproton.pn_data_get_decimal32(@impl) |
| 492 |
end |
| 493 |
|
| 494 |
# Puts a decimal64 value. |
| 495 |
# |
| 496 |
# @param value [Integer] The decimal64 value. |
| 497 |
# |
| 498 |
def decimal64=(value) |
| 499 |
check(Cproton.pn_data_put_decimal64(@impl, value)) |
| 500 |
end |
| 501 |
|
| 502 |
# If the current node is a decimal64, returns its value. Otherwise, |
| 503 |
# it returns 0. |
| 504 |
# |
| 505 |
# @return [Integer] The decimal64 value. |
| 506 |
# |
| 507 |
def decimal64 |
| 508 |
Cproton.pn_data_get_decimal64(@impl) |
| 509 |
end |
| 510 |
|
| 511 |
# Puts a decimal128 value. |
| 512 |
# |
| 513 |
# @param value [Integer] The decimal128 value. |
| 514 |
# |
| 515 |
def decimal128=(value) |
| 516 |
raise TypeError, "invalid decimal128 value: #{value}" if value.nil? |
| 517 |
value = value.to_s(16).rjust(32, "0") |
| 518 |
bytes = [] |
| 519 |
value.scan(/(..)/) {|v| bytes << v[0].to_i(16)} |
| 520 |
check(Cproton.pn_data_put_decimal128(@impl, bytes)) |
| 521 |
end |
| 522 |
|
| 523 |
# If the current node is a decimal128, returns its value. Otherwise, |
| 524 |
# returns 0. |
| 525 |
# |
| 526 |
# @return [Integer] The decimal128 value. |
| 527 |
# |
| 528 |
def decimal128 |
| 529 |
value = "" |
| 530 |
Cproton.pn_data_get_decimal128(@impl).each{|val| value += ("%02x" % val)} |
| 531 |
value.to_i(16) |
| 532 |
end |
| 533 |
|
| 534 |
# Puts a +UUID+ value. |
| 535 |
# |
| 536 |
# The UUID is expected to be in the format of a string or else a 128-bit |
| 537 |
# integer value. |
| 538 |
# |
| 539 |
# @param value [String, Numeric] A string or numeric representation of the UUID. |
| 540 |
# |
| 541 |
# @example |
| 542 |
# |
| 543 |
# # set a uuid value from a string value |
| 544 |
# require 'securerandom' |
| 545 |
# @impl.uuid = SecureRandom.uuid |
| 546 |
# |
| 547 |
# # or |
| 548 |
# @impl.uuid = "fd0289a5-8eec-4a08-9283-81d02c9d2fff" |
| 549 |
# |
| 550 |
# # set a uuid value from a 128-bit value |
| 551 |
# @impl.uuid = 0 # sets to 00000000-0000-0000-0000-000000000000 |
| 552 |
# |
| 553 |
def uuid=(value) |
| 554 |
raise ::ArgumentError, "invalid uuid: #{value}" if value.nil? |
| 555 |
|
| 556 |
# if the uuid that was submitted was numeric value, then translated |
| 557 |
# it into a hex string, otherwise assume it was a string represtation |
| 558 |
# and attempt to decode it |
| 559 |
if value.is_a? Numeric |
| 560 |
value = "%032x" % value |
| 561 |
else |
| 562 |
raise ::ArgumentError, "invalid uuid: #{value}" if !valid_uuid?(value) |
| 563 |
|
| 564 |
value = (value[0, 8] + |
| 565 |
value[9, 4] + |
| 566 |
value[14, 4] + |
| 567 |
value[19, 4] + |
| 568 |
value[24, 12]) |
| 569 |
end |
| 570 |
bytes = [] |
| 571 |
value.scan(/(..)/) {|v| bytes << v[0].to_i(16)} |
| 572 |
check(Cproton.pn_data_put_uuid(@impl, bytes)) |
| 573 |
end |
| 574 |
|
| 575 |
# If the current value is a +UUID+, returns its value. Otherwise, |
| 576 |
# it returns nil. |
| 577 |
# |
| 578 |
# @return [String] The string representation of the UUID. |
| 579 |
# |
| 580 |
def uuid |
| 581 |
value = "" |
| 582 |
Cproton.pn_data_get_uuid(@impl).each{|val| value += ("%02x" % val)} |
| 583 |
value.insert(8, "-").insert(13, "-").insert(18, "-").insert(23, "-") |
| 584 |
end |
| 585 |
|
| 586 |
# Puts a binary value. |
| 587 |
# |
| 588 |
# A binary string is encoded as an ASCII 8-bit string value. This is in |
| 589 |
# contranst to other strings, which are treated as UTF-8 encoded. |
| 590 |
# |
| 591 |
# @param value [String] An arbitrary string value. |
| 592 |
# |
| 593 |
# @see #string= |
| 594 |
# |
| 595 |
def binary=(value) |
| 596 |
check(Cproton.pn_data_put_binary(@impl, value)) |
| 597 |
end |
| 598 |
|
| 599 |
# If the current node is binary, returns its value. Otherwise, it returns |
| 600 |
# an empty string (""). |
| 601 |
# |
| 602 |
# @return [String] The binary string. |
| 603 |
# |
| 604 |
# @see #string |
| 605 |
# |
| 606 |
def binary |
| 607 |
Qpid::Proton::Types::BinaryString.new(Cproton.pn_data_get_binary(@impl)) |
| 608 |
end |
| 609 |
|
| 610 |
# Puts a UTF-8 encoded string value. |
| 611 |
# |
| 612 |
# *NOTE:* A nil value is stored as an empty string rather than as a nil. |
| 613 |
# |
| 614 |
# @param value [String] The UTF-8 encoded string value. |
| 615 |
# |
| 616 |
# @see #binary= |
| 617 |
# |
| 618 |
def string=(value) |
| 619 |
check(Cproton.pn_data_put_string(@impl, value)) |
| 620 |
end |
| 621 |
|
| 622 |
# If the current node is a string, returns its value. Otherwise, it |
| 623 |
# returns an empty string (""). |
| 624 |
# |
| 625 |
# @return [String] The UTF-8 encoded string. |
| 626 |
# |
| 627 |
# @see #binary |
| 628 |
# |
| 629 |
def string |
| 630 |
Qpid::Proton::Types::UTFString.new(Cproton.pn_data_get_string(@impl)) |
| 631 |
end |
| 632 |
|
| 633 |
# Puts a symbolic value. |
| 634 |
# |
| 635 |
# @param value [String|Symbol] The symbolic string value. |
| 636 |
# |
| 637 |
def symbol=(value) |
| 638 |
check(Cproton.pn_data_put_symbol(@impl, value.to_s)) |
| 639 |
end |
| 640 |
|
| 641 |
# If the current node is a symbol, returns its value. Otherwise, it |
| 642 |
# returns an empty string (""). |
| 643 |
# |
| 644 |
# @return [Symbol] The symbol value. |
| 645 |
# |
| 646 |
def symbol |
| 647 |
Cproton.pn_data_get_symbol(@impl).to_sym |
| 648 |
end |
| 649 |
|
| 650 |
# Get the current value as a single object. |
| 651 |
# |
| 652 |
# @return [Object] The current node's object. |
| 653 |
# |
| 654 |
# @see #type_code |
| 655 |
# @see #type |
| 656 |
# |
| 657 |
def get |
| 658 |
type.get(self); |
| 659 |
end |
| 660 |
|
| 661 |
# Puts a new value with the given type into the current node. |
| 662 |
# |
| 663 |
# @param value [Object] The value. |
| 664 |
# @param type_code [Mapping] The value's type. |
| 665 |
# |
| 666 |
# @private |
| 667 |
# |
| 668 |
def put(value, type_code); |
| 669 |
type_code.put(self, value); |
| 670 |
end |
| 671 |
|
| 672 |
private |
| 673 |
|
| 674 |
def valid_uuid?(value) |
| 675 |
# ensure that the UUID is in the right format |
| 676 |
# xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx |
| 677 |
value =~ /[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}/ |
| 678 |
end |
| 679 |
|
| 680 |
# @private |
| 681 |
def check(err) |
| 682 |
if err < 0 |
| 683 |
raise TypeError, "[#{err}]: #{Cproton.pn_data_error(@impl)}" |
| 684 |
else |
| 685 |
return err |
| 686 |
end |
| 687 |
end |
| 688 |
end |
| 689 |
end |
| 690 |
end |