Class: ORiN3BinaryConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/grpc/orin3/provider/orin3_binary_converter.rb

Defined Under Namespace

Modules: DataType

Constant Summary collapse

UINT8_MIN =
0
UINT8_MAX =
255
UINT16_MIN =
0
UINT16_MAX =
65535
UINT32_MIN =
0
UINT32_MAX =
4_294_967_295
UINT64_MIN =
0
UINT64_MAX =
18_446_744_073_709_551_615
INT8_MIN =
-128
INT8_MAX =
127
INT16_MIN =
-32768
INT16_MAX =
32767
INT32_MIN =
-2_147_483_648
INT32_MAX =
2_147_483_647
INT64_MIN =
-9_223_372_036_854_775_808
INT64_MAX =
9_223_372_036_854_775_807
FLOAT_MIN =
-3.40282347E+38
FLOAT_MAX =
3.40282347E+38
DOUBLE_MIN =
-1.7976931348623157e+308
DOUBLE_MAX =
1.7976931348623157e+308

Class Method Summary collapse

Class Method Details

.deserialize(binary_data) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/grpc/orin3/provider/orin3_binary_converter.rb', line 124

def self.deserialize(binary_data)
  type_indicator = binary_data[0].ord

  case type_indicator
  when ORiN3BinaryConverter::DataType::Nil
    return nil
  when ORiN3BinaryConverter::DataType::Bool
    return binary_data[1].ord == 1
  when ORiN3BinaryConverter::DataType::UInt8
    return binary_data[1].unpack('C').first
  when ORiN3BinaryConverter::DataType::Int8
    return binary_data[1].unpack('c').first
  when ORiN3BinaryConverter::DataType::UInt16
    return binary_data[1..2].unpack('S').first
  when ORiN3BinaryConverter::DataType::Int16
    return binary_data[1..2].unpack('s').first
  when ORiN3BinaryConverter::DataType::UInt32
    return binary_data[1..4].unpack('L').first
  when ORiN3BinaryConverter::DataType::Int32
    return binary_data[1..4].unpack('l').first
  when ORiN3BinaryConverter::DataType::UInt64
    return binary_data[1..8].unpack('Q').first
  when ORiN3BinaryConverter::DataType::Int64
    return binary_data[1..8].unpack('q').first
  when ORiN3BinaryConverter::DataType::Float
    return binary_data[1..4].unpack('e').first
  when ORiN3BinaryConverter::DataType::Double
    return binary_data[1..8].unpack('E').first
  when ORiN3BinaryConverter::DataType::String
    length = binary_data[1..4].unpack('L').first
    string_data = binary_data[5, length].force_encoding('UTF-8')
    return string_data
  when ORiN3BinaryConverter::DataType::DateTime
    timestamp = binary_data[1..8].unpack('q<').first
    return Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
  when ORiN3BinaryConverter::DataType::BoolArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i].ord == 1
    end
    return result
  when ORiN3BinaryConverter::DataType::UInt8Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i].unpack('C').first
    end
    return result
  when ORiN3BinaryConverter::DataType::Int8Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i].unpack('c').first
    end
    return result
  when ORiN3BinaryConverter::DataType::UInt16Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 2, 2].unpack('S<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::Int16Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 2, 2].unpack('s<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::UInt32Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 4, 4].unpack('L<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::Int32Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 4, 4].unpack('l<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::UInt64Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 8, 8].unpack('Q<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::Int64Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 8, 8].unpack('q<').first
    end
    return result
  when ORiN3BinaryConverter::DataType::FloatArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 4, 4].unpack('e').first
    end
    return result
  when ORiN3BinaryConverter::DataType::DoubleArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      result[i] = binary_data[5 + i * 8, 8].unpack('E').first
    end
    return result
  when ORiN3BinaryConverter::DataType::StringArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    index = 5
    length.times do |i|
      if binary_data[index]
        string_length = binary_data[index + 1, 4].unpack('L').first
        result[i] = binary_data[index + 5, string_length].force_encoding('UTF-8')
        index += string_length + 5
      else
        result[i] = nil
        index += 5
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::DateTimeArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      timestamp = binary_data[5 + i * 8, 8].unpack('q<').first
      result[i] = Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableBoolArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + i
      if (binary_data[index].ord == 2)
        result[i] = nil
      else
        result[i] = binary_data[index].ord == 1
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableUInt8Array
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 2)
      if (binary_data[index].ord == 1)
        result[i] = binary_data[index + 1].unpack('C').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableInt8Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 2)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1].unpack('c').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableUInt16Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 3)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 2].unpack('S<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableInt16Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 3)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 2].unpack('s<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableUInt32Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 5)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 4].unpack('L<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableInt32Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 5)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 4].unpack('l<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableUInt64Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 9)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 8].unpack('Q<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableInt64Array
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 9)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 8].unpack('q<').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableFloatArray
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 5)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 4].unpack('e').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableDoubleArray
    length = binary_data[1..4].unpack('L<').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 9)
      if binary_data[index].ord == 1
        result[i] = binary_data[index + 1, 8].unpack('E').first
      end
    end
    return result
  when ORiN3BinaryConverter::DataType::NullableDateTimeArray
    length = binary_data[1..4].unpack('L').first
    result = Array.new(length, nil)
    length.times do |i|
      index = 5 + (i * 9)
      if binary_data[index].ord == 1
        timestamp = binary_data[index + 1, 8].unpack('q<').first
        result[i] = Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
      end
    end
    return result
  else
    raise ArgumentError, "Unsupported data type: #{type_indicator}. Binary data: #{binary_data.inspect}"
  end
