static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { VALUE result = Qnil; if (ttype == TTYPE_BOOL) { result = default_read_bool(protocol); } else if (ttype == TTYPE_BYTE) { result = default_read_byte(protocol); } else if (ttype == TTYPE_I16) { result = default_read_i16(protocol); } else if (ttype == TTYPE_I32) { result = default_read_i32(protocol); } else if (ttype == TTYPE_I64) { result = default_read_i64(protocol); } else if (ttype == TTYPE_STRING) { result = default_read_string(protocol); } else if (ttype == TTYPE_DOUBLE) { result = default_read_double(protocol); } else if (ttype == TTYPE_STRUCT) { VALUE klass = rb_hash_aref(field_info, class_sym); result = rb_class_new_instance(0, NULL, klass); if (rb_obj_is_kind_of(result, thrift_union_class)) { rb_thrift_union_read(result, protocol); } else { rb_thrift_struct_read(result, protocol); } } else if (ttype == TTYPE_MAP) { int i; VALUE map_header = default_read_map_begin(protocol); int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); // Check the declared key and value types against the expected ones and skip the map contents // if the types don't match. VALUE key_info = rb_hash_aref(field_info, key_sym); VALUE value_info = rb_hash_aref(field_info, value_sym); if (!NIL_P(key_info) && !NIL_P(value_info)) { int specified_key_type = FIX2INT(rb_hash_aref(key_info, type_sym)); int specified_value_type = FIX2INT(rb_hash_aref(value_info, type_sym)); if (num_entries == 0 || (specified_key_type == key_ttype && specified_value_type == value_ttype)) { result = rb_hash_new(); for (i = 0; i < num_entries; ++i) { VALUE key, val; key = read_anything(protocol, key_ttype, key_info); val = read_anything(protocol, value_ttype, value_info); rb_hash_aset(result, key, val); } } else { skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); } } else { skip_map_contents(protocol, INT2FIX(key_ttype), INT2FIX(value_ttype), num_entries); } default_read_map_end(protocol); } else if (ttype == TTYPE_LIST) { int i; VALUE list_header = default_read_list_begin(protocol); int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); // Check the declared element type against the expected one and skip the list contents // if the types don't match. VALUE element_info = rb_hash_aref(field_info, element_sym); if (!NIL_P(element_info)) { int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); if (specified_element_type == element_ttype) { result = rb_ary_new2(num_elements); for (i = 0; i < num_elements; ++i) { rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } } else { skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); } } else { skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); } default_read_list_end(protocol); } else if (ttype == TTYPE_SET) { VALUE items; int i; VALUE set_header = default_read_set_begin(protocol); int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); // Check the declared element type against the expected one and skip the set contents // if the types don't match. VALUE element_info = rb_hash_aref(field_info, element_sym); if (!NIL_P(element_info)) { int specified_element_type = FIX2INT(rb_hash_aref(element_info, type_sym)); if (specified_element_type == element_ttype) { items = rb_ary_new2(num_elements); for (i = 0; i < num_elements; ++i) { rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } result = rb_class_new_instance(1, &items, rb_cSet); } else { skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); } } else { skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements); } default_read_set_end(protocol); } else { rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); } return result; }
static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) { VALUE result = Qnil; if (ttype == TTYPE_BOOL) { result = mt->read_bool(protocol); } else if (ttype == TTYPE_BYTE) { result = mt->read_byte(protocol); } else if (ttype == TTYPE_I16) { result = mt->read_i16(protocol); } else if (ttype == TTYPE_I32) { result = mt->read_i32(protocol); } else if (ttype == TTYPE_I64) { result = mt->read_i64(protocol); } else if (ttype == TTYPE_STRING) { result = mt->read_string(protocol); } else if (ttype == TTYPE_DOUBLE) { result = mt->read_double(protocol); } else if (ttype == TTYPE_STRUCT) { VALUE klass = rb_hash_aref(field_info, class_sym); result = rb_class_new_instance(0, NULL, klass); if (rb_obj_is_kind_of(result, thrift_union_class)) { rb_thrift_union_read(result, protocol); } else { rb_thrift_struct_read(result, protocol); } } else if (ttype == TTYPE_MAP) { int i; VALUE map_header = mt->read_map_begin(protocol); int key_ttype = FIX2INT(rb_ary_entry(map_header, 0)); int value_ttype = FIX2INT(rb_ary_entry(map_header, 1)); int num_entries = FIX2INT(rb_ary_entry(map_header, 2)); VALUE key_info = rb_hash_aref(field_info, key_sym); VALUE value_info = rb_hash_aref(field_info, value_sym); result = rb_hash_new(); for (i = 0; i < num_entries; ++i) { VALUE key, val; key = read_anything(protocol, key_ttype, key_info); val = read_anything(protocol, value_ttype, value_info); rb_hash_aset(result, key, val); } mt->read_map_end(protocol); } else if (ttype == TTYPE_LIST) { int i; VALUE list_header = mt->read_list_begin(protocol); int element_ttype = FIX2INT(rb_ary_entry(list_header, 0)); int num_elements = FIX2INT(rb_ary_entry(list_header, 1)); result = rb_ary_new2(num_elements); for (i = 0; i < num_elements; ++i) { rb_ary_push(result, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } mt->read_list_end(protocol); } else if (ttype == TTYPE_SET) { VALUE items; int i; VALUE set_header = mt->read_set_begin(protocol); int element_ttype = FIX2INT(rb_ary_entry(set_header, 0)); int num_elements = FIX2INT(rb_ary_entry(set_header, 1)); items = rb_ary_new2(num_elements); for (i = 0; i < num_elements; ++i) { rb_ary_push(items, read_anything(protocol, element_ttype, rb_hash_aref(field_info, element_sym))); } mt->read_set_end(protocol); result = rb_class_new_instance(1, &items, rb_cSet); } else { rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", ttype); } return result; }