static VALUE union_write(VALUE self, VALUE protocol, protocol_method_table *pmt) { DEBUG_FUNCTION_ENTRY(); // call validate rb_funcall(self, validate_method_id, 0); // write struct begin fastcall_call(pmt->write_struct_begin, protocol, rb_class_name(CLASS_OF(self))); struct_metadata* md = getStructMetadata(CLASS_OF(self)); VALUE setfield = rb_ivar_get(self, setfield_id); VALUE setvalue = rb_ivar_get(self, setvalue_id); field_metadata* fmd = getFieldMetadataByName(md, RSTRING_PTR(rb_funcall(setfield, to_s_method_id, 0))); int ttype = fmd->type; int field_id = fmd->id; fastcall_call(pmt->write_field_begin, protocol, setfield, INT2NUM(ttype), INT2NUM(field_id)); write_anything(setvalue, protocol, fmd, pmt); fastcall_call(pmt->write_field_end, protocol, Qnil); fastcall_call(pmt->write_field_stop, protocol, Qnil); // write struct end fastcall_call(pmt->write_struct_end, protocol, Qnil); fastcall_call(pmt->flush, protocol, Qnil); DEBUG_FUNCTION_EXIT(); return Qnil; }
static VALUE rb_thrift_union_write(VALUE self, VALUE protocol) { // call validate rb_funcall(self, validate_method_id, 0); // write struct begin default_write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); VALUE struct_fields = STRUCT_FIELDS(self); VALUE setfield = rb_ivar_get(self, setfield_id); VALUE setvalue = rb_ivar_get(self, setvalue_id); VALUE field_id = rb_funcall(self, name_to_id_method_id, 1, rb_funcall(setfield, to_s_method_id, 0)); VALUE field_info = rb_hash_aref(struct_fields, field_id); VALUE ttype_value = rb_hash_aref(field_info, type_sym); int ttype = FIX2INT(ttype_value); default_write_field_begin(protocol, setfield, ttype_value, field_id); write_anything(ttype, setvalue, protocol, field_info); default_write_field_end(protocol); default_write_field_stop(protocol); // write struct end default_write_struct_end(protocol); return Qnil; }
static VALUE struct_write(VALUE self, VALUE protocol, protocol_method_table* pmt) { DEBUG_FUNCTION_ENTRY(); // call validate rb_funcall(self, validate_method_id, 0); // write struct begin fastcall_call(pmt->write_struct_begin, protocol, rb_class_name(CLASS_OF(self))); // iterate through all the fields here struct_metadata* md = getStructMetadata(CLASS_OF(self)); int i = 0; for (i=0; i < getMetadataFieldCount(md); i++) { field_metadata* fmd = getFieldMetadataByIndex(md, i); DEBUGF("name=%s", fmd->name); VALUE field_value = rb_ivar_get(self, fmd->name_id); VALUE field_name = rb_str_new_cstr(fmd->name); DEBUGF("type=%d", TYPE(field_value)); DEBUG_FUNCTION_PROGRESS(); if (!NIL_P(field_value)) { DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->write_field_begin, protocol, field_name, INT2NUM(fmd->type), INT2NUM(fmd->id)); DEBUG_FUNCTION_PROGRESS(); write_anything(field_value, protocol, fmd, pmt); DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->write_field_end, protocol, Qnil); DEBUG_FUNCTION_PROGRESS(); } } DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->write_field_stop, protocol, Qnil); DEBUG_FUNCTION_PROGRESS(); // write struct end fastcall_call(pmt->write_struct_end, protocol, Qnil); DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->flush, protocol, Qnil); DEBUG_FUNCTION_EXIT(); return Qnil; }
static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { // call validate rb_funcall(self, validate_method_id, 0); // check_native_proto_method_table(protocol); // write struct begin mt->write_struct_begin(protocol, rb_class_name(CLASS_OF(self))); // iterate through all the fields here VALUE struct_fields = STRUCT_FIELDS(self); VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0); VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0); int i = 0; for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) { VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i); VALUE field_info = rb_hash_aref(struct_fields, field_id); VALUE ttype_value = rb_hash_aref(field_info, type_sym); int ttype = FIX2INT(ttype_value); VALUE field_name = rb_hash_aref(field_info, name_sym); VALUE field_value = get_field_value(self, field_name); if (!NIL_P(field_value)) { mt->write_field_begin(protocol, field_name, ttype_value, field_id); write_anything(ttype, field_value, protocol, field_info); mt->write_field_end(protocol); } } mt->write_field_stop(protocol); // write struct end mt->write_struct_end(protocol); return Qnil; }
static void write_container(field_metadata *fmd, VALUE value, VALUE protocol, protocol_method_table* pmt) { int sz, i; DEBUG_FUNCTION_ENTRY(); if (fmd->type == TTYPE_MAP) { DEBUG_FUNCTION_PROGRESS(); VALUE keys; VALUE key; VALUE val; Check_Type(value, T_HASH); field_metadata* key_md = fmd->key; int keytype = key_md->type; field_metadata* value_md = fmd->value; int valuetype = value_md->type; keys = rb_funcall(value, keys_method_id, 0); sz = RARRAY_LEN(keys); fastcall_call(pmt->write_map_begin, protocol, INT2FIX(keytype), INT2FIX(valuetype), INT2FIX(sz)); for (i = 0; i < sz; i++) { key = rb_ary_entry(keys, i); val = rb_hash_aref(value, key); write_anything(key, protocol, key_md, pmt); write_anything(val, protocol, value_md, pmt); } fastcall_call(pmt->write_map_end, protocol, Qnil); } else if (fmd->type == TTYPE_LIST) { DEBUG_FUNCTION_PROGRESS(); Check_Type(value, T_ARRAY); DEBUG_FUNCTION_PROGRESS(); sz = RARRAY_LEN(value); DEBUG_FUNCTION_PROGRESS(); field_metadata* element_md = fmd->element; int elementtype = element_md->type; DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->write_list_begin, protocol, INT2FIX(elementtype), INT2FIX(sz)); DEBUG_FUNCTION_PROGRESS(); for (i = 0; i < sz; ++i) { DEBUG_FUNCTION_PROGRESS(); VALUE val = rb_ary_entry(value, i); DEBUG_FUNCTION_PROGRESS(); write_anything(val, protocol, element_md, pmt); DEBUG_FUNCTION_PROGRESS(); } DEBUG_FUNCTION_PROGRESS(); fastcall_call(pmt->write_list_end, protocol, Qnil); } else if (fmd->type == TTYPE_SET) { DEBUG_FUNCTION_PROGRESS(); VALUE items; if (TYPE(value) == T_ARRAY) { items = value; } else { if (rb_cSet == CLASS_OF(value)) { items = rb_funcall(value, entries_method_id, 0); } else { Check_Type(value, T_HASH); items = rb_funcall(value, keys_method_id, 0); } } sz = RARRAY_LEN(items); field_metadata* element_md = fmd->element; int elementtype = element_md->type; fastcall_call(pmt->write_set_begin, protocol, INT2FIX(elementtype), INT2FIX(sz)); for (i = 0; i < sz; i++) { VALUE val = rb_ary_entry(items, i); write_anything(val, protocol, element_md, pmt); } fastcall_call(pmt->write_set_end, protocol, Qnil); } else { rb_raise(rb_eNotImpError, "can't write container of type: %d", fmd->type); } DEBUG_FUNCTION_EXIT(); }
static void write_container(int ttype, VALUE field_info, VALUE value, VALUE protocol) { int sz, i; if (ttype == TTYPE_MAP) { VALUE keys; VALUE key; VALUE val; Check_Type(value, T_HASH); VALUE key_info = rb_hash_aref(field_info, key_sym); VALUE keytype_value = rb_hash_aref(key_info, type_sym); int keytype = FIX2INT(keytype_value); VALUE value_info = rb_hash_aref(field_info, value_sym); VALUE valuetype_value = rb_hash_aref(value_info, type_sym); int valuetype = FIX2INT(valuetype_value); keys = rb_funcall(value, keys_method_id, 0); sz = RARRAY_LEN(keys); mt->write_map_begin(protocol, keytype_value, valuetype_value, INT2FIX(sz)); for (i = 0; i < sz; i++) { key = rb_ary_entry(keys, i); val = rb_hash_aref(value, key); if (IS_CONTAINER(keytype)) { write_container(keytype, key_info, key, protocol); } else { write_anything(keytype, key, protocol, key_info); } if (IS_CONTAINER(valuetype)) { write_container(valuetype, value_info, val, protocol); } else { write_anything(valuetype, val, protocol, value_info); } } mt->write_map_end(protocol); } else if (ttype == TTYPE_LIST) { Check_Type(value, T_ARRAY); sz = RARRAY_LEN(value); VALUE element_type_info = rb_hash_aref(field_info, element_sym); VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); int element_type = FIX2INT(element_type_value); mt->write_list_begin(protocol, element_type_value, INT2FIX(sz)); for (i = 0; i < sz; ++i) { VALUE val = rb_ary_entry(value, i); if (IS_CONTAINER(element_type)) { write_container(element_type, element_type_info, val, protocol); } else { write_anything(element_type, val, protocol, element_type_info); } } mt->write_list_end(protocol); } else if (ttype == TTYPE_SET) { VALUE items; if (TYPE(value) == T_ARRAY) { items = value; } else { if (rb_cSet == CLASS_OF(value)) { items = rb_funcall(value, entries_method_id, 0); } else { Check_Type(value, T_HASH); items = rb_funcall(value, keys_method_id, 0); } } sz = RARRAY_LEN(items); VALUE element_type_info = rb_hash_aref(field_info, element_sym); VALUE element_type_value = rb_hash_aref(element_type_info, type_sym); int element_type = FIX2INT(element_type_value); mt->write_set_begin(protocol, element_type_value, INT2FIX(sz)); for (i = 0; i < sz; i++) { VALUE val = rb_ary_entry(items, i); if (IS_CONTAINER(element_type)) { write_container(element_type, element_type_info, val, protocol); } else { write_anything(element_type, val, protocol, element_type_info); } } mt->write_set_end(protocol); } else { rb_raise(rb_eNotImpError, "can't write container of type: %d", ttype); } }