Debian Codegraph @

ruby/lib/codec/mapping.rb

raw
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 module Qpid::Proton
20 module Codec
21
22 # Maps between Proton types and their Ruby native language counterparts.
23 #
24 # @private
25 class Mapping
26
27 attr_reader :code
28 attr_reader :put_method
29 attr_reader :get_method
30
31 @@by_code = {}
32 @@by_class = {}
33
34 # Creates a new mapping.
35 #
36 # ==== Arguments
37 #
38 # * code - the AMQP code for this type
39 # * name - the AMQP name for this type
40 # * klasses - native Ruby classes that are mapped to this AMQP type
41 # * getter - overrides the get method for the type
42 def initialize(code, name, klasses = nil, getter = nil)
43
44 @code = code
45 @name = name
46
47 @@by_code[code] = self
48
49 unless klasses.nil?
50 klasses.each do |klass|
51 raise "entry exists for #{klass}" if @@by_class.keys.include? klass
52 @@by_class[klass] = self unless klass.nil?
53 end
54 end
55
56 @put_method = (name + "=").intern
57
58 if getter.nil?
59 @get_method = name.intern
60 else
61 @get_method = getter.intern
62 end
63 end
64
65 def to_s; @name; end
66
67 def put(data, value)
68 data.__send__(@put_method, value)
69 end
70
71 def get(data)
72 data.__send__(@get_method)
73 end
74
75 def self.for_class(klass)
76 c = klass
77 c = c.superclass while c && (x = @@by_class[c]).nil?
78 raise TypeError, "#{klass} cannot be converted to AMQP" unless x
79 x
80 end
81
82 def self.for_code(code)
83 @@by_code[code]
84 end
85
86 # Convert x to a Mapping
87 def self.[](x)
88 case x
89 when Mapping then x
90 when Integer then @@by_code[x]
91 when Types::Type then @@by_code[x.code]
92 end
93 end
94
95 end
96
97 NULL = Mapping.new(Cproton::PN_NULL, "null", [NilClass])
98 BOOL = Mapping.new(Cproton::PN_BOOL, "bool", [TrueClass, FalseClass])
99 UBYTE = Mapping.new(Cproton::PN_UBYTE, "ubyte")
100 BYTE = Mapping.new(Cproton::PN_BYTE, "byte")
101 USHORT = Mapping.new(Cproton::PN_USHORT, "ushort")
102 SHORT = Mapping.new(Cproton::PN_SHORT, "short")
103 UINT = Mapping.new(Cproton::PN_UINT, "uint")
104 INT = Mapping.new(Cproton::PN_INT, "int")
105 CHAR = Mapping.new(Cproton::PN_CHAR, "char")
106 ULONG = Mapping.new(Cproton::PN_ULONG, "ulong")
107 LONG = Mapping.new(Cproton::PN_LONG, "long", [Integer])
108 TIMESTAMP = Mapping.new(Cproton::PN_TIMESTAMP, "timestamp", [Date, Time])
109 FLOAT = Mapping.new(Cproton::PN_FLOAT, "float")
110 DOUBLE = Mapping.new(Cproton::PN_DOUBLE, "double", [Float])
111 DECIMAL32 = Mapping.new(Cproton::PN_DECIMAL32, "decimal32")
112 DECIMAL64 = Mapping.new(Cproton::PN_DECIMAL64, "decimal64")
113 DECIMAL128 = Mapping.new(Cproton::PN_DECIMAL128, "decimal128")
114 UUID = Mapping.new(Cproton::PN_UUID, "uuid")
115 BINARY = Mapping.new(Cproton::PN_BINARY, "binary")
116 STRING = Mapping.new(Cproton::PN_STRING, "string", [::String,
117 Types::UTFString,
118 Types::BinaryString])
119 SYMBOL = Mapping.new(Cproton::PN_SYMBOL, "symbol", [::Symbol])
120 DESCRIBED = Mapping.new(Cproton::PN_DESCRIBED, "described", [Types::Described])
121 ARRAY = Mapping.new(Cproton::PN_ARRAY, "array", [Types::UniformArray])
122 LIST = Mapping.new(Cproton::PN_LIST, "list", [::Array])
123 MAP = Mapping.new(Cproton::PN_MAP, "map", [::Hash])
124
125 private
126
127 class << STRING
128 def put(data, value)
129 # if we have a symbol then convert it to a string
130 value = value.to_s if value.is_a?(Symbol)
131
132 isutf = false
133
134 if value.is_a?(Types::UTFString)
135 isutf = true
136 else
137 # For Ruby 1.8 we will just treat all strings as binary.
138 # For Ruby 1.9+ we can check the encoding first to see what it is
139 if RUBY_VERSION >= "1.9"
140 # If the string is ASCII-8BIT then treat is as binary. Otherwise,
141 # try to convert it to UTF-8 and, if successful, send as that.
142 if value.encoding != Encoding::ASCII_8BIT &&
143 value.encode(Encoding::UTF_8).valid_encoding?
144 isutf = true
145 end
146 end
147 end
148
149 data.string = value if isutf
150 data.binary = value if !isutf
151 end
152 end
153
154 class << MAP
155 def put(data, map, options = {})
156 data.put_map
157 data.enter
158 map.each_pair do |key, value|
159 if options[:keys] == :SYMBOL
160 SYMBOL.put(data, key)
161 else
162 data.object = key
163 end
164
165 if value.nil?
166 data.null
167 else
168 data.object = value
169 end
170 end
171 data.exit
172 end
173 end
174
175 class << DESCRIBED
176 def put(data, described)
177 data.put_described
178 data.enter
179 data.object = described.descriptor
180 data.object = described.value
181 data.exit
182 end
183 end
184 end
185 end