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

  // read struct begin
  fastcall_call(pmt->read_struct_begin, protocol, Qnil);

  struct_metadata* md = getStructMetadata(CLASS_OF(self));

  VALUE field_header = fastcall_call(pmt->read_field_begin, protocol, Qnil);

  VALUE field_type_value = rb_ary_entry(field_header, 1);
  VALUE field_id_value = rb_ary_entry(field_header, 2);

  int field_type = FIX2INT(field_type_value);
  int field_id = FIX2INT(field_id_value);


  field_metadata *fmd = getFieldMetadataByID(md, field_id);

  // make sure we got a type we expected
  if (fmd) {
    int specified_type = fmd->type;
    if (field_type == specified_type) {
      // read the value
      char* name = fmd->name;
      rb_iv_set(self, "@setfield", ID2SYM(rb_intern(name)));
      rb_iv_set(self, "@value", read_anything(protocol, fmd, pmt));
    } else {
      rb_funcall(protocol, skip_method_id, 1, field_type_value);
    }
  } else {
    rb_funcall(protocol, skip_method_id, 1, field_type_value);
  }

  // read field end
  fastcall_call(pmt->read_field_end, protocol, Qnil);

  field_header = fastcall_call(pmt->read_field_begin, protocol, Qnil);
  field_type_value = rb_ary_entry(field_header, 1);
  field_type = FIX2INT(field_type_value);

  if (field_type != TTYPE_STOP) {
    rb_raise(rb_eRuntimeError, "too many fields in union!");
  }

  // read struct end
  fastcall_call(pmt->read_struct_end, protocol, Qnil);

  // call validate
  rb_funcall(self, validate_method_id, 0);

  DEBUG_FUNCTION_EXIT();
  return Qnil;
}
예제 #2
0
static VALUE rb_thrift_union_read(VALUE self, VALUE protocol) {
  // read struct begin
  mt->read_struct_begin(protocol);

  VALUE struct_fields = STRUCT_FIELDS(self);

  VALUE field_header = mt->read_field_begin(protocol);
  VALUE field_type_value = rb_ary_entry(field_header, 1);
  int field_type = FIX2INT(field_type_value);

  // make sure we got a type we expected
  VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));

  if (!NIL_P(field_info)) {
    int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
    if (field_type == specified_type) {
      // read the value
      VALUE name = rb_hash_aref(field_info, name_sym);
      rb_iv_set(self, "@setfield", ID2SYM(rb_intern(RSTRING_PTR(name))));
      rb_iv_set(self, "@value", read_anything(protocol, field_type, field_info));
    } else {
      rb_funcall(protocol, skip_method_id, 1, field_type_value);
    }
  } else {
    rb_funcall(protocol, skip_method_id, 1, field_type_value);
  }

  // read field end
  mt->read_field_end(protocol);

  field_header = mt->read_field_begin(protocol);
  field_type_value = rb_ary_entry(field_header, 1);
  field_type = FIX2INT(field_type_value);

  if (field_type != TTYPE_STOP) {
    rb_raise(rb_eRuntimeError, "too many fields in union!");
  }

  // read field end
  mt->read_field_end(protocol);

  // read struct end
  mt->read_struct_end(protocol);

  // call validate
  rb_funcall(self, validate_method_id, 0);

  return Qnil;
}
예제 #3
0
파일: struct.c 프로젝트: ggPeti/thrift
static VALUE struct_read(VALUE self, VALUE protocol, protocol_method_table *pmt)
{
  // read struct begin
  fastcall_call(pmt->read_struct_begin, protocol, Qnil);

  struct_metadata* md = getStructMetadata(CLASS_OF(self));

  // read each field
  while (true) {
    VALUE field_header = fastcall_call(pmt->read_field_begin, protocol, Qnil);

    VALUE field_type_value = rb_ary_entry(field_header, 1);
    VALUE field_id_value = rb_ary_entry(field_header, 2);

    int field_type = FIX2INT(field_type_value);
    if (field_type == TTYPE_STOP) {
      break;
    }


    // make sure we got a type we expected
    int field_id = FIX2INT(field_id_value);
    field_metadata *fmd = getFieldMetadataByID(md, field_id);

    if (fmd) {
      int specified_type = fmd->type;
      if (field_type == specified_type) {
        // read the value
        rb_ivar_set(self, fmd->name_id, read_anything(protocol, fmd, pmt));
      } else {
        rb_funcall(protocol, skip_method_id, 1, field_type_value);
      }
    } else {
      rb_funcall(protocol, skip_method_id, 1, field_type_value);
    }

    // read field end
    fastcall_call(pmt->read_field_end, protocol, Qnil);
  }

  // read struct end
  fastcall_call(pmt->read_struct_end, protocol, Qnil);

  // call validate
  rb_funcall(self, validate_method_id, 0);

  return Qnil;
}
예제 #4
0
static VALUE rb_thrift_struct_read(VALUE self, VALUE protocol) {
  // check_native_proto_method_table(protocol);

  // read struct begin
  mt->read_struct_begin(protocol);

  VALUE struct_fields = STRUCT_FIELDS(self);

  // read each field
  while (true) {
    VALUE field_header = mt->read_field_begin(protocol);
    VALUE field_type_value = rb_ary_entry(field_header, 1);
    int field_type = FIX2INT(field_type_value);

    if (field_type == TTYPE_STOP) {
      break;
    }

    // make sure we got a type we expected
    VALUE field_info = rb_hash_aref(struct_fields, rb_ary_entry(field_header, 2));

    if (!NIL_P(field_info)) {
      int specified_type = FIX2INT(rb_hash_aref(field_info, type_sym));
      if (field_type == specified_type) {
        // read the value
        VALUE name = rb_hash_aref(field_info, name_sym);
        set_field_value(self, name, read_anything(protocol, field_type, field_info));
      } else {
        rb_funcall(protocol, skip_method_id, 1, field_type_value);
      }
    } else {
      rb_funcall(protocol, skip_method_id, 1, field_type_value);
    }

    // read field end
    mt->read_field_end(protocol);
  }

  // read struct end
  mt->read_struct_end(protocol);

  // call validate
  rb_funcall(self, validate_method_id, 0);

  return Qnil;
}
예제 #5
0
파일: struct.c 프로젝트: ggPeti/thrift
static VALUE read_anything(VALUE protocol, field_metadata* fmd, protocol_method_table *pmt) {
  VALUE result = Qnil;

  if (fmd->type == TTYPE_BOOL) {
    result = fastcall_call(pmt->read_bool, protocol, Qnil);
  } else if (fmd->type == TTYPE_BYTE) {
    result = fastcall_call(pmt->read_byte, protocol, Qnil);
  } else if (fmd->type == TTYPE_I16) {
    result = fastcall_call(pmt->read_i16, protocol, Qnil);
  } else if (fmd->type == TTYPE_I32) {
    result = fastcall_call(pmt->read_i32, protocol, Qnil);
  } else if (fmd->type == TTYPE_I64) {
    result = fastcall_call(pmt->read_i64, protocol, Qnil);
  } else if (fmd->type == TTYPE_STRING) {
    result = fastcall_call(pmt->read_string, protocol, Qnil);
  } else if (fmd->type == TTYPE_DOUBLE) {
    result = fastcall_call(pmt->read_double, protocol, Qnil);
  } else if (fmd->type == TTYPE_STRUCT) {

    result = rb_class_new_instance(0, NULL, fmd->klass_v);

    if (rb_obj_is_kind_of(result, thrift_union_class)) {
      union_read(result, protocol, pmt);
    } else {
      struct_read(result, protocol, pmt);
    }
  } else if (fmd->type == TTYPE_MAP) {
    int i;

    VALUE map_header = fastcall_call(pmt->read_map_begin, protocol, Qnil);
    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.
    field_metadata* key_md = fmd->key;
    field_metadata* value_md = fmd->value;

    if (key_md && value_md) {
      int specified_key_type = key_md->type;
      int specified_value_type = value_md->type;
      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_md, pmt);
          val = read_anything(protocol, value_md, pmt);

          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);
    }

    fastcall_call(pmt->read_map_end, protocol, Qnil);
  } else if (fmd->type == TTYPE_LIST) {
    int i;

    VALUE list_header = fastcall_call(pmt->read_list_begin, protocol, Qnil);
    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.
    field_metadata* element_md = fmd->element;

    if (element_md) {
      int specified_element_type = element_md->type;
      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_md, pmt));
        }
      } else {
        skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
      }
    } else {
      skip_list_or_set_contents(protocol, INT2FIX(element_ttype), num_elements);
    }

    fastcall_call(pmt->read_list_end, protocol, Qnil);
  } else if (fmd->type == TTYPE_SET) {
    VALUE items;
    int i;

    VALUE set_header = fastcall_call(pmt->read_set_begin, protocol, Qnil);
    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.
    field_metadata* element_md = fmd->element;
    if (element_md) {
      int specified_element_type = element_md->type;
      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_md, pmt));
        }

        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);
    }

    fastcall_call(pmt->read_set_end, protocol, Qnil);
  } else {
    rb_raise(rb_eNotImpError, "read_anything not implemented for type %d!", fmd->type);
  }

  return result;
}
예제 #6
0
파일: struct.c 프로젝트: crnt/thrift-1
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);
    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;
}
예제 #7
0
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;
}