THRIFT-1766 [Ruby] Provide support for binary types
Patch: Nathan Beyer
diff --git a/lib/rb/ext/binary_protocol_accelerated.c b/lib/rb/ext/binary_protocol_accelerated.c
index a8ebe7f..8b27dbc 100644
--- a/lib/rb/ext/binary_protocol_accelerated.c
+++ b/lib/rb/ext/binary_protocol_accelerated.c
@@ -79,7 +79,7 @@
static void write_string_direct(VALUE trans, VALUE str) {
if (TYPE(str) != T_STRING) {
- rb_raise(rb_eStandardError, "Value should be a string");
+ rb_raise(rb_eStandardError, "Value should be a string");
}
str = convert_to_utf8_byte_buffer(str);
write_i32_direct(trans, RSTRING_LEN(str));
@@ -219,11 +219,21 @@
return Qnil;
}
+VALUE rb_thrift_binary_proto_write_binary(VALUE self, VALUE buf) {
+ CHECK_NIL(buf);
+ VALUE trans = GET_TRANSPORT(self);
+ buf = force_binary_encoding(buf);
+ write_i32_direct(trans, RSTRING_LEN(buf));
+ rb_funcall(trans, write_method_id, 1, buf);
+ return Qnil;
+}
+
//---------------------------------------
// interface reading methods
//---------------------------------------
VALUE rb_thrift_binary_proto_read_string(VALUE self);
+VALUE rb_thrift_binary_proto_read_binary(VALUE self);
VALUE rb_thrift_binary_proto_read_byte(VALUE self);
VALUE rb_thrift_binary_proto_read_i32(VALUE self);
VALUE rb_thrift_binary_proto_read_i16(VALUE self);
@@ -381,11 +391,15 @@
}
VALUE rb_thrift_binary_proto_read_string(VALUE self) {
- int size = read_i32_direct(self);
- VALUE buffer = READ(self, size);
+ VALUE buffer = rb_thrift_binary_proto_read_binary(self);
return convert_to_string(buffer);
}
+VALUE rb_thrift_binary_proto_read_binary(VALUE self) {
+ int size = read_i32_direct(self);
+ return READ(self, size);
+}
+
void Init_binary_protocol_accelerated() {
VALUE thrift_binary_protocol_class = rb_const_get(thrift_module, rb_intern("BinaryProtocol"));
@@ -410,6 +424,7 @@
rb_define_method(bpa_class, "write_i64", rb_thrift_binary_proto_write_i64, 1);
rb_define_method(bpa_class, "write_double", rb_thrift_binary_proto_write_double, 1);
rb_define_method(bpa_class, "write_string", rb_thrift_binary_proto_write_string, 1);
+ rb_define_method(bpa_class, "write_binary", rb_thrift_binary_proto_write_binary, 1);
// unused methods
rb_define_method(bpa_class, "write_message_end", rb_thrift_binary_proto_write_message_end, 0);
rb_define_method(bpa_class, "write_struct_begin", rb_thrift_binary_proto_write_struct_begin, 1);
@@ -431,6 +446,7 @@
rb_define_method(bpa_class, "read_i64", rb_thrift_binary_proto_read_i64, 0);
rb_define_method(bpa_class, "read_double", rb_thrift_binary_proto_read_double, 0);
rb_define_method(bpa_class, "read_string", rb_thrift_binary_proto_read_string, 0);
+ rb_define_method(bpa_class, "read_binary", rb_thrift_binary_proto_read_binary, 0);
// unused methods
rb_define_method(bpa_class, "read_message_end", rb_thrift_binary_proto_read_message_end, 0);
rb_define_method(bpa_class, "read_struct_begin", rb_thift_binary_proto_read_struct_begin, 0);
diff --git a/lib/rb/ext/compact_protocol.c b/lib/rb/ext/compact_protocol.c
index 0c05481..1637e99 100644
--- a/lib/rb/ext/compact_protocol.c
+++ b/lib/rb/ext/compact_protocol.c
@@ -167,6 +167,7 @@
VALUE rb_thrift_compact_proto_write_i32(VALUE self, VALUE i32);
VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str);
+VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf);
VALUE rb_thrift_compact_proto_write_message_end(VALUE self) {
return Qnil;
@@ -305,10 +306,16 @@
}
VALUE rb_thrift_compact_proto_write_string(VALUE self, VALUE str) {
- VALUE transport = GET_TRANSPORT(self);
str = convert_to_utf8_byte_buffer(str);
- write_varint32(transport, RSTRING_LEN(str));
- WRITE(transport, RSTRING_PTR(str), RSTRING_LEN(str));
+ rb_thrift_compact_proto_write_binary(self, str);
+ return Qnil;
+}
+
+VALUE rb_thrift_compact_proto_write_binary(VALUE self, VALUE buf) {
+ buf = force_binary_encoding(buf);
+ VALUE transport = GET_TRANSPORT(self);
+ write_varint32(transport, RSTRING_LEN(buf));
+ WRITE(transport, RSTRING_PTR(buf), RSTRING_LEN(buf));
return Qnil;
}
@@ -319,6 +326,7 @@
#define is_bool_type(ctype) (((ctype) & 0x0F) == CTYPE_BOOLEAN_TRUE || ((ctype) & 0x0F) == CTYPE_BOOLEAN_FALSE)
VALUE rb_thrift_compact_proto_read_string(VALUE self);
+VALUE rb_thrift_compact_proto_read_binary(VALUE self);
VALUE rb_thrift_compact_proto_read_byte(VALUE self);
VALUE rb_thrift_compact_proto_read_i32(VALUE self);
VALUE rb_thrift_compact_proto_read_i16(VALUE self);
@@ -547,20 +555,24 @@
}
VALUE rb_thrift_compact_proto_read_string(VALUE self) {
- int64_t size = read_varint64(self);
- VALUE buffer = READ(self, size);
+ VALUE buffer = rb_thrift_compact_proto_read_binary(self);
return convert_to_string(buffer);
}
+VALUE rb_thrift_compact_proto_read_binary(VALUE self) {
+ int64_t size = read_varint64(self);
+ return READ(self, size);
+}
+
static void Init_constants() {
thrift_compact_protocol_class = rb_const_get(thrift_module, rb_intern("CompactProtocol"));
-
+
VERSION = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION")));
VERSION_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("VERSION_MASK")));
TYPE_MASK = rb_num2ll(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_MASK")));
TYPE_SHIFT_AMOUNT = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("TYPE_SHIFT_AMOUNT")));
PROTOCOL_ID = FIX2INT(rb_const_get(thrift_compact_protocol_class, rb_intern("PROTOCOL_ID")));
-
+
last_field_id = rb_intern("@last_field");
boolean_field_id = rb_intern("@boolean_field");
bool_value_id = rb_intern("@bool_value");
@@ -583,6 +595,7 @@
rb_define_method(thrift_compact_protocol_class, "write_i64", rb_thrift_compact_proto_write_i64, 1);
rb_define_method(thrift_compact_protocol_class, "write_double", rb_thrift_compact_proto_write_double, 1);
rb_define_method(thrift_compact_protocol_class, "write_string", rb_thrift_compact_proto_write_string, 1);
+ rb_define_method(thrift_compact_protocol_class, "write_binary", rb_thrift_compact_proto_write_binary, 1);
rb_define_method(thrift_compact_protocol_class, "write_message_end", rb_thrift_compact_proto_write_message_end, 0);
rb_define_method(thrift_compact_protocol_class, "write_struct_begin", rb_thrift_compact_proto_write_struct_begin, 1);
@@ -605,6 +618,7 @@
rb_define_method(thrift_compact_protocol_class, "read_i64", rb_thrift_compact_proto_read_i64, 0);
rb_define_method(thrift_compact_protocol_class, "read_double", rb_thrift_compact_proto_read_double, 0);
rb_define_method(thrift_compact_protocol_class, "read_string", rb_thrift_compact_proto_read_string, 0);
+ rb_define_method(thrift_compact_protocol_class, "read_binary", rb_thrift_compact_proto_read_binary, 0);
rb_define_method(thrift_compact_protocol_class, "read_message_end", rb_thrift_compact_proto_read_message_end, 0);
rb_define_method(thrift_compact_protocol_class, "read_struct_begin", rb_thrift_compact_proto_read_struct_begin, 0);