| Kevin Clark | 916f353 | 2009-03-20 04:21:39 +0000 | [diff] [blame] | 1 | /** |
| 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 Duxbury | 09d13c2 | 2010-08-11 18:37:25 +0000 | [diff] [blame] | 20 | #include "struct.h" |
| 21 | #include "constants.h" |
| Bryan Duxbury | 6b771d2 | 2009-03-26 04:55:34 +0000 | [diff] [blame] | 22 | #include "macros.h" |
| Dmytro Shteflyuk | 32776c0 | 2026-02-10 12:25:07 -0500 | [diff] [blame^] | 23 | #include "protocol.h" |
| Jake Farrell | d5df77a | 2011-11-06 17:43:44 +0000 | [diff] [blame] | 24 | #include "strlcpy.h" |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 25 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 26 | VALUE thrift_union_class; |
| 27 | |
| 28 | ID setfield_id; |
| 29 | ID setvalue_id; |
| 30 | |
| 31 | ID to_s_method_id; |
| 32 | ID name_to_id_method_id; |
| Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 33 | static ID sorted_field_ids_method_id; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 34 | |
| 35 | #define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET) |
| 36 | #define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id) |
| 37 | |
| 38 | //------------------------------------------- |
| 39 | // Writing section |
| 40 | //------------------------------------------- |
| 41 | |
| 42 | // default fn pointers for protocol stuff here |
| 43 | |
| 44 | VALUE default_write_bool(VALUE protocol, VALUE value) { |
| 45 | rb_funcall(protocol, write_boolean_method_id, 1, value); |
| 46 | return Qnil; |
| 47 | } |
| 48 | |
| 49 | VALUE default_write_byte(VALUE protocol, VALUE value) { |
| 50 | rb_funcall(protocol, write_byte_method_id, 1, value); |
| 51 | return Qnil; |
| 52 | } |
| 53 | |
| 54 | VALUE default_write_i16(VALUE protocol, VALUE value) { |
| 55 | rb_funcall(protocol, write_i16_method_id, 1, value); |
| 56 | return Qnil; |
| 57 | } |
| 58 | |
| 59 | VALUE default_write_i32(VALUE protocol, VALUE value) { |
| 60 | rb_funcall(protocol, write_i32_method_id, 1, value); |
| 61 | return Qnil; |
| 62 | } |
| 63 | |
| 64 | VALUE default_write_i64(VALUE protocol, VALUE value) { |
| 65 | rb_funcall(protocol, write_i64_method_id, 1, value); |
| 66 | return Qnil; |
| 67 | } |
| 68 | |
| 69 | VALUE default_write_double(VALUE protocol, VALUE value) { |
| 70 | rb_funcall(protocol, write_double_method_id, 1, value); |
| 71 | return Qnil; |
| 72 | } |
| 73 | |
| 74 | VALUE default_write_string(VALUE protocol, VALUE value) { |
| 75 | rb_funcall(protocol, write_string_method_id, 1, value); |
| 76 | return Qnil; |
| 77 | } |
| 78 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 79 | VALUE default_write_uuid(VALUE protocol, VALUE value) { |
| 80 | rb_funcall(protocol, write_uuid_method_id, 1, value); |
| 81 | return Qnil; |
| 82 | } |
| 83 | |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 84 | VALUE default_write_binary(VALUE protocol, VALUE value) { |
| 85 | rb_funcall(protocol, write_binary_method_id, 1, value); |
| 86 | return Qnil; |
| 87 | } |
| 88 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 89 | VALUE default_write_list_begin(VALUE protocol, VALUE etype, VALUE length) { |
| 90 | rb_funcall(protocol, write_list_begin_method_id, 2, etype, length); |
| 91 | return Qnil; |
| 92 | } |
| 93 | |
| 94 | VALUE default_write_list_end(VALUE protocol) { |
| 95 | rb_funcall(protocol, write_list_end_method_id, 0); |
| 96 | return Qnil; |
| 97 | } |
| 98 | |
| 99 | VALUE default_write_set_begin(VALUE protocol, VALUE etype, VALUE length) { |
| 100 | rb_funcall(protocol, write_set_begin_method_id, 2, etype, length); |
| 101 | return Qnil; |
| 102 | } |
| 103 | |
| 104 | VALUE default_write_set_end(VALUE protocol) { |
| 105 | rb_funcall(protocol, write_set_end_method_id, 0); |
| 106 | return Qnil; |
| 107 | } |
| 108 | |
| 109 | VALUE default_write_map_begin(VALUE protocol, VALUE ktype, VALUE vtype, VALUE length) { |
| 110 | rb_funcall(protocol, write_map_begin_method_id, 3, ktype, vtype, length); |
| 111 | return Qnil; |
| 112 | } |
| 113 | |
| 114 | VALUE default_write_map_end(VALUE protocol) { |
| 115 | rb_funcall(protocol, write_map_end_method_id, 0); |
| 116 | return Qnil; |
| 117 | } |
| 118 | |
| 119 | VALUE default_write_struct_begin(VALUE protocol, VALUE struct_name) { |
| 120 | rb_funcall(protocol, write_struct_begin_method_id, 1, struct_name); |
| 121 | return Qnil; |
| 122 | } |
| 123 | |
| 124 | VALUE default_write_struct_end(VALUE protocol) { |
| 125 | rb_funcall(protocol, write_struct_end_method_id, 0); |
| 126 | return Qnil; |
| 127 | } |
| 128 | |
| 129 | VALUE default_write_field_begin(VALUE protocol, VALUE name, VALUE type, VALUE id) { |
| 130 | rb_funcall(protocol, write_field_begin_method_id, 3, name, type, id); |
| 131 | return Qnil; |
| 132 | } |
| 133 | |
| 134 | VALUE default_write_field_end(VALUE protocol) { |
| 135 | rb_funcall(protocol, write_field_end_method_id, 0); |
| 136 | return Qnil; |
| 137 | } |
| 138 | |
| 139 | VALUE default_write_field_stop(VALUE protocol) { |
| 140 | rb_funcall(protocol, write_field_stop_method_id, 0); |
| 141 | return Qnil; |
| 142 | } |
| 143 | |
| 144 | VALUE default_read_field_begin(VALUE protocol) { |
| 145 | return rb_funcall(protocol, read_field_begin_method_id, 0); |
| 146 | } |
| 147 | |
| 148 | VALUE default_read_field_end(VALUE protocol) { |
| 149 | return rb_funcall(protocol, read_field_end_method_id, 0); |
| 150 | } |
| 151 | |
| 152 | VALUE default_read_map_begin(VALUE protocol) { |
| 153 | return rb_funcall(protocol, read_map_begin_method_id, 0); |
| 154 | } |
| 155 | |
| 156 | VALUE default_read_map_end(VALUE protocol) { |
| 157 | return rb_funcall(protocol, read_map_end_method_id, 0); |
| 158 | } |
| 159 | |
| 160 | VALUE default_read_list_begin(VALUE protocol) { |
| 161 | return rb_funcall(protocol, read_list_begin_method_id, 0); |
| 162 | } |
| 163 | |
| 164 | VALUE default_read_list_end(VALUE protocol) { |
| 165 | return rb_funcall(protocol, read_list_end_method_id, 0); |
| 166 | } |
| 167 | |
| 168 | VALUE default_read_set_begin(VALUE protocol) { |
| 169 | return rb_funcall(protocol, read_set_begin_method_id, 0); |
| 170 | } |
| 171 | |
| 172 | VALUE default_read_set_end(VALUE protocol) { |
| 173 | return rb_funcall(protocol, read_set_end_method_id, 0); |
| 174 | } |
| 175 | |
| 176 | VALUE default_read_byte(VALUE protocol) { |
| 177 | return rb_funcall(protocol, read_byte_method_id, 0); |
| 178 | } |
| 179 | |
| 180 | VALUE default_read_bool(VALUE protocol) { |
| 181 | return rb_funcall(protocol, read_bool_method_id, 0); |
| 182 | } |
| 183 | |
| 184 | VALUE default_read_i16(VALUE protocol) { |
| 185 | return rb_funcall(protocol, read_i16_method_id, 0); |
| 186 | } |
| 187 | |
| 188 | VALUE default_read_i32(VALUE protocol) { |
| 189 | return rb_funcall(protocol, read_i32_method_id, 0); |
| 190 | } |
| 191 | |
| 192 | VALUE default_read_i64(VALUE protocol) { |
| 193 | return rb_funcall(protocol, read_i64_method_id, 0); |
| 194 | } |
| 195 | |
| 196 | VALUE default_read_double(VALUE protocol) { |
| 197 | return rb_funcall(protocol, read_double_method_id, 0); |
| 198 | } |
| 199 | |
| 200 | VALUE default_read_string(VALUE protocol) { |
| 201 | return rb_funcall(protocol, read_string_method_id, 0); |
| 202 | } |
| 203 | |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 204 | VALUE default_read_uuid(VALUE protocol) { |
| 205 | return rb_funcall(protocol, read_uuid_method_id, 0); |
| 206 | } |
| 207 | |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 208 | VALUE default_read_binary(VALUE protocol) { |
| 209 | return rb_funcall(protocol, read_binary_method_id, 0); |
| 210 | } |
| 211 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 212 | VALUE default_read_struct_begin(VALUE protocol) { |
| 213 | return rb_funcall(protocol, read_struct_begin_method_id, 0); |
| 214 | } |
| 215 | |
| 216 | VALUE default_read_struct_end(VALUE protocol) { |
| 217 | return rb_funcall(protocol, read_struct_end_method_id, 0); |
| 218 | } |
| 219 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 220 | // end default protocol methods |
| 221 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 222 | static VALUE rb_thrift_union_write (VALUE self, VALUE protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 223 | static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol); |
| 224 | static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info); |
| 225 | |
| 226 | VALUE get_field_value(VALUE obj, VALUE field_name) { |
| Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 227 | char name_buf[RSTRING_LEN(field_name) + 2]; |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 228 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 229 | name_buf[0] = '@'; |
| Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 230 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), RSTRING_LEN(field_name) + 1); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 231 | |
| 232 | VALUE value = rb_ivar_get(obj, rb_intern(name_buf)); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 233 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 234 | return value; |
| 235 | } |
| 236 | |
| 237 | static void write_container(int ttype, VALUE field_info, VALUE value, VALUE protocol) { |
| Jean Boussier | 1e84341 | 2021-10-13 12:36:28 +0200 | [diff] [blame] | 238 | long sz, i; |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 239 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 240 | if (ttype == TTYPE_MAP) { |
| 241 | VALUE keys; |
| 242 | VALUE key; |
| 243 | VALUE val; |
| 244 | |
| 245 | Check_Type(value, T_HASH); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 246 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 247 | VALUE key_info = rb_hash_aref(field_info, key_sym); |
| 248 | VALUE keytype_value = rb_hash_aref(key_info, type_sym); |
| 249 | int keytype = FIX2INT(keytype_value); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 250 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 251 | VALUE value_info = rb_hash_aref(field_info, value_sym); |
| 252 | VALUE valuetype_value = rb_hash_aref(value_info, type_sym); |
| 253 | int valuetype = FIX2INT(valuetype_value); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 254 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 255 | keys = rb_funcall(value, keys_method_id, 0); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 256 | |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 257 | sz = RARRAY_LEN(keys); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 258 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 259 | default_write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz)); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 260 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 261 | for (i = 0; i < sz; i++) { |
| 262 | key = rb_ary_entry(keys, i); |
| 263 | val = rb_hash_aref(value, key); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 264 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 265 | if (IS_CONTAINER(keytype)) { |
| 266 | write_container(keytype, key_info, key, protocol); |
| 267 | } else { |
| 268 | write_anything(keytype, key, protocol, key_info); |
| 269 | } |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 270 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 271 | if (IS_CONTAINER(valuetype)) { |
| 272 | write_container(valuetype, value_info, val, protocol); |
| 273 | } else { |
| 274 | write_anything(valuetype, val, protocol, value_info); |
| 275 | } |
| 276 | } |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 277 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 278 | default_write_map_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 279 | } else if (ttype == TTYPE_LIST) { |
| 280 | Check_Type(value, T_ARRAY); |
| 281 | |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 282 | sz = RARRAY_LEN(value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 283 | |
| 284 | VALUE element_type_info = rb_hash_aref(field_info, element_sym); |
| 285 | VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); |
| 286 | int element_type = FIX2INT(element_type_value); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 287 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 288 | default_write_list_begin(protocol, element_type_value, INT2FIX(sz)); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 289 | for (i = 0; i < sz; ++i) { |
| 290 | VALUE val = rb_ary_entry(value, i); |
| 291 | if (IS_CONTAINER(element_type)) { |
| 292 | write_container(element_type, element_type_info, val, protocol); |
| 293 | } else { |
| 294 | write_anything(element_type, val, protocol, element_type_info); |
| 295 | } |
| 296 | } |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 297 | default_write_list_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 298 | } else if (ttype == TTYPE_SET) { |
| 299 | VALUE items; |
| 300 | |
| 301 | if (TYPE(value) == T_ARRAY) { |
| 302 | items = value; |
| Joe Ennever | 5b15f8c | 2015-08-31 19:20:36 +0000 | [diff] [blame] | 303 | } else { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 304 | if (rb_cSet == CLASS_OF(value)) { |
| 305 | items = rb_funcall(value, entries_method_id, 0); |
| 306 | } else { |
| 307 | Check_Type(value, T_HASH); |
| 308 | items = rb_funcall(value, keys_method_id, 0); |
| 309 | } |
| 310 | } |
| 311 | |
| Bryan Duxbury | e3ab50d | 2009-03-25 21:06:53 +0000 | [diff] [blame] | 312 | sz = RARRAY_LEN(items); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 313 | |
| 314 | VALUE element_type_info = rb_hash_aref(field_info, element_sym); |
| 315 | VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); |
| 316 | int element_type = FIX2INT(element_type_value); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 317 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 318 | default_write_set_begin(protocol, element_type_value, INT2FIX(sz)); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 319 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 320 | for (i = 0; i < sz; i++) { |
| 321 | VALUE val = rb_ary_entry(items, i); |
| 322 | if (IS_CONTAINER(element_type)) { |
| 323 | write_container(element_type, element_type_info, val, protocol); |
| 324 | } else { |
| 325 | write_anything(element_type, val, protocol, element_type_info); |
| 326 | } |
| 327 | } |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 328 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 329 | default_write_set_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 330 | } else { |
| 331 | rb_raise(rb_eNotImpError, "can't write container of type: %d", ttype); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info) { |
| 336 | if (ttype == TTYPE_BOOL) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 337 | default_write_bool(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 338 | } else if (ttype == TTYPE_BYTE) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 339 | default_write_byte(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 340 | } else if (ttype == TTYPE_I16) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 341 | default_write_i16(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 342 | } else if (ttype == TTYPE_I32) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 343 | default_write_i32(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 344 | } else if (ttype == TTYPE_I64) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 345 | default_write_i64(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 346 | } else if (ttype == TTYPE_DOUBLE) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 347 | default_write_double(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 348 | } else if (ttype == TTYPE_STRING) { |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 349 | VALUE is_binary = rb_hash_aref(field_info, binary_sym); |
| 350 | if (is_binary != Qtrue) { |
| 351 | default_write_string(protocol, value); |
| 352 | } else { |
| 353 | default_write_binary(protocol, value); |
| 354 | } |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 355 | } else if (ttype == TTYPE_UUID) { |
| 356 | default_write_uuid(protocol, value); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 357 | } else if (IS_CONTAINER(ttype)) { |
| 358 | write_container(ttype, field_info, value, protocol); |
| 359 | } else if (ttype == TTYPE_STRUCT) { |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 360 | if (rb_obj_is_kind_of(value, thrift_union_class)) { |
| 361 | rb_thrift_union_write(value, protocol); |
| 362 | } else { |
| 363 | rb_thrift_struct_write(value, protocol); |
| 364 | } |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 365 | } else { |
| 366 | rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", ttype); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { |
| 371 | // call validate |
| 372 | rb_funcall(self, validate_method_id, 0); |
| 373 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 374 | // write struct begin |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 375 | default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 376 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 377 | // iterate through all the fields here |
| 378 | VALUE struct_fields = STRUCT_FIELDS(self); |
| Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 379 | VALUE sorted_field_ids = rb_funcall(self, sorted_field_ids_method_id, 0); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 380 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 381 | int i = 0; |
| Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 382 | for (i=0; i < RARRAY_LEN(sorted_field_ids); i++) { |
| 383 | VALUE field_id = rb_ary_entry(sorted_field_ids, i); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 384 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 385 | VALUE field_info = rb_hash_aref(struct_fields, field_id); |
| 386 | |
| 387 | VALUE ttype_value = rb_hash_aref(field_info, type_sym); |
| 388 | int ttype = FIX2INT(ttype_value); |
| 389 | VALUE field_name = rb_hash_aref(field_info, name_sym); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 390 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 391 | VALUE field_value = get_field_value(self, field_name); |
| 392 | |
| 393 | if (!NIL_P(field_value)) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 394 | default_write_field_begin(protocol, field_name, ttype_value, field_id); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 395 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 396 | write_anything(ttype, field_value, protocol, field_info); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 397 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 398 | default_write_field_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 399 | } |
| 400 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 401 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 402 | default_write_field_stop(protocol); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 403 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 404 | // write struct end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 405 | default_write_struct_end(protocol); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 406 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 407 | return Qnil; |
| 408 | } |
| 409 | |
| 410 | //------------------------------------------- |
| 411 | // Reading section |
| 412 | //------------------------------------------- |
| 413 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 414 | static VALUE rb_thrift_union_read(VALUE self, VALUE protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 415 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol); |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 416 | static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size); |
| 417 | static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 418 | |
| 419 | static void set_field_value(VALUE obj, VALUE field_name, VALUE value) { |
| Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 420 | char name_buf[RSTRING_LEN(field_name) + 2]; |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 421 | |
| 422 | name_buf[0] = '@'; |
| Bryan Duxbury | bcbf6d6 | 2011-10-24 17:29:16 +0000 | [diff] [blame] | 423 | strlcpy(&name_buf[1], RSTRING_PTR(field_name), RSTRING_LEN(field_name)+1); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 424 | |
| 425 | rb_ivar_set(obj, rb_intern(name_buf), value); |
| 426 | } |
| 427 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 428 | // Helper method to skip the contents of a map (assumes the map header has been read). |
| 429 | static void skip_map_contents(VALUE protocol, VALUE key_type_value, VALUE value_type_value, int size) { |
| 430 | int i; |
| 431 | for (i = 0; i < size; i++) { |
| 432 | rb_funcall(protocol, skip_method_id, 1, key_type_value); |
| 433 | rb_funcall(protocol, skip_method_id, 1, value_type_value); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | // Helper method to skip the contents of a list or set (assumes the list/set header has been read). |
| 438 | static void skip_list_or_set_contents(VALUE protocol, VALUE element_type_value, int size) { |
| 439 | int i; |
| 440 | for (i = 0; i < size; i++) { |
| 441 | rb_funcall(protocol, skip_method_id, 1, element_type_value); |
| 442 | } |
| 443 | } |
| 444 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 445 | static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { |
| 446 | VALUE result = Qnil; |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 447 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 448 | if (ttype == TTYPE_BOOL) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 449 | result = default_read_bool(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 450 | } else if (ttype == TTYPE_BYTE) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 451 | result = default_read_byte(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 452 | } else if (ttype == TTYPE_I16) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 453 | result = default_read_i16(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 454 | } else if (ttype == TTYPE_I32) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 455 | result = default_read_i32(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 456 | } else if (ttype == TTYPE_I64) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 457 | result = default_read_i64(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 458 | } else if (ttype == TTYPE_STRING) { |
| henrique | 8a2bab3 | 2014-07-16 20:10:57 +0200 | [diff] [blame] | 459 | VALUE is_binary = rb_hash_aref(field_info, binary_sym); |
| 460 | if (is_binary != Qtrue) { |
| 461 | result = default_read_string(protocol); |
| 462 | } else { |
| 463 | result = default_read_binary(protocol); |
| 464 | } |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 465 | } else if (ttype == TTYPE_DOUBLE) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 466 | result = default_read_double(protocol); |
| Dmytro Shteflyuk | e9ac8e3 | 2025-11-19 23:33:23 -0500 | [diff] [blame] | 467 | } else if (ttype == TTYPE_UUID) { |
| 468 | result = default_read_uuid(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 469 | } else if (ttype == TTYPE_STRUCT) { |
| 470 | VALUE klass = rb_hash_aref(field_info, class_sym); |
| 471 | result = rb_class_new_instance(0, NULL, klass); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 472 | |
| 473 | if (rb_obj_is_kind_of(result, thrift_union_class)) { |
| 474 | rb_thrift_union_read(result, protocol); |
| 475 | } else { |
| 476 | rb_thrift_struct_read(result, protocol); |
| 477 | } |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 478 | } else if (ttype == TTYPE_MAP) { |
| 479 | int i; |
| 480 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 481 | VALUE map_header = default_read_map_begin(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 482 | int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); |
| 483 | int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); |
| 484 | int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 485 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 486 | // Check the declared key and value types against the expected ones and skip the map contents |
| 487 | // if the types don't match. |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 488 | VALUE key_info = rb_hash_aref(field_info, key_sym); |
| 489 | VALUE value_info = rb_hash_aref(field_info, value_sym); |
| 490 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 491 | if (!NIL_P(key_info) && !NIL_P(value_info)) { |
| 492 | int specified_key_type = FIX2INT(rb_hash_aref(key_info, type_sym)); |
| 493 | int specified_value_type = FIX2INT(rb_hash_aref(value_info, type_sym)); |
| Bryan Duxbury | e80a194 | 2011-09-20 18:45:56 +0000 | [diff] [blame] | 494 | if (num_entries == 0 || (specified_key_type == key_ttype && specified_value_type == value_ttype)) { |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 495 | result = rb_hash_new(); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 496 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 497 | for (i = 0; i < num_entries; ++i) { |
| 498 | VALUE key, val; |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 499 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 500 | key = read_anything(protocol, key_ttype, key_info); |
| 501 | val = read_anything(protocol, value_ttype, value_info); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 502 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 503 | rb_hash_aset(result, key, val); |
| 504 | } |
| 505 | } else { |
| 506 | skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); |
| 507 | } |
| 508 | } else { |
| 509 | skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 510 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 511 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 512 | default_read_map_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 513 | } else if (ttype == TTYPE_LIST) { |
| 514 | int i; |
| 515 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 516 | VALUE list_header = default_read_list_begin(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 517 | int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); |
| 518 | int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 519 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 520 | // Check the declared element type against the expected one and skip the list contents |
| 521 | // if the types don't match. |
| 522 | VALUE element_info = rb_hash_aref(field_info, element_sym); |
| 523 | if (!NIL_P(element_info)) { |
| 524 | int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); |
| 525 | if (specified_element_type == element_ttype) { |
| 526 | result = rb_ary_new2(num_elements); |
| 527 | |
| 528 | for (i = 0; i < num_elements; ++i) { |
| 529 | rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 530 | } |
| 531 | } else { |
| 532 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| 533 | } |
| 534 | } else { |
| 535 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 536 | } |
| 537 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 538 | default_read_list_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 539 | } else if (ttype == TTYPE_SET) { |
| 540 | VALUE items; |
| 541 | int i; |
| 542 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 543 | VALUE set_header = default_read_set_begin(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 544 | int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); |
| 545 | int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 546 | |
| Bryan Duxbury | 911d2f1 | 2011-05-31 20:03:29 +0000 | [diff] [blame] | 547 | // Check the declared element type against the expected one and skip the set contents |
| 548 | // if the types don't match. |
| 549 | VALUE element_info = rb_hash_aref(field_info, element_sym); |
| 550 | if (!NIL_P(element_info)) { |
| 551 | int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); |
| 552 | if (specified_element_type == element_ttype) { |
| 553 | items = rb_ary_new2(num_elements); |
| 554 | |
| 555 | for (i = 0; i < num_elements; ++i) { |
| 556 | rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); |
| 557 | } |
| 558 | |
| 559 | result = rb_class_new_instance(1, &items, rb_cSet); |
| 560 | } else { |
| 561 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| 562 | } |
| 563 | } else { |
| 564 | skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 565 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 566 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 567 | default_read_set_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 568 | } else { |
| 569 | rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); |
| 570 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 571 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 572 | return result; |
| 573 | } |
| 574 | |
| 575 | static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 576 | // read struct begin |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 577 | default_read_struct_begin(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 578 | |
| 579 | VALUE struct_fields = STRUCT_FIELDS(self); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 580 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 581 | // read each field |
| 582 | while (true) { |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 583 | VALUE field_header = default_read_field_begin(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 584 | VALUE field_type_value = rb_ary_entry(field_header, 1); |
| 585 | int field_type = FIX2INT(field_type_value); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 586 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 587 | if (field_type == TTYPE_STOP) { |
| 588 | break; |
| 589 | } |
| 590 | |
| 591 | // make sure we got a type we expected |
| 592 | VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2)); |
| 593 | |
| 594 | if (!NIL_P(field_info)) { |
| 595 | int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym)); |
| 596 | if (field_type == specified_type) { |
| 597 | // read the value |
| 598 | VALUE name = rb_hash_aref(field_info, name_sym); |
| 599 | set_field_value(self, name, read_anything(protocol, field_type, field_info)); |
| 600 | } else { |
| 601 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 602 | } |
| 603 | } else { |
| 604 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 605 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 606 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 607 | // read field end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 608 | default_read_field_end(protocol); |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 609 | } |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 610 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 611 | // read struct end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 612 | default_read_struct_end(protocol); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 613 | |
| Bryan Duxbury | 834895d | 2009-10-15 01:20:34 +0000 | [diff] [blame] | 614 | // call validate |
| 615 | rb_funcall(self, validate_method_id, 0); |
| 616 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 617 | return Qnil; |
| 618 | } |
| 619 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 620 | |
| 621 | // -------------------------------- |
| 622 | // Union section |
| 623 | // -------------------------------- |
| 624 | |
| 625 | static VALUE rb_thrift_union_read(VALUE self, VALUE protocol) { |
| 626 | // read struct begin |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 627 | default_read_struct_begin(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 628 | |
| 629 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 630 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 631 | VALUE field_header = default_read_field_begin(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 632 | VALUE field_type_value = rb_ary_entry(field_header, 1); |
| 633 | int field_type = FIX2INT(field_type_value); |
| 634 | |
| 635 | // make sure we got a type we expected |
| 636 | VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2)); |
| 637 | |
| 638 | if (!NIL_P(field_info)) { |
| 639 | int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym)); |
| 640 | if (field_type == specified_type) { |
| 641 | // read the value |
| 642 | VALUE name = rb_hash_aref(field_info, name_sym); |
| Roger Meier | 0240572 | 2014-01-12 23:29:11 +0100 | [diff] [blame] | 643 | rb_iv_set(self, "@setfield", rb_str_intern(name)); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 644 | rb_iv_set(self, "@value", read_anything(protocol, field_type, field_info)); |
| 645 | } else { |
| 646 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 647 | } |
| 648 | } else { |
| 649 | rb_funcall(protocol, skip_method_id, 1, field_type_value); |
| 650 | } |
| 651 | |
| 652 | // read field end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 653 | default_read_field_end(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 654 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 655 | field_header = default_read_field_begin(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 656 | field_type_value = rb_ary_entry(field_header, 1); |
| 657 | field_type = FIX2INT(field_type_value); |
| 658 | |
| 659 | if (field_type != TTYPE_STOP) { |
| Dmytro Shteflyuk | 32776c0 | 2026-02-10 12:25:07 -0500 | [diff] [blame^] | 660 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("too many fields in union!"))); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 661 | } |
| 662 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 663 | // read struct end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 664 | default_read_struct_end(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 665 | |
| 666 | // call validate |
| 667 | rb_funcall(self, validate_method_id, 0); |
| 668 | |
| 669 | return Qnil; |
| 670 | } |
| 671 | |
| 672 | static VALUE rb_thrift_union_write(VALUE self, VALUE protocol) { |
| 673 | // call validate |
| 674 | rb_funcall(self, validate_method_id, 0); |
| 675 | |
| 676 | // write struct begin |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 677 | default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 678 | |
| 679 | VALUE struct_fields = STRUCT_FIELDS(self); |
| 680 | |
| 681 | VALUE setfield = rb_ivar_get(self, setfield_id); |
| 682 | VALUE setvalue = rb_ivar_get(self, setvalue_id); |
| 683 | VALUE field_id = rb_funcall(self, name_to_id_method_id, 1, rb_funcall(setfield, to_s_method_id, 0)); |
| 684 | |
| 685 | VALUE field_info = rb_hash_aref(struct_fields, field_id); |
| 686 | |
| Joe Ennever | 5b15f8c | 2015-08-31 19:20:36 +0000 | [diff] [blame] | 687 | if(NIL_P(field_info)) { |
| Dmytro Shteflyuk | 32776c0 | 2026-02-10 12:25:07 -0500 | [diff] [blame^] | 688 | rb_exc_raise(get_protocol_exception(INT2FIX(PROTOERR_INVALID_DATA), rb_str_new2("set_field is not valid for this union!"))); |
| Joe Ennever | 5b15f8c | 2015-08-31 19:20:36 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 691 | VALUE ttype_value = rb_hash_aref(field_info, type_sym); |
| 692 | int ttype = FIX2INT(ttype_value); |
| 693 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 694 | default_write_field_begin(protocol, setfield, ttype_value, field_id); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 695 | |
| 696 | write_anything(ttype, setvalue, protocol, field_info); |
| 697 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 698 | default_write_field_end(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 699 | |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 700 | default_write_field_stop(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 701 | |
| 702 | // write struct end |
| Bryan Duxbury | d2cc5bb | 2010-07-28 21:00:06 +0000 | [diff] [blame] | 703 | default_write_struct_end(protocol); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 704 | |
| 705 | return Qnil; |
| 706 | } |
| 707 | |
| Dmytro Shteflyuk | 0d18fb2 | 2025-12-20 12:13:46 -0500 | [diff] [blame] | 708 | void Init_struct(void) { |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 709 | VALUE struct_module = rb_const_get(thrift_module, rb_intern("Struct")); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 710 | |
| Bryan Duxbury | c016628 | 2009-02-02 00:48:17 +0000 | [diff] [blame] | 711 | rb_define_method(struct_module, "write", rb_thrift_struct_write, 1); |
| 712 | rb_define_method(struct_module, "read", rb_thrift_struct_read, 1); |
| Bryan Duxbury | ccae884 | 2009-07-31 18:47:09 +0000 | [diff] [blame] | 713 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 714 | thrift_union_class = rb_const_get(thrift_module, rb_intern("Union")); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 715 | rb_global_variable(&thrift_union_class); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 716 | |
| 717 | rb_define_method(thrift_union_class, "write", rb_thrift_union_write, 1); |
| 718 | rb_define_method(thrift_union_class, "read", rb_thrift_union_read, 1); |
| 719 | |
| 720 | setfield_id = rb_intern("@setfield"); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 721 | rb_global_variable(&setfield_id); |
| 722 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 723 | setvalue_id = rb_intern("@value"); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 724 | rb_global_variable(&setvalue_id); |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 725 | |
| 726 | to_s_method_id = rb_intern("to_s"); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 727 | rb_global_variable(&to_s_method_id); |
| 728 | |
| Bryan Duxbury | 33e190c | 2010-02-16 21:19:01 +0000 | [diff] [blame] | 729 | name_to_id_method_id = rb_intern("name_to_id"); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 730 | rb_global_variable(&name_to_id_method_id); |
| 731 | |
| Bryan Duxbury | d1df20a | 2011-06-15 20:52:57 +0000 | [diff] [blame] | 732 | sorted_field_ids_method_id = rb_intern("sorted_field_ids"); |
| Stan Hu | cc70b4e | 2021-03-11 03:49:57 +0530 | [diff] [blame] | 733 | rb_global_variable(&sorted_field_ids_method_id); |
| Bryan Duxbury | 65073e5 | 2010-02-23 15:46:46 +0000 | [diff] [blame] | 734 | } |