THRIFT-712. rb: Inspect should print binary fields as hex instead of escaped string
authorBryan Duxbury <bryanduxbury@apache.org>
Thu, 18 Feb 2010 22:00:45 +0000 (22:00 +0000)
committerBryan Duxbury <bryanduxbury@apache.org>
Thu, 18 Feb 2010 22:00:45 +0000 (22:00 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911610 13f79535-47bb-0310-9956-ffa450edef68

compiler/cpp/src/generate/t_rb_generator.cc
lib/rb/Rakefile
lib/rb/lib/thrift/struct_union.rb
lib/rb/lib/thrift/union.rb
lib/rb/spec/ThriftSpec.thrift
lib/rb/spec/struct_spec.rb
lib/rb/spec/union_spec.rb

index 62234f6..cfa47c5 100644 (file)
@@ -651,6 +651,10 @@ void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type,
       out << ", :element => ";
       generate_field_data(out, ((t_set*)field_type)->get_elem_type());
     }
+  } else {
+    if (((t_base_type*)field_type)->is_binary()) {
+      out << ", :binary => true" << endl;
+    }
   }
   
   if(optional) {
index 39334f0..bd38561 100644 (file)
@@ -82,7 +82,7 @@ begin
     p.summary = "Ruby libraries for Thrift (a language-agnostic RPC system)"
     p.url = "http://incubator.apache.org/thrift/"
     p.include_rakefile = true
-    p.version = "0.2.3"
+    p.version = "0.2.4"
     p.rubygems_version = ">= 1.2.0"
   end
 
index 2397822..ddd7938 100644 (file)
@@ -141,6 +141,8 @@ module Thrift
         inspect_collection(value, field_info)
       elsif value.is_a? Set
         inspect_collection(value, field_info)
+      elsif value.is_a?(String) && field_info[:binary]
+        value.unpack("H*").first
       else
         value.inspect
       end
index 640e78b..9fde8fc 100644 (file)
@@ -29,6 +29,10 @@ module Thrift
           name, value = name.keys.first, name.values.first
         end
 
+        if Thrift.type_checking
+          raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s)
+        end
+
         if value.nil?
           raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!"
         end
index b497a60..e4dece4 100644 (file)
@@ -59,6 +59,7 @@ union TestUnion {
   2: i32 i32_field;
   3: i32 other_i32_field;
   4: SomeEnum enum_field;
+  5: binary binary_field;
 }
 
 struct Foo {
@@ -72,6 +73,10 @@ struct Foo {
   8: bool my_bool
 }
 
+struct Foo2 {
+  1: binary my_binary
+}
+
 struct BoolStruct {
   1: bool yesno = 1
 }
index 4b46284..c1271b9 100644 (file)
@@ -68,6 +68,10 @@ class ThriftStructSpec < Spec::ExampleGroup
       StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
     end
 
+    it "should pretty print binary fields" do
+      Foo2.new(:my_binary => "\001\002\003").inspect.should == "<SpecNamespace::Foo2 my_binary:010203>"
+    end
+
     it "should offer field? methods" do
       Foo.new.opt_string?.should be_false
       Foo.new(:simple => 52).simple?.should be_true
index f563b0b..33df251 100644 (file)
@@ -160,5 +160,9 @@ class ThriftUnionSpec < Spec::ExampleGroup
       My_union.new(:im_true => false).im_true?.should be_true
       My_union.new(:im_true => true).im_true?.should be_true
     end
+    
+    it "should pretty print binary fields" do
+      TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
+    end
   end
 end