grpc-orin3-provider 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -1
- data/README.md +18 -21
- data/Rakefile +42 -0
- data/gen/Design.ORiN3.Common/V1/orin3_common_pb.rb +23 -0
- data/gen/Design.ORiN3.Common/V1/orin3_common_remote_engine_pb.rb +25 -0
- data/gen/Design.ORiN3.Provider/V1/orin3_object_type_pb.rb +23 -0
- data/gen/Design.ORiN3.Provider/V1/orin3_value_type_pb.rb +23 -0
- data/gen/Message.ORiN3.Provider/V1/orin3_base_object_pb.rb +39 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3baseobject_services_pb.rb → orin3_base_object_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_child_creator_pb.rb +39 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3childcreator_services_pb.rb → orin3_child_creator_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_child_pb.rb +28 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3child_services_pb.rb → orin3_child_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_common_type_pb.rb +79 -0
- data/gen/Message.ORiN3.Provider/V1/orin3_controller_creator_pb.rb +28 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3controllercreator_services_pb.rb → orin3_controller_creator_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_executable_pb.rb +28 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3executable_services_pb.rb → orin3_executable_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_file_pb.rb +41 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3file_services_pb.rb → orin3_file_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_job_pb.rb +32 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3job_services_pb.rb → orin3_job_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_parent_pb.rb +31 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3parent_services_pb.rb → orin3_parent_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_resource_opener_pb.rb +30 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3resourceopener_services_pb.rb → orin3_resource_opener_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_root_object_pb.rb +44 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3rootobject_services_pb.rb → orin3_root_object_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_stream_pb.rb +28 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3stream_services_pb.rb → orin3_stream_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.Provider/V1/orin3_variable_pb.rb +31 -0
- data/gen/Message.ORiN3.Provider/V1/{orin3variable_services_pb.rb → orin3_variable_services_pb.rb} +3 -3
- data/gen/Message.ORiN3.RemoteEngine/V1/orin3_remote_engine_pb.rb +40 -0
- data/gen/Message.ORiN3.RemoteEngine/V1/orin3_remote_engine_services_pb.rb +32 -0
- data/lib/grpc/client/orin3/message_client_error.rb +23 -0
- data/lib/grpc/client/orin3/provider/orin3_base_object.rb +145 -0
- data/lib/grpc/client/orin3/provider/orin3_child_creator.rb +104 -0
- data/lib/grpc/client/orin3/provider/orin3_controller.rb +68 -0
- data/lib/grpc/client/orin3/provider/orin3_file.rb +146 -0
- data/lib/grpc/client/orin3/provider/orin3_job.rb +86 -0
- data/lib/grpc/client/orin3/provider/orin3_module.rb +30 -0
- data/lib/grpc/client/orin3/provider/orin3_resource_opener.rb +50 -0
- data/lib/grpc/client/orin3/provider/orin3_root_object.rb +108 -0
- data/lib/grpc/client/orin3/provider/orin3_stream.rb +48 -0
- data/lib/grpc/client/orin3/provider/orin3_variable.rb +500 -0
- data/lib/grpc/client/orin3/remoteengine/orin3_remote_engine.rb +158 -0
- data/lib/grpc/orin3/provider/date_time_converter.rb +25 -0
- data/lib/grpc/orin3/provider/orin3_binary_converter.rb +686 -0
- data/lib/grpc/orin3/provider/version.rb +1 -1
- data/lib/grpc/orin3/provider.rb +160 -99
- data/testsemiauto/test_with_mock_provider.rb +908 -0
- data/testsemiauto/test_with_test_provider.rb +200 -0
- data/testsemiauto/testdata/file.txt +1 -0
- metadata +51 -28
- data/gen/Message.ORiN3.Common/V1/orin3common_pb.rb +0 -77
- data/gen/Message.ORiN3.Provider/V1/orin3baseobject_pb.rb +0 -38
- data/gen/Message.ORiN3.Provider/V1/orin3child_pb.rb +0 -28
- data/gen/Message.ORiN3.Provider/V1/orin3childcreator_pb.rb +0 -38
- data/gen/Message.ORiN3.Provider/V1/orin3controllercreator_pb.rb +0 -28
- data/gen/Message.ORiN3.Provider/V1/orin3executable_pb.rb +0 -28
- data/gen/Message.ORiN3.Provider/V1/orin3file_pb.rb +0 -41
- data/gen/Message.ORiN3.Provider/V1/orin3job_pb.rb +0 -32
- data/gen/Message.ORiN3.Provider/V1/orin3parent_pb.rb +0 -31
- data/gen/Message.ORiN3.Provider/V1/orin3resourceopener_pb.rb +0 -30
- data/gen/Message.ORiN3.Provider/V1/orin3rootobject_pb.rb +0 -44
- data/gen/Message.ORiN3.Provider/V1/orin3stream_pb.rb +0 -28
- data/gen/Message.ORiN3.Provider/V1/orin3variable_pb.rb +0 -30
@@ -0,0 +1,686 @@
|
|
1
|
+
class ORiN3BinaryConverter
|
2
|
+
module DataType
|
3
|
+
Nil = 0
|
4
|
+
ObjectArray = 1
|
5
|
+
Bool = 2
|
6
|
+
BoolArray = 3
|
7
|
+
NullableBool = 4
|
8
|
+
NullableBoolArray = 5
|
9
|
+
UInt8 = 6
|
10
|
+
UInt8Array = 7
|
11
|
+
NullableUInt8 = 8
|
12
|
+
NullableUInt8Array = 9
|
13
|
+
UInt16 = 10
|
14
|
+
UInt16Array = 11
|
15
|
+
NullableUInt16 = 12
|
16
|
+
NullableUInt16Array = 13
|
17
|
+
UInt32 = 14
|
18
|
+
UInt32Array = 15
|
19
|
+
NullableUInt32 = 16
|
20
|
+
NullableUInt32Array = 17
|
21
|
+
UInt64 = 18
|
22
|
+
UInt64Array = 19
|
23
|
+
NullableUInt64 = 20
|
24
|
+
NullableUInt64Array = 21
|
25
|
+
Int8 = 22
|
26
|
+
Int8Array = 23
|
27
|
+
NullableInt8 = 24
|
28
|
+
NullableInt8Array = 25
|
29
|
+
Int16 = 26
|
30
|
+
Int16Array = 27
|
31
|
+
NullableInt16 = 28
|
32
|
+
NullableInt16Array = 29
|
33
|
+
Int32 = 30
|
34
|
+
Int32Array = 31
|
35
|
+
NullableInt32 = 32
|
36
|
+
NullableInt32Array = 33
|
37
|
+
Int64 = 34
|
38
|
+
Int64Array = 35
|
39
|
+
NullableInt64 = 36
|
40
|
+
NullableInt64Array = 37
|
41
|
+
Float = 38
|
42
|
+
FloatArray = 39
|
43
|
+
NullableFloat = 40
|
44
|
+
NullableFloatArray = 41
|
45
|
+
Double = 42
|
46
|
+
DoubleArray = 43
|
47
|
+
NullableDouble = 44
|
48
|
+
NullableDoubleArray = 45
|
49
|
+
String = 46
|
50
|
+
StringArray = 47
|
51
|
+
DateTime = 48
|
52
|
+
DateTimeArray = 49
|
53
|
+
NullableDateTime = 50
|
54
|
+
NullableDateTimeArray = 51
|
55
|
+
Dictionary = 52
|
56
|
+
|
57
|
+
def self.values
|
58
|
+
constants.map { |const| const_get(const) }
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
UINT8_MIN = 0
|
63
|
+
UINT8_MAX = 255
|
64
|
+
UINT16_MIN = 0
|
65
|
+
UINT16_MAX = 65535
|
66
|
+
UINT32_MIN = 0
|
67
|
+
UINT32_MAX = 4_294_967_295
|
68
|
+
UINT64_MIN = 0
|
69
|
+
UINT64_MAX = 18_446_744_073_709_551_615
|
70
|
+
INT8_MIN = -128
|
71
|
+
INT8_MAX = 127
|
72
|
+
INT16_MIN = -32768
|
73
|
+
INT16_MAX = 32767
|
74
|
+
INT32_MIN = -2_147_483_648
|
75
|
+
INT32_MAX = 2_147_483_647
|
76
|
+
INT64_MIN = -9_223_372_036_854_775_808
|
77
|
+
INT64_MAX = 9_223_372_036_854_775_807
|
78
|
+
FLOAT_MIN = -3.40282347E+38
|
79
|
+
FLOAT_MAX = 3.40282347E+38
|
80
|
+
DOUBLE_MIN = -1.7976931348623157e+308
|
81
|
+
DOUBLE_MAX = 1.7976931348623157e+308
|
82
|
+
|
83
|
+
def self.serialize(data, type = nil)
|
84
|
+
if (!type.nil?)
|
85
|
+
return self.serialize_core(data, type)
|
86
|
+
else
|
87
|
+
case data
|
88
|
+
when nil
|
89
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::Nil)
|
90
|
+
when Integer
|
91
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::Int64)
|
92
|
+
when TrueClass, FalseClass
|
93
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::Bool)
|
94
|
+
when String
|
95
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::String)
|
96
|
+
when Time
|
97
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::DateTime)
|
98
|
+
when Array
|
99
|
+
if data.all? { |item| item.is_a?(TrueClass) || item.is_a?(FalseClass) }
|
100
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::BoolArray)
|
101
|
+
elsif data.all? { |item| item.nil? || item.is_a?(TrueClass) || item.is_a?(FalseClass) }
|
102
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableBoolArray)
|
103
|
+
elsif data.all? { |item| item.is_a?(Integer) }
|
104
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::Int64Array)
|
105
|
+
elsif data.all? { |item| item.nil? || item.is_a?(Integer) }
|
106
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableInt64Array)
|
107
|
+
elsif data.all? { |item| item.nil? || item.is_a?(String) }
|
108
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::StringArray)
|
109
|
+
elsif data.all? { |item| item.is_a?(DateTime) }
|
110
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::DateTimeArray)
|
111
|
+
elsif data.all? { |item| item.nil? || item.is_a?(DateTime) }
|
112
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::NullableDateTimeArray)
|
113
|
+
else
|
114
|
+
raise ArgumentError, "Unsupported data type"
|
115
|
+
end
|
116
|
+
when Hash
|
117
|
+
return self.serialize_core(data, ORiN3BinaryConverter::DataType::Dictionary)
|
118
|
+
else
|
119
|
+
raise ArgumentError, "Unsupported data type"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
def self.deserialize(binary_data)
|
125
|
+
type_indicator = binary_data[0].ord
|
126
|
+
|
127
|
+
case type_indicator
|
128
|
+
when ORiN3BinaryConverter::DataType::Nil
|
129
|
+
return nil
|
130
|
+
when ORiN3BinaryConverter::DataType::Bool
|
131
|
+
return binary_data[1].ord == 1
|
132
|
+
when ORiN3BinaryConverter::DataType::UInt8
|
133
|
+
return binary_data[1].unpack('C').first
|
134
|
+
when ORiN3BinaryConverter::DataType::Int8
|
135
|
+
return binary_data[1].unpack('c').first
|
136
|
+
when ORiN3BinaryConverter::DataType::UInt16
|
137
|
+
return binary_data[1..2].unpack('S').first
|
138
|
+
when ORiN3BinaryConverter::DataType::Int16
|
139
|
+
return binary_data[1..2].unpack('s').first
|
140
|
+
when ORiN3BinaryConverter::DataType::UInt32
|
141
|
+
return binary_data[1..4].unpack('L').first
|
142
|
+
when ORiN3BinaryConverter::DataType::Int32
|
143
|
+
return binary_data[1..4].unpack('l').first
|
144
|
+
when ORiN3BinaryConverter::DataType::UInt64
|
145
|
+
return binary_data[1..8].unpack('Q').first
|
146
|
+
when ORiN3BinaryConverter::DataType::Int64
|
147
|
+
return binary_data[1..8].unpack('q').first
|
148
|
+
when ORiN3BinaryConverter::DataType::Float
|
149
|
+
return binary_data[1..4].unpack('e').first
|
150
|
+
when ORiN3BinaryConverter::DataType::Double
|
151
|
+
return binary_data[1..8].unpack('E').first
|
152
|
+
when ORiN3BinaryConverter::DataType::String
|
153
|
+
length = binary_data[1..4].unpack('L').first
|
154
|
+
string_data = binary_data[5, length].force_encoding('UTF-8')
|
155
|
+
return string_data
|
156
|
+
when ORiN3BinaryConverter::DataType::DateTime
|
157
|
+
timestamp = binary_data[1..8].unpack('q<').first
|
158
|
+
return Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
|
159
|
+
when ORiN3BinaryConverter::DataType::BoolArray
|
160
|
+
length = binary_data[1..4].unpack('L').first
|
161
|
+
result = Array.new(length, nil)
|
162
|
+
length.times do |i|
|
163
|
+
result[i] = binary_data[5 + i].ord == 1
|
164
|
+
end
|
165
|
+
return result
|
166
|
+
when ORiN3BinaryConverter::DataType::UInt8Array
|
167
|
+
length = binary_data[1..4].unpack('L').first
|
168
|
+
result = Array.new(length, nil)
|
169
|
+
length.times do |i|
|
170
|
+
result[i] = binary_data[5 + i].unpack('C').first
|
171
|
+
end
|
172
|
+
return result
|
173
|
+
when ORiN3BinaryConverter::DataType::Int8Array
|
174
|
+
length = binary_data[1..4].unpack('L').first
|
175
|
+
result = Array.new(length, nil)
|
176
|
+
length.times do |i|
|
177
|
+
result[i] = binary_data[5 + i].unpack('c').first
|
178
|
+
end
|
179
|
+
return result
|
180
|
+
when ORiN3BinaryConverter::DataType::UInt16Array
|
181
|
+
length = binary_data[1..4].unpack('L').first
|
182
|
+
result = Array.new(length, nil)
|
183
|
+
length.times do |i|
|
184
|
+
result[i] = binary_data[5 + i * 2, 2].unpack('S<').first
|
185
|
+
end
|
186
|
+
return result
|
187
|
+
when ORiN3BinaryConverter::DataType::Int16Array
|
188
|
+
length = binary_data[1..4].unpack('L').first
|
189
|
+
result = Array.new(length, nil)
|
190
|
+
length.times do |i|
|
191
|
+
result[i] = binary_data[5 + i * 2, 2].unpack('s<').first
|
192
|
+
end
|
193
|
+
return result
|
194
|
+
when ORiN3BinaryConverter::DataType::UInt32Array
|
195
|
+
length = binary_data[1..4].unpack('L').first
|
196
|
+
result = Array.new(length, nil)
|
197
|
+
length.times do |i|
|
198
|
+
result[i] = binary_data[5 + i * 4, 4].unpack('L<').first
|
199
|
+
end
|
200
|
+
return result
|
201
|
+
when ORiN3BinaryConverter::DataType::Int32Array
|
202
|
+
length = binary_data[1..4].unpack('L').first
|
203
|
+
result = Array.new(length, nil)
|
204
|
+
length.times do |i|
|
205
|
+
result[i] = binary_data[5 + i * 4, 4].unpack('l<').first
|
206
|
+
end
|
207
|
+
return result
|
208
|
+
when ORiN3BinaryConverter::DataType::UInt64Array
|
209
|
+
length = binary_data[1..4].unpack('L').first
|
210
|
+
result = Array.new(length, nil)
|
211
|
+
length.times do |i|
|
212
|
+
result[i] = binary_data[5 + i * 8, 8].unpack('Q<').first
|
213
|
+
end
|
214
|
+
return result
|
215
|
+
when ORiN3BinaryConverter::DataType::Int64Array
|
216
|
+
length = binary_data[1..4].unpack('L').first
|
217
|
+
result = Array.new(length, nil)
|
218
|
+
length.times do |i|
|
219
|
+
result[i] = binary_data[5 + i * 8, 8].unpack('q<').first
|
220
|
+
end
|
221
|
+
return result
|
222
|
+
when ORiN3BinaryConverter::DataType::FloatArray
|
223
|
+
length = binary_data[1..4].unpack('L').first
|
224
|
+
result = Array.new(length, nil)
|
225
|
+
length.times do |i|
|
226
|
+
result[i] = binary_data[5 + i * 4, 4].unpack('e').first
|
227
|
+
end
|
228
|
+
return result
|
229
|
+
when ORiN3BinaryConverter::DataType::DoubleArray
|
230
|
+
length = binary_data[1..4].unpack('L').first
|
231
|
+
result = Array.new(length, nil)
|
232
|
+
length.times do |i|
|
233
|
+
result[i] = binary_data[5 + i * 8, 8].unpack('E').first
|
234
|
+
end
|
235
|
+
return result
|
236
|
+
when ORiN3BinaryConverter::DataType::StringArray
|
237
|
+
length = binary_data[1..4].unpack('L').first
|
238
|
+
result = Array.new(length, nil)
|
239
|
+
index = 5
|
240
|
+
length.times do |i|
|
241
|
+
if binary_data[index]
|
242
|
+
string_length = binary_data[index + 1, 4].unpack('L').first
|
243
|
+
result[i] = binary_data[index + 5, string_length].force_encoding('UTF-8')
|
244
|
+
index += string_length + 5
|
245
|
+
else
|
246
|
+
result[i] = nil
|
247
|
+
index += 5
|
248
|
+
end
|
249
|
+
end
|
250
|
+
return result
|
251
|
+
when ORiN3BinaryConverter::DataType::DateTimeArray
|
252
|
+
length = binary_data[1..4].unpack('L').first
|
253
|
+
result = Array.new(length, nil)
|
254
|
+
length.times do |i|
|
255
|
+
timestamp = binary_data[5 + i * 8, 8].unpack('q<').first
|
256
|
+
result[i] = Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
|
257
|
+
end
|
258
|
+
return result
|
259
|
+
when ORiN3BinaryConverter::DataType::NullableBoolArray
|
260
|
+
length = binary_data[1..4].unpack('L').first
|
261
|
+
result = Array.new(length, nil)
|
262
|
+
length.times do |i|
|
263
|
+
index = 5 + i
|
264
|
+
if (binary_data[index].ord == 2)
|
265
|
+
result[i] = nil
|
266
|
+
else
|
267
|
+
result[i] = binary_data[index].ord == 1
|
268
|
+
end
|
269
|
+
end
|
270
|
+
return result
|
271
|
+
when ORiN3BinaryConverter::DataType::NullableUInt8Array
|
272
|
+
length = binary_data[1..4].unpack('L').first
|
273
|
+
result = Array.new(length, nil)
|
274
|
+
length.times do |i|
|
275
|
+
index = 5 + (i * 2)
|
276
|
+
if (binary_data[index].ord == 1)
|
277
|
+
result[i] = binary_data[index + 1].unpack('C').first
|
278
|
+
end
|
279
|
+
end
|
280
|
+
return result
|
281
|
+
when ORiN3BinaryConverter::DataType::NullableInt8Array
|
282
|
+
length = binary_data[1..4].unpack('L<').first
|
283
|
+
result = Array.new(length, nil)
|
284
|
+
length.times do |i|
|
285
|
+
index = 5 + (i * 2)
|
286
|
+
if binary_data[index].ord == 1
|
287
|
+
result[i] = binary_data[index + 1].unpack('c').first
|
288
|
+
end
|
289
|
+
end
|
290
|
+
return result
|
291
|
+
when ORiN3BinaryConverter::DataType::NullableUInt16Array
|
292
|
+
length = binary_data[1..4].unpack('L<').first
|
293
|
+
result = Array.new(length, nil)
|
294
|
+
length.times do |i|
|
295
|
+
index = 5 + (i * 3)
|
296
|
+
if binary_data[index].ord == 1
|
297
|
+
result[i] = binary_data[index + 1, 2].unpack('S<').first
|
298
|
+
end
|
299
|
+
end
|
300
|
+
return result
|
301
|
+
when ORiN3BinaryConverter::DataType::NullableInt16Array
|
302
|
+
length = binary_data[1..4].unpack('L<').first
|
303
|
+
result = Array.new(length, nil)
|
304
|
+
length.times do |i|
|
305
|
+
index = 5 + (i * 3)
|
306
|
+
if binary_data[index].ord == 1
|
307
|
+
result[i] = binary_data[index + 1, 2].unpack('s<').first
|
308
|
+
end
|
309
|
+
end
|
310
|
+
return result
|
311
|
+
when ORiN3BinaryConverter::DataType::NullableUInt32Array
|
312
|
+
length = binary_data[1..4].unpack('L<').first
|
313
|
+
result = Array.new(length, nil)
|
314
|
+
length.times do |i|
|
315
|
+
index = 5 + (i * 5)
|
316
|
+
if binary_data[index].ord == 1
|
317
|
+
result[i] = binary_data[index + 1, 4].unpack('L<').first
|
318
|
+
end
|
319
|
+
end
|
320
|
+
return result
|
321
|
+
when ORiN3BinaryConverter::DataType::NullableInt32Array
|
322
|
+
length = binary_data[1..4].unpack('L<').first
|
323
|
+
result = Array.new(length, nil)
|
324
|
+
length.times do |i|
|
325
|
+
index = 5 + (i * 5)
|
326
|
+
if binary_data[index].ord == 1
|
327
|
+
result[i] = binary_data[index + 1, 4].unpack('l<').first
|
328
|
+
end
|
329
|
+
end
|
330
|
+
return result
|
331
|
+
when ORiN3BinaryConverter::DataType::NullableUInt64Array
|
332
|
+
length = binary_data[1..4].unpack('L<').first
|
333
|
+
result = Array.new(length, nil)
|
334
|
+
length.times do |i|
|
335
|
+
index = 5 + (i * 9)
|
336
|
+
if binary_data[index].ord == 1
|
337
|
+
result[i] = binary_data[index + 1, 8].unpack('Q<').first
|
338
|
+
end
|
339
|
+
end
|
340
|
+
return result
|
341
|
+
when ORiN3BinaryConverter::DataType::NullableInt64Array
|
342
|
+
length = binary_data[1..4].unpack('L<').first
|
343
|
+
result = Array.new(length, nil)
|
344
|
+
length.times do |i|
|
345
|
+
index = 5 + (i * 9)
|
346
|
+
if binary_data[index].ord == 1
|
347
|
+
result[i] = binary_data[index + 1, 8].unpack('q<').first
|
348
|
+
end
|
349
|
+
end
|
350
|
+
return result
|
351
|
+
when ORiN3BinaryConverter::DataType::NullableFloatArray
|
352
|
+
length = binary_data[1..4].unpack('L<').first
|
353
|
+
result = Array.new(length, nil)
|
354
|
+
length.times do |i|
|
355
|
+
index = 5 + (i * 5)
|
356
|
+
if binary_data[index].ord == 1
|
357
|
+
result[i] = binary_data[index + 1, 4].unpack('e').first
|
358
|
+
end
|
359
|
+
end
|
360
|
+
return result
|
361
|
+
when ORiN3BinaryConverter::DataType::NullableDoubleArray
|
362
|
+
length = binary_data[1..4].unpack('L<').first
|
363
|
+
result = Array.new(length, nil)
|
364
|
+
length.times do |i|
|
365
|
+
index = 5 + (i * 9)
|
366
|
+
if binary_data[index].ord == 1
|
367
|
+
result[i] = binary_data[index + 1, 8].unpack('E').first
|
368
|
+
end
|
369
|
+
end
|
370
|
+
return result
|
371
|
+
when ORiN3BinaryConverter::DataType::NullableDateTimeArray
|
372
|
+
length = binary_data[1..4].unpack('L').first
|
373
|
+
result = Array.new(length, nil)
|
374
|
+
length.times do |i|
|
375
|
+
index = 5 + (i * 9)
|
376
|
+
if binary_data[index].ord == 1
|
377
|
+
timestamp = binary_data[index + 1, 8].unpack('q<').first
|
378
|
+
result[i] = Grpc::ORiN3::Provider::DateTimeConverter::from_int64(timestamp)
|
379
|
+
end
|
380
|
+
end
|
381
|
+
return result
|
382
|
+
else
|
383
|
+
raise ArgumentError, "Unsupported data type: #{type_indicator}. Binary data: #{binary_data.inspect}"
|
384
|
+
end
|
385
|
+
end
|
386
|
+
|
387
|
+
def self.from_dictionary_to_binary(data)
|
388
|
+
length_data = data.map do |key, value|
|
389
|
+
5 + key.bytes.length + value.length
|
390
|
+
end
|
391
|
+
length = length_data.sum
|
392
|
+
byte_array = Array.new(length + 4, 0)
|
393
|
+
byte_array[0..3] = [data.keys.length].pack('l<').bytes
|
394
|
+
index = 4
|
395
|
+
data.each do |key, value|
|
396
|
+
string_bytes = key.bytes
|
397
|
+
byte_array[index] = ORiN3BinaryConverter::DataType::String
|
398
|
+
byte_array[(index + 1)..(index + 4)] = [string_bytes.length].pack('L').bytes
|
399
|
+
|
400
|
+
# キーのバイト配列を追加
|
401
|
+
byte_array[(index + 5)..(index + 5 + string_bytes.length - 1)] = string_bytes
|
402
|
+
|
403
|
+
index += 5 + string_bytes.length
|
404
|
+
# 値がバイナリデータの場合、バイト配列として追加する
|
405
|
+
if value.is_a?(String)
|
406
|
+
byte_array[index..(index + value.bytes.length - 1)] = value.bytes
|
407
|
+
index += value.bytes.length
|
408
|
+
else
|
409
|
+
raise ArgumentError, "Value is not a valid binary string."
|
410
|
+
end
|
411
|
+
end
|
412
|
+
return byte_array.pack('C*')
|
413
|
+
end
|
414
|
+
|
415
|
+
private
|
416
|
+
def self.serialize_core(data, type)
|
417
|
+
case type
|
418
|
+
when ORiN3BinaryConverter::DataType::Nil
|
419
|
+
byte_array = Array.new(1, 0)
|
420
|
+
byte_array[0] = ORiN3BinaryConverter::DataType::Nil
|
421
|
+
return byte_array.pack('C*')
|
422
|
+
|
423
|
+
when ORiN3BinaryConverter::DataType::Bool
|
424
|
+
serialize_not_null_not_array_no_range([ TrueClass, FalseClass ], data, type, 1) { data ? 1 : 0 }
|
425
|
+
when ORiN3BinaryConverter::DataType::UInt8
|
426
|
+
serialize_not_null_not_array([ Integer ], data, type, 1, UINT8_MIN, UINT8_MAX, "UInt8") { data }
|
427
|
+
when ORiN3BinaryConverter::DataType::UInt16
|
428
|
+
serialize_not_null_not_array([ Integer ], data, type, 2, UINT16_MIN, UINT16_MAX, "UInt16") { [data].pack('S<').bytes }
|
429
|
+
when ORiN3BinaryConverter::DataType::UInt32
|
430
|
+
serialize_not_null_not_array([ Integer ], data, type, 4, UINT32_MIN, UINT32_MAX, "UInt32") { [data].pack('L<').bytes }
|
431
|
+
when ORiN3BinaryConverter::DataType::UInt64
|
432
|
+
serialize_not_null_not_array([ Integer ], data, type, 8, UINT64_MIN, UINT64_MAX, "UInt64") { [data].pack('Q<').bytes }
|
433
|
+
when ORiN3BinaryConverter::DataType::Int8
|
434
|
+
serialize_not_null_not_array([ Integer ], data, type, 1, INT8_MIN, INT8_MAX, "Int8") { data }
|
435
|
+
when ORiN3BinaryConverter::DataType::Int16
|
436
|
+
serialize_not_null_not_array([ Integer ], data, type, 2, INT16_MIN, INT16_MAX, "Int16") { [data].pack('s<').bytes }
|
437
|
+
when ORiN3BinaryConverter::DataType::Int32
|
438
|
+
serialize_not_null_not_array([ Integer ], data, type, 4, INT32_MIN, INT32_MAX, "Int32") { [data].pack('l<').bytes }
|
439
|
+
when ORiN3BinaryConverter::DataType::Int64
|
440
|
+
serialize_not_null_not_array([ Integer ], data, type, 8, INT64_MIN, INT64_MAX, "Int64") { [data].pack('q<').bytes }
|
441
|
+
when ORiN3BinaryConverter::DataType::Float
|
442
|
+
serialize_not_null_not_array_no_range([ Float ], data, type, 4) { [data].pack('e').bytes }
|
443
|
+
when ORiN3BinaryConverter::DataType::Double
|
444
|
+
serialize_not_null_not_array_no_range([ Float ], data, type, 8) { [data].pack('E').bytes }
|
445
|
+
when ORiN3BinaryConverter::DataType::DateTime
|
446
|
+
serialize_not_null_not_array_no_range([ Time ], data, type, 8) { [Grpc::ORiN3::Provider::DateTimeConverter.to_int64(data)].pack('q<').bytes }
|
447
|
+
when ORiN3BinaryConverter::DataType::String
|
448
|
+
if !data.nil? && !data.is_a?(String)
|
449
|
+
raise ArgumentError, "Value is not #{String}."
|
450
|
+
end
|
451
|
+
string_bytes = data.bytes
|
452
|
+
length = string_bytes.length
|
453
|
+
byte_array = Array.new(1 + 4 + length, 0)
|
454
|
+
byte_array[0] = ORiN3BinaryConverter::DataType::String
|
455
|
+
byte_array[1..4] = [length].pack('L').bytes
|
456
|
+
byte_array[5..] = string_bytes
|
457
|
+
return byte_array.pack('C*')
|
458
|
+
|
459
|
+
when ORiN3BinaryConverter::DataType::BoolArray
|
460
|
+
serialize_not_null_array_no_range([ TrueClass, FalseClass ], data, type, 1) { |item| item ? 1 : 0 }
|
461
|
+
when ORiN3BinaryConverter::DataType::UInt8Array
|
462
|
+
serialize_not_null_array([ Integer ], data, type, 1, UINT8_MIN, UINT8_MAX, "UInt8") { |item| item }
|
463
|
+
when ORiN3BinaryConverter::DataType::UInt16Array
|
464
|
+
serialize_not_null_array([ Integer ], data, type, 2, UINT16_MIN, UINT16_MAX, "UInt16") { |item| [item].pack('S<').bytes }
|
465
|
+
when ORiN3BinaryConverter::DataType::UInt32Array
|
466
|
+
serialize_not_null_array([ Integer ], data, type, 4, UINT32_MIN, UINT32_MAX, "UInt32") { |item| [item].pack('L<').bytes }
|
467
|
+
when ORiN3BinaryConverter::DataType::UInt64Array
|
468
|
+
serialize_not_null_array([ Integer ], data, type, 8, UINT64_MIN, UINT64_MAX, "UInt64") { |item| [item].pack('Q<').bytes }
|
469
|
+
when ORiN3BinaryConverter::DataType::Int8Array
|
470
|
+
serialize_not_null_array([ Integer ], data, type, 1, INT8_MIN, INT8_MAX, "Int8") { |item| item }
|
471
|
+
when ORiN3BinaryConverter::DataType::Int16Array
|
472
|
+
serialize_not_null_array([ Integer ], data, type, 2, INT16_MIN, INT16_MAX, "Int16") { |item| [item].pack('s<').bytes }
|
473
|
+
when ORiN3BinaryConverter::DataType::Int32Array
|
474
|
+
serialize_not_null_array([ Integer ], data, type, 4, INT32_MIN, INT32_MAX, "Int32") { |item| [item].pack('l<').bytes }
|
475
|
+
when ORiN3BinaryConverter::DataType::Int64Array
|
476
|
+
serialize_not_null_array([ Integer ], data, type, 8, INT64_MIN, INT64_MAX, "Int64") { |item| [item].pack('q<').bytes }
|
477
|
+
when ORiN3BinaryConverter::DataType::FloatArray
|
478
|
+
serialize_not_null_array_no_range([ Float ], data, type, 4) { |item| [item].pack('e').bytes }
|
479
|
+
when ORiN3BinaryConverter::DataType::DoubleArray
|
480
|
+
serialize_not_null_array_no_range([ Float ], data, type, 8) { |item| [item].pack('E').bytes }
|
481
|
+
when ORiN3BinaryConverter::DataType::DateTimeArray
|
482
|
+
serialize_not_null_array_no_range([ Time ], data, type, 8) { |item| [Grpc::ORiN3::Provider::DateTimeConverter.to_int64(item)].pack('q<').bytes }
|
483
|
+
when ORiN3BinaryConverter::DataType::StringArray
|
484
|
+
if data.nil?
|
485
|
+
raise ArgumentError, "Value is nil."
|
486
|
+
elsif !data.is_a?(Array)
|
487
|
+
raise ArgumentError, "Value is not Array."
|
488
|
+
end
|
489
|
+
data.each do |item|
|
490
|
+
if !item.nil? && !item.is_a?(String)
|
491
|
+
raise ArgumentError, "Value is not #{String}."
|
492
|
+
end
|
493
|
+
end
|
494
|
+
total_length = data.sum { |item| item.nil? ? 0 : item.bytes.length } + data.length * 5 + 5
|
495
|
+
byte_array = Array.new(total_length, 0)
|
496
|
+
byte_array[0] = ORiN3BinaryConverter::DataType::StringArray
|
497
|
+
byte_array[1..4] = [data.length].pack('L<').bytes
|
498
|
+
offset = 5
|
499
|
+
data.each do |item|
|
500
|
+
if item.nil?
|
501
|
+
offset += 5
|
502
|
+
else
|
503
|
+
string_bytes = item.bytes
|
504
|
+
length = string_bytes.length
|
505
|
+
byte_array[offset, 1] = 1
|
506
|
+
offset += 1
|
507
|
+
byte_array[offset, 4] = [length].pack('L<').bytes
|
508
|
+
offset += 4
|
509
|
+
byte_array[offset, length] = string_bytes
|
510
|
+
offset += length
|
511
|
+
end
|
512
|
+
end
|
513
|
+
return byte_array.pack('C*')
|
514
|
+
|
515
|
+
when ORiN3BinaryConverter::DataType::NullableBoolArray
|
516
|
+
if data.nil?
|
517
|
+
raise ArgumentError, "Value is nil."
|
518
|
+
elsif !data.is_a?(Array)
|
519
|
+
raise ArgumentError, "Value is not Array."
|
520
|
+
end
|
521
|
+
data.each do |item|
|
522
|
+
if !item.nil? && (!item.is_a?(TrueClass) && !item.is_a?(FalseClass))
|
523
|
+
raise ArgumentError, "Value is not #{String}."
|
524
|
+
end
|
525
|
+
end
|
526
|
+
byte_array = Array.new(1 + 4 + data.length, 0)
|
527
|
+
byte_array[0] = ORiN3BinaryConverter::DataType::NullableBoolArray
|
528
|
+
byte_array[1..4] = [data.length].pack('l<').bytes
|
529
|
+
data.each_with_index do |item, index|
|
530
|
+
if (data[index].nil?)
|
531
|
+
byte_array[5 + index] = 2
|
532
|
+
else
|
533
|
+
byte_array[5 + index] = data[index] ? 1 : 0
|
534
|
+
end
|
535
|
+
end
|
536
|
+
return byte_array.pack('C*')
|
537
|
+
|
538
|
+
when ORiN3BinaryConverter::DataType::NullableUInt8Array
|
539
|
+
serialize_array([ Integer ], data, type, 1, UINT8_MIN, UINT8_MAX, "UInt8") { |item| item }
|
540
|
+
when ORiN3BinaryConverter::DataType::NullableUInt16Array
|
541
|
+
serialize_array([ Integer ], data, type, 2, UINT16_MIN, UINT16_MAX, "UInt16") { |item| [item].pack('S<').bytes }
|
542
|
+
when ORiN3BinaryConverter::DataType::NullableUInt32Array
|
543
|
+
serialize_array([ Integer ], data, type, 4, UINT32_MIN, UINT32_MAX, "UInt32") { |item| [item].pack('L<').bytes }
|
544
|
+
when ORiN3BinaryConverter::DataType::NullableUInt64Array
|
545
|
+
serialize_array([ Integer ], data, type, 8, UINT64_MIN, UINT64_MAX, "UInt64") { |item| [item].pack('Q<').bytes }
|
546
|
+
when ORiN3BinaryConverter::DataType::NullableInt8Array
|
547
|
+
serialize_array([ Integer ], data, type, 1, INT8_MIN, INT8_MAX, "Int8") { |item| item }
|
548
|
+
when ORiN3BinaryConverter::DataType::NullableInt16Array
|
549
|
+
serialize_array([ Integer ], data, type, 2, INT16_MIN, INT16_MAX, "Int16") { |item| [item].pack('s<').bytes }
|
550
|
+
when ORiN3BinaryConverter::DataType::NullableInt32Array
|
551
|
+
serialize_array([ Integer ], data, type, 4, INT32_MIN, INT32_MAX, "Int32") { |item| [item].pack('l<').bytes }
|
552
|
+
when ORiN3BinaryConverter::DataType::NullableInt64Array
|
553
|
+
serialize_array([ Integer ], data, type, 8, INT64_MIN, INT64_MAX, "Int64") { |item| [item].pack('q<').bytes }
|
554
|
+
when ORiN3BinaryConverter::DataType::NullableFloatArray
|
555
|
+
serialize_array_no_range([ Float ], data, type, 4) { |item| [item].pack('e').bytes }
|
556
|
+
when ORiN3BinaryConverter::DataType::NullableDoubleArray
|
557
|
+
serialize_array_no_range([ Float ], data, type, 8) { |item| [item].pack('E').bytes }
|
558
|
+
when ORiN3BinaryConverter::DataType::NullableDateTimeArray
|
559
|
+
serialize_array_no_range([ Time ], data, type, 8) { |item| [Grpc::ORiN3::Provider::DateTimeConverter.to_int64(item)].pack('q<').bytes }
|
560
|
+
# when ORiN3BinaryConverter::DataType::Dictionary
|
561
|
+
# if data.nil?
|
562
|
+
# raise ArgumentError, "Value is nil."
|
563
|
+
# elsif !data.is_a?(Hash)
|
564
|
+
# raise ArgumentError, "Value is not Hash."
|
565
|
+
# end
|
566
|
+
# # data.each do |item|
|
567
|
+
# # if !item.nil? && (!item.is_a?(TrueClass) && !item.is_a?(FalseClass))
|
568
|
+
# # raise ArgumentError, "Value is not #{String}."
|
569
|
+
# # end
|
570
|
+
# # end
|
571
|
+
# length_data = data.map do |key, value|
|
572
|
+
# 5 + key.bytes.length + value.length
|
573
|
+
# end
|
574
|
+
# length = length_data.sum
|
575
|
+
# byte_array = Array.new(length + 9, 0)
|
576
|
+
# byte_array[0] = ORiN3BinaryConverter::DataType::Dictionary
|
577
|
+
# byte_array[1..4] = [length].pack('l<').bytes
|
578
|
+
# byte_array[5..8] = [data.keys.length].pack('l<').bytes
|
579
|
+
# index = 9
|
580
|
+
# data.each do |key, value|
|
581
|
+
# string_bytes = key.bytes
|
582
|
+
# byte_array[index] = ORiN3BinaryConverter::DataType::String
|
583
|
+
# byte_array[(index + 1)..(index + 4)] = [string_bytes.length].pack('L').bytes
|
584
|
+
|
585
|
+
# # キーのバイト配列を追加
|
586
|
+
# byte_array[(index + 5)..(index + 5 + string_bytes.length - 1)] = string_bytes
|
587
|
+
|
588
|
+
# index += 5 + string_bytes.length
|
589
|
+
|
590
|
+
# # 値がバイナリデータの場合、バイト配列として追加する
|
591
|
+
# if value.is_a?(String)
|
592
|
+
# byte_array[index..(index + value.bytes.length - 1)] = value.bytes
|
593
|
+
# index += value.bytes.length
|
594
|
+
# else
|
595
|
+
# raise ArgumentError, "Value is not a valid binary string."
|
596
|
+
# end
|
597
|
+
# end
|
598
|
+
# puts "byte_array=#{byte_array}"
|
599
|
+
# return byte_array.pack('C*')
|
600
|
+
else
|
601
|
+
raise ArgumentError, "Unsupported data type"
|
602
|
+
end
|
603
|
+
end
|
604
|
+
|
605
|
+
def self.serialize_not_null_not_array(ruby_type, data, type, data_size, min, max, type_name, &packer)
|
606
|
+
if !data.nil? && !ruby_type.any? { |element| data.is_a?(element) }
|
607
|
+
raise ArgumentError, "Value is not #{ruby_type}."
|
608
|
+
elsif !data.nil? && (data < min || max < data)
|
609
|
+
raise ArgumentError, "Value #{data} is out of range for #{type_name}. It must be between #{min} and #{max}."
|
610
|
+
end
|
611
|
+
serialize_not_null_not_array_no_range(ruby_type, data, type, data_size) { yield packer }
|
612
|
+
end
|
613
|
+
|
614
|
+
def self.serialize_not_null_not_array_no_range(ruby_type, data, type, data_size, &packer)
|
615
|
+
if data.nil?
|
616
|
+
raise ArgumentError, "Value is nil."
|
617
|
+
elsif !ruby_type.any? { |element| data.is_a?(element) }
|
618
|
+
raise ArgumentError, "Value is not #{ruby_type}."
|
619
|
+
end
|
620
|
+
byte_array = Array.new(data_size + 1, 0)
|
621
|
+
byte_array[0] = type
|
622
|
+
byte_array[1..data_size] = yield packer
|
623
|
+
return byte_array.pack('C*')
|
624
|
+
end
|
625
|
+
|
626
|
+
def self.serialize_not_null_array(ruby_type, data, type, data_size, min, max, type_name, &packer)
|
627
|
+
serialize_not_null_array_core(ruby_type, data, type, data_size, true, min, max, type_name) { |item| yield item }
|
628
|
+
end
|
629
|
+
|
630
|
+
def self.serialize_not_null_array_no_range(ruby_type, data, type, data_size, &packer)
|
631
|
+
serialize_not_null_array_core(ruby_type, data, type, data_size, false, nil, nil, nil) { |item| yield item }
|
632
|
+
end
|
633
|
+
|
634
|
+
def self.serialize_not_null_array_core(ruby_type, data, type, data_size, range_check_flg, min, max, type_name, &packer)
|
635
|
+
if data.nil?
|
636
|
+
raise ArgumentError, "Value is nil."
|
637
|
+
elsif !data.is_a?(Array)
|
638
|
+
raise ArgumentError, "Value is not Array."
|
639
|
+
end
|
640
|
+
byte_array = Array.new(1 + 4 + (data_size * data.length), 0)
|
641
|
+
byte_array[0] = type
|
642
|
+
byte_array[1..4] = [data.length].pack('L<').bytes
|
643
|
+
data.each_with_index do |item, index|
|
644
|
+
if item.nil?
|
645
|
+
raise ArgumentError, "Value is nil."
|
646
|
+
elsif !ruby_type.any? { |element| item.is_a?(element) }
|
647
|
+
raise ArgumentError, "Value is not #{ruby_type}."
|
648
|
+
elsif range_check_flg && (item < min || max < item)
|
649
|
+
raise ArgumentError, "Value #{item} is out of range for #{type_name}. It must be between #{min} and #{max}."
|
650
|
+
end
|
651
|
+
byte_array[5 + (data_size * index), data_size] = yield item
|
652
|
+
end
|
653
|
+
return byte_array.pack('C*')
|
654
|
+
end
|
655
|
+
|
656
|
+
def self.serialize_array(ruby_type, data, type, data_size, min, max, type_name, &packer)
|
657
|
+
serialize_array_core(ruby_type, data, type, data_size, true, min, max, type_name) { |item| yield item }
|
658
|
+
end
|
659
|
+
|
660
|
+
def self.serialize_array_no_range(ruby_type, data, type, data_size, &packer)
|
661
|
+
serialize_array_core(ruby_type, data, type, data_size, false, nil, nil, nil) { |item| yield item }
|
662
|
+
end
|
663
|
+
|
664
|
+
def self.serialize_array_core(ruby_type, data, type, data_size, range_check_flg, min, max, type_name, &packer)
|
665
|
+
if data.nil?
|
666
|
+
raise ArgumentError, "Value is nil."
|
667
|
+
elsif !data.is_a?(Array)
|
668
|
+
raise ArgumentError, "Value is not Array."
|
669
|
+
end
|
670
|
+
byte_array = Array.new(1 + 4 + ((data_size + 1) * data.length), 0)
|
671
|
+
byte_array[0] = type
|
672
|
+
byte_array[1..4] = [data.length].pack('l<').bytes
|
673
|
+
data.each_with_index do |item, index|
|
674
|
+
if !item.nil? && !ruby_type.any? { |element| item.is_a?(element) }
|
675
|
+
raise ArgumentError, "Value is not #{ruby_type}."
|
676
|
+
elsif range_check_flg && !item.nil? && (item < min || max < item)
|
677
|
+
raise ArgumentError, "Value #{item} is out of range for #{type_name}. It must be between #{min} and #{max}."
|
678
|
+
end
|
679
|
+
if !item.nil?
|
680
|
+
byte_array[5 + (index * (data_size + 1))] = 1
|
681
|
+
byte_array[5 + (index * (data_size + 1)) + 1, data_size] = yield item
|
682
|
+
end
|
683
|
+
end
|
684
|
+
return byte_array.pack('C*')
|
685
|
+
end
|
686
|
+
end
|