THRIFT-1766 [Ruby] Provide support for binary types
Patch: Vanja Bucic
diff --git a/lib/rb/ext/constants.h b/lib/rb/ext/constants.h
index 76947e5..e7aec44 100644
--- a/lib/rb/ext/constants.h
+++ b/lib/rb/ext/constants.h
@@ -42,6 +42,7 @@
extern ID write_i64_method_id;
extern ID write_double_method_id;
extern ID write_string_method_id;
+extern ID write_binary_method_id;
extern ID write_map_begin_method_id;
extern ID write_map_end_method_id;
extern ID write_list_begin_method_id;
@@ -54,6 +55,7 @@
extern ID read_i32_method_id;
extern ID read_i64_method_id;
extern ID read_string_method_id;
+extern ID read_binary_method_id;
extern ID read_double_method_id;
extern ID read_map_begin_method_id;
extern ID read_map_end_method_id;
@@ -87,6 +89,7 @@
extern VALUE value_sym;
extern VALUE element_sym;
extern VALUE class_sym;
+extern VALUE binary_sym;
extern VALUE rb_cSet;
extern VALUE thrift_module;
diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c
index 313da4c..f500d03 100644
--- a/lib/rb/ext/struct.c
+++ b/lib/rb/ext/struct.c
@@ -75,6 +75,11 @@
return Qnil;
}
+VALUE default_write_binary(VALUE protocol, VALUE value) {
+ rb_funcall(protocol, write_binary_method_id, 1, value);
+ return Qnil;
+}
+
VALUE default_write_list_begin(VALUE protocol, VALUE etype, VALUE length) {
rb_funcall(protocol, write_list_begin_method_id, 2, etype, length);
return Qnil;
@@ -190,6 +195,10 @@
return rb_funcall(protocol, read_string_method_id, 0);
}
+VALUE default_read_binary(VALUE protocol) {
+ return rb_funcall(protocol, read_binary_method_id, 0);
+}
+
VALUE default_read_struct_begin(VALUE protocol) {
return rb_funcall(protocol, read_struct_begin_method_id, 0);
}
@@ -327,7 +336,12 @@
} else if (ttype == TTYPE_DOUBLE) {
default_write_double(protocol, value);
} else if (ttype == TTYPE_STRING) {
- default_write_string(protocol, value);
+ VALUE is_binary = rb_hash_aref(field_info, binary_sym);
+ if (is_binary != Qtrue) {
+ default_write_string(protocol, value);
+ } else {
+ default_write_binary(protocol, value);
+ }
} else if (IS_CONTAINER(ttype)) {
write_container(ttype, field_info, value, protocol);
} else if (ttype == TTYPE_STRUCT) {
@@ -430,7 +444,12 @@
} else if (ttype == TTYPE_I64) {
result = default_read_i64(protocol);
} else if (ttype == TTYPE_STRING) {
- result = default_read_string(protocol);
+ VALUE is_binary = rb_hash_aref(field_info, binary_sym);
+ if (is_binary != Qtrue) {
+ result = default_read_string(protocol);
+ } else {
+ result = default_read_binary(protocol);
+ }
} else if (ttype == TTYPE_DOUBLE) {
result = default_read_double(protocol);
} else if (ttype == TTYPE_STRUCT) {
diff --git a/lib/rb/ext/thrift_native.c b/lib/rb/ext/thrift_native.c
index 614f2dd..3430b7c 100644
--- a/lib/rb/ext/thrift_native.c
+++ b/lib/rb/ext/thrift_native.c
@@ -57,6 +57,7 @@
ID write_i64_method_id;
ID write_double_method_id;
ID write_string_method_id;
+ID write_binary_method_id;
ID write_map_begin_method_id;
ID write_map_end_method_id;
ID write_list_begin_method_id;
@@ -69,6 +70,7 @@
ID read_i32_method_id;
ID read_i64_method_id;
ID read_string_method_id;
+ID read_binary_method_id;
ID read_double_method_id;
ID read_map_begin_method_id;
ID read_map_end_method_id;
@@ -104,6 +106,7 @@
VALUE value_sym;
VALUE element_sym;
VALUE class_sym;
+VALUE binary_sym;
VALUE protocol_exception_class;
void Init_thrift_native() {
@@ -140,6 +143,7 @@
write_i64_method_id = rb_intern("write_i64");
write_double_method_id = rb_intern("write_double");
write_string_method_id = rb_intern("write_string");
+ write_binary_method_id = rb_intern("write_binary");
write_map_begin_method_id = rb_intern("write_map_begin");
write_map_end_method_id = rb_intern("write_map_end");
write_list_begin_method_id = rb_intern("write_list_begin");
@@ -152,6 +156,7 @@
read_i32_method_id = rb_intern("read_i32");
read_i64_method_id = rb_intern("read_i64");
read_string_method_id = rb_intern("read_string");
+ read_binary_method_id = rb_intern("read_binary");
read_double_method_id = rb_intern("read_double");
read_map_begin_method_id = rb_intern("read_map_begin");
read_map_end_method_id = rb_intern("read_map_end");
@@ -187,6 +192,7 @@
value_sym = ID2SYM(rb_intern("value"));
element_sym = ID2SYM(rb_intern("element"));
class_sym = ID2SYM(rb_intern("class"));
+ binary_sym = ID2SYM(rb_intern("binary"));
Init_struct();
Init_binary_protocol_accelerated();