예제 #1
0
파일: struct.c 프로젝트: ggPeti/thrift
static void write_anything(VALUE value, VALUE protocol, field_metadata* fmd, protocol_method_table *pmt) {
  DEBUG_FUNCTION_ENTRY();

  if (fmd->type == TTYPE_BOOL) {
    fastcall_call(pmt->write_bool, protocol, value);
  } else if (fmd->type == TTYPE_BYTE) {
    fastcall_call(pmt->write_byte, protocol, value);
  } else if (fmd->type == TTYPE_I16) {
    fastcall_call(pmt->write_i16, protocol, value);
  } else if (fmd->type == TTYPE_I32) {
    fastcall_call(pmt->write_i32, protocol, value);
  } else if (fmd->type == TTYPE_I64) {
    fastcall_call(pmt->write_i64, protocol, value);
  } else if (fmd->type == TTYPE_DOUBLE) {
    fastcall_call(pmt->write_double, protocol, value);
  } else if (fmd->type == TTYPE_STRING) {
    fastcall_call(pmt->write_string, protocol, value);
  } else if (IS_CONTAINER(fmd->type)) {
    write_container(fmd, value, protocol, pmt);
  } else if (fmd->type == TTYPE_STRUCT) {
    if (rb_obj_is_kind_of(value, thrift_union_class)) {
      union_write(value, protocol, pmt);
    } else {
      struct_write(value, protocol, pmt);
    }
  } else {
    rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", fmd->type);
  }

  DEBUG_FUNCTION_EXIT();
}
예제 #2
0
static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_info) {
  if (ttype == TTYPE_BOOL) {
    default_write_bool(protocol, value);
  } else if (ttype == TTYPE_BYTE) {
    default_write_byte(protocol, value);
  } else if (ttype == TTYPE_I16) {
    default_write_i16(protocol, value);
  } else if (ttype == TTYPE_I32) {
    default_write_i32(protocol, value);
  } else if (ttype == TTYPE_I64) {
    default_write_i64(protocol, value);
  } else if (ttype == TTYPE_DOUBLE) {
    default_write_double(protocol, value);
  } else if (ttype == TTYPE_STRING) {
    default_write_string(protocol, value);
  } else if (IS_CONTAINER(ttype)) {
    write_container(ttype, field_info, value, protocol);
  } else if (ttype == TTYPE_STRUCT) {
    if (rb_obj_is_kind_of(value, thrift_union_class)) {
      rb_thrift_union_write(value, protocol);
    } else {
      rb_thrift_struct_write(value, protocol);
    }
  } else {
    rb_raise(rb_eNotImpError, "Unknown type for binary_encoding: %d", ttype);
  }
}
예제 #3
0
파일: struct.c 프로젝트: crnt/thrift-1
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);
  }
}