end

.from_dictionary_to_binary(data) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'lib/grpc/orin3/provider/orin3_binary_converter.rb', line 387

def self.from_dictionary_to_binary(data)
  length_data = data.map do |key, value|
    5 + key.bytes.length + value.length
  end
  length = length_data.sum
  byte_array = Array.new(length + 4, 0)
  byte_array[0..3] = [data.keys.length].pack('l<').bytes
  index = 4
  data.each do |key, value|
    string_bytes = key.bytes
    byte_array[index] = ORiN3BinaryConverter::DataType::String
    byte_array[(index + 1)..(index + 4)] = [string_bytes.length].pack('L').bytes
    
    # キーのバイト配列を追加
    byte_array[(index + 5)..(index + 5 + string_bytes.length - 1)] = string_bytes
    
    index += 5 + string_bytes.length
    # 値がバイナリデータの場合、バイト配列として追加する
    if value.is_a?(String)
      byte_array[index..(index + value.bytes.length - 1)] = value.bytes
      index += value.bytes.length
    else
      raise ArgumentError, "Value is not a valid binary string."
    end
  end
  return byte_array.pack('C*')
end

.serialize(data, type = nil) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/grpc/orin3/provider/orin3_binary_converter.rb', line 83

def self.serialize(data, type = nil)
  if (!type.nil?)
    return self.serialize_core(data, type)
  else
    case data
    when nil
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::Nil)
    when Integer
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::Int64)
    when TrueClass, FalseClass
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::Bool)
    when String
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::String)
    when Time
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::DateTime)
    when Array
      if data.all? { |item| item.is_a?(TrueClass) || item.is_a?(FalseClass) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::BoolArray)
      elsif data.all? { |item| item.nil? || item.is_a?(TrueClass) || item.is_a?(FalseClass) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableBoolArray)
      elsif data.all? { |item| item.is_a?(Integer) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::Int64Array)
      elsif data.all? { |item| item.nil? || item.is_a?(Integer) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableInt64Array)
      elsif data.all? { |item| item.nil? || item.is_a?(String) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::StringArray)
      elsif data.all? { |item| item.is_a?(DateTime) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::DateTimeArray)
      elsif data.all? { |item| item.nil? || item.is_a?(DateTime) }
        return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableDateTimeArray)
      else
        raise ArgumentError, "Unsupported data type"
      end
    when Hash
      return self.serialize_core(data, ORiN3BinaryConverter::DataType::Dictionary)
    else
      raise ArgumentError, "Unsupported data type"
    end
  end
end