THRIFT-1766 [Ruby] Provide support for binary types
authorhenrique <henrique@apache.org>
Wed, 16 Jul 2014 18:10:57 +0000 (20:10 +0200)
committerhenrique <henrique@apache.org>
Wed, 16 Jul 2014 18:10:57 +0000 (20:10 +0200)
Patch: Vanja Bucic

lib/rb/ext/constants.h
lib/rb/ext/struct.c
lib/rb/ext/thrift_native.c

index 76947e5..e7aec44 100644 (file)
@@ -42,6 +42,7 @@ extern ID write_i32_method_id;
 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_i16_method_id;
 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 key_sym;
 extern VALUE value_sym;
 extern VALUE element_sym;
 extern VALUE class_sym;
+extern VALUE binary_sym;
 
 extern VALUE rb_cSet;
 extern VALUE thrift_module;
index 313da4c..f500d03 100644 (file)
@@ -75,6 +75,11 @@ VALUE default_write_string(VALUE protocol, VALUE value) {
   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 @@ VALUE default_read_string(VALUE protocol) {
   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 @@ static void write_anything(int ttype, VALUE value, VALUE protocol, VALUE field_i
   } 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 @@ static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) {
   } 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) {
index 614f2dd..3430b7c 100644 (file)
@@ -57,6 +57,7 @@ ID write_i32_method_id;
 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_i16_method_id;
 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 key_sym;
 VALUE value_sym;
 VALUE element_sym;
 VALUE class_sym;
+VALUE binary_sym;
 VALUE protocol_exception_class;
 
 void Init_thrift_native() {
@@ -140,6 +143,7 @@ void Init_thrift_native() {
   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 @@ void Init_thrift_native() {
   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 @@ void Init_thrift_native() {
   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();