blob: 90c4b05a0482806a0fa72dfc56859e225eb93114 [file] [log] [blame]
Kevin Clark916f3532009-03-20 04:21:39 +00001/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Bryan Duxbury09d13c22010-08-11 18:37:25 +000020#include "struct.h"
21#include "constants.h"
Bryan Duxbury6b771d22009-03-26 04:55:34 +000022#include "macros.h"
Bryan Duxburyc0166282009-02-02 00:48:17 +000023
Bryan Duxbury1e80d442009-02-03 18:16:54 +000024#ifndef HAVE_STRLCPY
25
26static
27size_t
28strlcpy (char *dst, const char *src, size_t dst_sz)
29{
30 size_t n;
31
32 for (n = 0; n < dst_sz; n++) {
33 if ((*dst++ = *src++) == '\0')
34 break;
35 }
36
37 if (n < dst_sz)
38 return n;
39 if (n > 0)
40 *(dst - 1) = '\0';
41 return n + strlen (src);
42}
Bryan Duxbury09d13c22010-08-11 18:37:25 +000043#else
44/*
45 Ruby 1.9.x includes the OpenBSD implementation of strlcpy.
46 See missing/strlcpy.c in Ruby 1.9 source
47 */
48extern size_t strlcpy(char *, const char *, size_t);
Bryan Duxbury1e80d442009-02-03 18:16:54 +000049#endif
50
Bryan Duxbury33e190c2010-02-16 21:19:01 +000051VALUE thrift_union_class;
52
53ID setfield_id;
54ID setvalue_id;
55
56ID to_s_method_id;
57ID name_to_id_method_id;
Bryan Duxburyc0166282009-02-02 00:48:17 +000058
59#define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET)
60#define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id)
61
62//-------------------------------------------
63// Writing section
64//-------------------------------------------
65
66// default fn pointers for protocol stuff here
67
68VALUE default_write_bool(VALUE protocol, VALUE value) {
69 rb_funcall(protocol, write_boolean_method_id, 1, value);
70 return Qnil;
71}
72
73VALUE default_write_byte(VALUE protocol, VALUE value) {
74 rb_funcall(protocol, write_byte_method_id, 1, value);
75 return Qnil;
76}
77
78VALUE default_write_i16(VALUE protocol, VALUE value) {
79 rb_funcall(protocol, write_i16_method_id, 1, value);
80 return Qnil;
81}
82
83VALUE default_write_i32(VALUE protocol, VALUE value) {
84 rb_funcall(protocol, write_i32_method_id, 1, value);
85 return Qnil;
86}
87
88VALUE default_write_i64(VALUE protocol, VALUE value) {
89 rb_funcall(protocol, write_i64_method_id, 1, value);
90 return Qnil;
91}
92
93VALUE default_write_double(VALUE protocol, VALUE value) {
94 rb_funcall(protocol, write_double_method_id, 1, value);
95 return Qnil;
96}
97
98VALUE default_write_string(VALUE protocol, VALUE value) {
99 rb_funcall(protocol, write_string_method_id, 1, value);
100 return Qnil;
101}
102
103VALUE default_write_list_begin(VALUE protocol, VALUE etype, VALUE length) {
104 rb_funcall(protocol, write_list_begin_method_id, 2, etype, length);
105 return Qnil;
106}
107
108VALUE default_write_list_end(VALUE protocol) {
109 rb_funcall(protocol, write_list_end_method_id, 0);
110 return Qnil;
111}
112
113VALUE default_write_set_begin(VALUE protocol, VALUE etype, VALUE length) {
114 rb_funcall(protocol, write_set_begin_method_id, 2, etype, length);
115 return Qnil;
116}
117
118VALUE default_write_set_end(VALUE protocol) {
119 rb_funcall(protocol, write_set_end_method_id, 0);
120 return Qnil;
121}
122
123VALUE default_write_map_begin(VALUE protocol, VALUE ktype, VALUE vtype, VALUE length) {
124 rb_funcall(protocol, write_map_begin_method_id, 3, ktype, vtype, length);
125 return Qnil;
126}
127
128VALUE default_write_map_end(VALUE protocol) {
129 rb_funcall(protocol, write_map_end_method_id, 0);
130 return Qnil;
131}
132
133VALUE default_write_struct_begin(VALUE protocol, VALUE struct_name) {
134 rb_funcall(protocol, write_struct_begin_method_id, 1, struct_name);
135 return Qnil;
136}
137
138VALUE default_write_struct_end(VALUE protocol) {
139 rb_funcall(protocol, write_struct_end_method_id, 0);
140 return Qnil;
141}
142
143VALUE default_write_field_begin(VALUE protocol, VALUE name, VALUE type, VALUE id) {
144 rb_funcall(protocol, write_field_begin_method_id, 3, name, type, id);
145 return Qnil;
146}
147
148VALUE default_write_field_end(VALUE protocol) {
149 rb_funcall(protocol, write_field_end_method_id, 0);
150 return Qnil;
151}
152
153VALUE default_write_field_stop(VALUE protocol) {
154 rb_funcall(protocol, write_field_stop_method_id, 0);
155 return Qnil;
156}
157
158VALUE default_read_field_begin(VALUE protocol) {
159 return rb_funcall(protocol, read_field_begin_method_id, 0);
160}
161
162VALUE default_read_field_end(VALUE protocol) {
163 return rb_funcall(protocol, read_field_end_method_id, 0);
164}
165
166VALUE default_read_map_begin(VALUE protocol) {
167 return rb_funcall(protocol, read_map_begin_method_id, 0);
168}
169
170VALUE default_read_map_end(VALUE protocol) {
171 return rb_funcall(protocol, read_map_end_method_id, 0);
172}
173
174VALUE default_read_list_begin(VALUE protocol) {
175 return rb_funcall(protocol, read_list_begin_method_id, 0);
176}
177
178VALUE default_read_list_end(VALUE protocol) {
179 return rb_funcall(protocol, read_list_end_method_id, 0);
180}
181
182VALUE default_read_set_begin(VALUE protocol) {
183 return rb_funcall(protocol, read_set_begin_method_id, 0);
184}
185
186VALUE default_read_set_end(VALUE protocol) {
187 return rb_funcall(protocol, read_set_end_method_id, 0);
188}
189
190VALUE default_read_byte(VALUE protocol) {
191 return rb_funcall(protocol, read_byte_method_id, 0);
192}
193
194VALUE default_read_bool(VALUE protocol) {
195 return rb_funcall(protocol, read_bool_method_id, 0);
196}
197
198VALUE default_read_i16(VALUE protocol) {
199 return rb_funcall(protocol, read_i16_method_id, 0);
200}
201
202VALUE default_read_i32(VALUE protocol) {
203 return rb_funcall(protocol, read_i32_method_id, 0);
204}
205
206VALUE default_read_i64(VALUE protocol) {
207 return rb_funcall(protocol, read_i64_method_id, 0);
208}
209
210VALUE default_read_double(VALUE protocol) {
211 return rb_funcall(protocol, read_double_method_id, 0);
212}
213
214VALUE default_read_string(VALUE protocol) {
215 return rb_funcall(protocol, read_string_method_id, 0);
216}
217
218VALUE default_read_struct_begin(VALUE protocol) {
219 return rb_funcall(protocol, read_struct_begin_method_id, 0);
220}
221
222VALUE default_read_struct_end(VALUE protocol) {
223 return rb_funcall(protocol, read_struct_end_method_id, 0);
224}
225
Bryan Duxburyc0166282009-02-02 00:48:17 +0000226// end default protocol methods
227
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000228static VALUE rb_thrift_union_write (VALUE self, VALUE protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000229static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol);
230static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info);
231
232VALUE get_field_value(VALUE obj, VALUE field_name) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000233 char name_buf[RSTRING_LEN(field_name) + 1];
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000234
Bryan Duxburyc0166282009-02-02 00:48:17 +0000235 name_buf[0] = '@';
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000236 strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000237
238 VALUE value = rb_ivar_get(obj, rb_intern(name_buf));
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000239
Bryan Duxburyc0166282009-02-02 00:48:17 +0000240 return value;
241}
242
243static void write_container(int ttype, VALUE field_info, VALUE value, VALUE protocol) {
244 int sz, i;
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000245
Bryan Duxburyc0166282009-02-02 00:48:17 +0000246 if (ttype == TTYPE_MAP) {
247 VALUE keys;
248 VALUE key;
249 VALUE val;
250
251 Check_Type(value, T_HASH);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000252
Bryan Duxburyc0166282009-02-02 00:48:17 +0000253 VALUE key_info = rb_hash_aref(field_info, key_sym);
254 VALUE keytype_value = rb_hash_aref(key_info, type_sym);
255 int keytype = FIX2INT(keytype_value);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000256
Bryan Duxburyc0166282009-02-02 00:48:17 +0000257 VALUE value_info = rb_hash_aref(field_info, value_sym);
258 VALUE valuetype_value = rb_hash_aref(value_info, type_sym);
259 int valuetype = FIX2INT(valuetype_value);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000260
Bryan Duxburyc0166282009-02-02 00:48:17 +0000261 keys = rb_funcall(value, keys_method_id, 0);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000262
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000263 sz = RARRAY_LEN(keys);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000264
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000265 default_write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz));
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000266
Bryan Duxburyc0166282009-02-02 00:48:17 +0000267 for (i = 0; i < sz; i++) {
268 key = rb_ary_entry(keys, i);
269 val = rb_hash_aref(value, key);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000270
Bryan Duxburyc0166282009-02-02 00:48:17 +0000271 if (IS_CONTAINER(keytype)) {
272 write_container(keytype, key_info, key, protocol);
273 } else {
274 write_anything(keytype, key, protocol, key_info);
275 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000276
Bryan Duxburyc0166282009-02-02 00:48:17 +0000277 if (IS_CONTAINER(valuetype)) {
278 write_container(valuetype, value_info, val, protocol);
279 } else {
280 write_anything(valuetype, val, protocol, value_info);
281 }
282 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000283
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000284 default_write_map_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000285 } else if (ttype == TTYPE_LIST) {
286 Check_Type(value, T_ARRAY);
287
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000288 sz = RARRAY_LEN(value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000289
290 VALUE element_type_info = rb_hash_aref(field_info, element_sym);
291 VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
292 int element_type = FIX2INT(element_type_value);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000293
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000294 default_write_list_begin(protocol, element_type_value, INT2FIX(sz));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000295 for (i = 0; i < sz; ++i) {
296 VALUE val = rb_ary_entry(value, i);
297 if (IS_CONTAINER(element_type)) {
298 write_container(element_type, element_type_info, val, protocol);
299 } else {
300 write_anything(element_type, val, protocol, element_type_info);
301 }
302 }
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000303 default_write_list_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000304 } else if (ttype == TTYPE_SET) {
305 VALUE items;
306
307 if (TYPE(value) == T_ARRAY) {
308 items = value;
309 } else {
310 if (rb_cSet == CLASS_OF(value)) {
311 items = rb_funcall(value, entries_method_id, 0);
312 } else {
313 Check_Type(value, T_HASH);
314 items = rb_funcall(value, keys_method_id, 0);
315 }
316 }
317
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000318 sz = RARRAY_LEN(items);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000319
320 VALUE element_type_info = rb_hash_aref(field_info, element_sym);
321 VALUE element_type_value = rb_hash_aref(element_type_info, type_sym);
322 int element_type = FIX2INT(element_type_value);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000323
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000324 default_write_set_begin(protocol, element_type_value, INT2FIX(sz));
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000325
Bryan Duxburyc0166282009-02-02 00:48:17 +0000326 for (i = 0; i < sz; i++) {
327 VALUE val = rb_ary_entry(items, i);
328 if (IS_CONTAINER(element_type)) {
329 write_container(element_type, element_type_info, val, protocol);
330 } else {
331 write_anything(element_type, val, protocol, element_type_info);
332 }
333 }
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000334
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000335 default_write_set_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000336 } else {
337 rb_raise(rb_eNotImpError, "can't write container of type: %d", ttype);
338 }
339}
340
341static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info) {
342 if (ttype == TTYPE_BOOL) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000343 default_write_bool(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000344 } else if (ttype == TTYPE_BYTE) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000345 default_write_byte(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000346 } else if (ttype == TTYPE_I16) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000347 default_write_i16(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000348 } else if (ttype == TTYPE_I32) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000349 default_write_i32(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000350 } else if (ttype == TTYPE_I64) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000351 default_write_i64(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000352 } else if (ttype == TTYPE_DOUBLE) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000353 default_write_double(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000354 } else if (ttype == TTYPE_STRING) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000355 default_write_string(protocol, value);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000356 } else if (IS_CONTAINER(ttype)) {
357 write_container(ttype, field_info, value, protocol);
358 } else if (ttype == TTYPE_STRUCT) {
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000359 if (rb_obj_is_kind_of(value, thrift_union_class)) {
360 rb_thrift_union_write(value, protocol);
361 } else {
362 rb_thrift_struct_write(value, protocol);
363 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000364 } else {
365 rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", ttype);
366 }
367}
368
369static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) {
370 // call validate
371 rb_funcall(self, validate_method_id, 0);
372
Bryan Duxburyc0166282009-02-02 00:48:17 +0000373 // write struct begin
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000374 default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self)));
Bryan Duxburyccae8842009-07-31 18:47:09 +0000375
Bryan Duxburyc0166282009-02-02 00:48:17 +0000376 // iterate through all the fields here
377 VALUE struct_fields = STRUCT_FIELDS(self);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000378
Bryan Duxburyc0166282009-02-02 00:48:17 +0000379 VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0);
380 VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000381
Bryan Duxburyc0166282009-02-02 00:48:17 +0000382 int i = 0;
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000383 for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) {
Bryan Duxburyc0166282009-02-02 00:48:17 +0000384 VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000385
Bryan Duxburyc0166282009-02-02 00:48:17 +0000386 VALUE field_info = rb_hash_aref(struct_fields, field_id);
387
388 VALUE ttype_value = rb_hash_aref(field_info, type_sym);
389 int ttype = FIX2INT(ttype_value);
390 VALUE field_name = rb_hash_aref(field_info, name_sym);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000391
Bryan Duxburyc0166282009-02-02 00:48:17 +0000392 VALUE field_value = get_field_value(self, field_name);
393
394 if (!NIL_P(field_value)) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000395 default_write_field_begin(protocol, field_name, ttype_value, field_id);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000396
Bryan Duxburyc0166282009-02-02 00:48:17 +0000397 write_anything(ttype, field_value, protocol, field_info);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000398
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000399 default_write_field_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000400 }
401 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000402
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000403 default_write_field_stop(protocol);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000404
Bryan Duxburyc0166282009-02-02 00:48:17 +0000405 // write struct end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000406 default_write_struct_end(protocol);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000407
Bryan Duxburyc0166282009-02-02 00:48:17 +0000408 return Qnil;
409}
410
411//-------------------------------------------
412// Reading section
413//-------------------------------------------
414
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000415static VALUE rb_thrift_union_read(VALUE self, VALUE protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000416static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol);
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000417static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size);
418static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000419
420static void set_field_value(VALUE obj, VALUE field_name, VALUE value) {
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000421 char name_buf[RSTRING_LEN(field_name) + 1];
Bryan Duxburyc0166282009-02-02 00:48:17 +0000422
423 name_buf[0] = '@';
Bryan Duxburye3ab50d2009-03-25 21:06:53 +0000424 strlcpy(&name_buf[1], RSTRING_PTR(field_name), sizeof(name_buf));
Bryan Duxburyc0166282009-02-02 00:48:17 +0000425
426 rb_ivar_set(obj, rb_intern(name_buf), value);
427}
428
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000429// Helper method to skip the contents of a map (assumes the map header has been read).
430static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size) {
431 int i;
432 for (i = 0; i < size; i++) {
433 rb_funcall(protocol, skip_method_id, 1, key_type_value);
434 rb_funcall(protocol, skip_method_id, 1, value_type_value);
435 }
436}
437
438// Helper method to skip the contents of a list or set (assumes the list/set header has been read).
439static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size) {
440 int i;
441 for (i = 0; i < size; i++) {
442 rb_funcall(protocol, skip_method_id, 1, element_type_value);
443 }
444}
445
Bryan Duxburyc0166282009-02-02 00:48:17 +0000446static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) {
447 VALUE result = Qnil;
Bryan Duxburyccae8842009-07-31 18:47:09 +0000448
Bryan Duxburyc0166282009-02-02 00:48:17 +0000449 if (ttype == TTYPE_BOOL) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000450 result = default_read_bool(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000451 } else if (ttype == TTYPE_BYTE) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000452 result = default_read_byte(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000453 } else if (ttype == TTYPE_I16) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000454 result = default_read_i16(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000455 } else if (ttype == TTYPE_I32) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000456 result = default_read_i32(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000457 } else if (ttype == TTYPE_I64) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000458 result = default_read_i64(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000459 } else if (ttype == TTYPE_STRING) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000460 result = default_read_string(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000461 } else if (ttype == TTYPE_DOUBLE) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000462 result = default_read_double(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000463 } else if (ttype == TTYPE_STRUCT) {
464 VALUE klass = rb_hash_aref(field_info, class_sym);
465 result = rb_class_new_instance(0, NULL, klass);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000466
467 if (rb_obj_is_kind_of(result, thrift_union_class)) {
468 rb_thrift_union_read(result, protocol);
469 } else {
470 rb_thrift_struct_read(result, protocol);
471 }
Bryan Duxburyc0166282009-02-02 00:48:17 +0000472 } else if (ttype == TTYPE_MAP) {
473 int i;
474
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000475 VALUE map_header = default_read_map_begin(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000476 int key_ttype = FIX2INT(rb_ary_entry(map_header, 0));
477 int value_ttype = FIX2INT(rb_ary_entry(map_header, 1));
478 int num_entries = FIX2INT(rb_ary_entry(map_header, 2));
Bryan Duxburyccae8842009-07-31 18:47:09 +0000479
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000480 // Check the declared key and value types against the expected ones and skip the map contents
481 // if the types don't match.
Bryan Duxburyc0166282009-02-02 00:48:17 +0000482 VALUE key_info = rb_hash_aref(field_info, key_sym);
483 VALUE value_info = rb_hash_aref(field_info, value_sym);
484
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000485 if (!NIL_P(key_info) && !NIL_P(value_info)) {
486 int specified_key_type = FIX2INT(rb_hash_aref(key_info, type_sym));
487 int specified_value_type = FIX2INT(rb_hash_aref(value_info, type_sym));
488 if (specified_key_type == key_ttype && specified_value_type == value_ttype) {
489 result = rb_hash_new();
Bryan Duxburyccae8842009-07-31 18:47:09 +0000490
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000491 for (i = 0; i < num_entries; ++i) {
492 VALUE key, val;
Bryan Duxburyccae8842009-07-31 18:47:09 +0000493
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000494 key = read_anything(protocol, key_ttype, key_info);
495 val = read_anything(protocol, value_ttype, value_info);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000496
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000497 rb_hash_aset(result, key, val);
498 }
499 } else {
500 skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries);
501 }
502 } else {
503 skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000504 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000505
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000506 default_read_map_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000507 } else if (ttype == TTYPE_LIST) {
508 int i;
509
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000510 VALUE list_header = default_read_list_begin(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000511 int element_ttype = FIX2INT(rb_ary_entry(list_header, 0));
512 int num_elements = FIX2INT(rb_ary_entry(list_header, 1));
Bryan Duxburyccae8842009-07-31 18:47:09 +0000513
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000514 // Check the declared element type against the expected one and skip the list contents
515 // if the types don't match.
516 VALUE element_info = rb_hash_aref(field_info, element_sym);
517 if (!NIL_P(element_info)) {
518 int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym));
519 if (specified_element_type == element_ttype) {
520 result = rb_ary_new2(num_elements);
521
522 for (i = 0; i < num_elements; ++i) {
523 rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym)));
524 }
525 } else {
526 skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
527 }
528 } else {
529 skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000530 }
531
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000532 default_read_list_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000533 } else if (ttype == TTYPE_SET) {
534 VALUE items;
535 int i;
536
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000537 VALUE set_header = default_read_set_begin(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000538 int element_ttype = FIX2INT(rb_ary_entry(set_header, 0));
539 int num_elements = FIX2INT(rb_ary_entry(set_header, 1));
Bryan Duxburyccae8842009-07-31 18:47:09 +0000540
Bryan Duxbury911d2f12011-05-31 20:03:29 +0000541 // Check the declared element type against the expected one and skip the set contents
542 // if the types don't match.
543 VALUE element_info = rb_hash_aref(field_info, element_sym);
544 if (!NIL_P(element_info)) {
545 int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym));
546 if (specified_element_type == element_ttype) {
547 items = rb_ary_new2(num_elements);
548
549 for (i = 0; i < num_elements; ++i) {
550 rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym)));
551 }
552
553 result = rb_class_new_instance(1, &items, rb_cSet);
554 } else {
555 skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
556 }
557 } else {
558 skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000559 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000560
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000561 default_read_set_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000562 } else {
563 rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype);
564 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000565
Bryan Duxburyc0166282009-02-02 00:48:17 +0000566 return result;
567}
568
569static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) {
Bryan Duxburyc0166282009-02-02 00:48:17 +0000570 // read struct begin
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000571 default_read_struct_begin(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000572
573 VALUE struct_fields = STRUCT_FIELDS(self);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000574
Bryan Duxburyc0166282009-02-02 00:48:17 +0000575 // read each field
576 while (true) {
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000577 VALUE field_header = default_read_field_begin(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000578 VALUE field_type_value = rb_ary_entry(field_header, 1);
579 int field_type = FIX2INT(field_type_value);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000580
Bryan Duxburyc0166282009-02-02 00:48:17 +0000581 if (field_type == TTYPE_STOP) {
582 break;
583 }
584
585 // make sure we got a type we expected
586 VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));
587
588 if (!NIL_P(field_info)) {
589 int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
590 if (field_type == specified_type) {
591 // read the value
592 VALUE name = rb_hash_aref(field_info, name_sym);
593 set_field_value(self, name, read_anything(protocol, field_type, field_info));
594 } else {
595 rb_funcall(protocol, skip_method_id, 1, field_type_value);
596 }
597 } else {
598 rb_funcall(protocol, skip_method_id, 1, field_type_value);
599 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000600
Bryan Duxburyc0166282009-02-02 00:48:17 +0000601 // read field end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000602 default_read_field_end(protocol);
Bryan Duxburyc0166282009-02-02 00:48:17 +0000603 }
Bryan Duxburyccae8842009-07-31 18:47:09 +0000604
Bryan Duxburyc0166282009-02-02 00:48:17 +0000605 // read struct end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000606 default_read_struct_end(protocol);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000607
Bryan Duxbury834895d2009-10-15 01:20:34 +0000608 // call validate
609 rb_funcall(self, validate_method_id, 0);
610
Bryan Duxburyc0166282009-02-02 00:48:17 +0000611 return Qnil;
612}
613
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000614
615// --------------------------------
616// Union section
617// --------------------------------
618
619static VALUE rb_thrift_union_read(VALUE self, VALUE protocol) {
620 // read struct begin
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000621 default_read_struct_begin(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000622
623 VALUE struct_fields = STRUCT_FIELDS(self);
624
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000625 VALUE field_header = default_read_field_begin(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000626 VALUE field_type_value = rb_ary_entry(field_header, 1);
627 int field_type = FIX2INT(field_type_value);
628
629 // make sure we got a type we expected
630 VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));
631
632 if (!NIL_P(field_info)) {
633 int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
634 if (field_type == specified_type) {
635 // read the value
636 VALUE name = rb_hash_aref(field_info, name_sym);
637 rb_iv_set(self, "@setfield", ID2SYM(rb_intern(RSTRING_PTR(name))));
638 rb_iv_set(self, "@value", read_anything(protocol, field_type, field_info));
639 } else {
640 rb_funcall(protocol, skip_method_id, 1, field_type_value);
641 }
642 } else {
643 rb_funcall(protocol, skip_method_id, 1, field_type_value);
644 }
645
646 // read field end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000647 default_read_field_end(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000648
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000649 field_header = default_read_field_begin(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000650 field_type_value = rb_ary_entry(field_header, 1);
651 field_type = FIX2INT(field_type_value);
652
653 if (field_type != TTYPE_STOP) {
654 rb_raise(rb_eRuntimeError, "too many fields in union!");
655 }
656
657 // read field end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000658 default_read_field_end(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000659
660 // read struct end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000661 default_read_struct_end(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000662
663 // call validate
664 rb_funcall(self, validate_method_id, 0);
665
666 return Qnil;
667}
668
669static VALUE rb_thrift_union_write(VALUE self, VALUE protocol) {
670 // call validate
671 rb_funcall(self, validate_method_id, 0);
672
673 // write struct begin
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000674 default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self)));
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000675
676 VALUE struct_fields = STRUCT_FIELDS(self);
677
678 VALUE setfield = rb_ivar_get(self, setfield_id);
679 VALUE setvalue = rb_ivar_get(self, setvalue_id);
680 VALUE field_id = rb_funcall(self, name_to_id_method_id, 1, rb_funcall(setfield, to_s_method_id, 0));
681
682 VALUE field_info = rb_hash_aref(struct_fields, field_id);
683
684 VALUE ttype_value = rb_hash_aref(field_info, type_sym);
685 int ttype = FIX2INT(ttype_value);
686
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000687 default_write_field_begin(protocol, setfield, ttype_value, field_id);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000688
689 write_anything(ttype, setvalue, protocol, field_info);
690
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000691 default_write_field_end(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000692
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000693 default_write_field_stop(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000694
695 // write struct end
Bryan Duxburyd2cc5bb2010-07-28 21:00:06 +0000696 default_write_struct_end(protocol);
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000697
698 return Qnil;
699}
700
Bryan Duxburyc0166282009-02-02 00:48:17 +0000701void Init_struct() {
702 VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct"));
Bryan Duxburyccae8842009-07-31 18:47:09 +0000703
Bryan Duxburyc0166282009-02-02 00:48:17 +0000704 rb_define_method(struct_module, "write", rb_thrift_struct_write, 1);
705 rb_define_method(struct_module, "read", rb_thrift_struct_read, 1);
Bryan Duxburyccae8842009-07-31 18:47:09 +0000706
Bryan Duxbury33e190c2010-02-16 21:19:01 +0000707 thrift_union_class = rb_const_get(thrift_module, rb_intern("Union"));
708
709 rb_define_method(thrift_union_class, "write", rb_thrift_union_write, 1);
710 rb_define_method(thrift_union_class, "read", rb_thrift_union_read, 1);
711
712 setfield_id = rb_intern("@setfield");
713 setvalue_id = rb_intern("@value");
714
715 to_s_method_id = rb_intern("to_s");
716 name_to_id_method_id = rb_intern("name_to_id");
Bryan Duxbury65073e52010-02-23 15:46:46 +0000717}