From: Bryan Duxbury Date: Thu, 18 Feb 2010 22:00:45 +0000 (+0000) Subject: THRIFT-712. rb: Inspect should print binary fields as hex instead of escaped string X-Git-Tag: 0.3.0~117 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=39dadd6656d0cf009e8fdef42db9eea108cb6bb9;p=common%2Fthrift.git THRIFT-712. rb: Inspect should print binary fields as hex instead of escaped string git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911610 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 62234f6a..cfa47c5a 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -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) { diff --git a/lib/rb/Rakefile b/lib/rb/Rakefile index 39334f04..bd385613 100644 --- a/lib/rb/Rakefile +++ b/lib/rb/Rakefile @@ -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 diff --git a/lib/rb/lib/thrift/struct_union.rb b/lib/rb/lib/thrift/struct_union.rb index 2397822d..ddd7938d 100644 --- a/lib/rb/lib/thrift/struct_union.rb +++ b/lib/rb/lib/thrift/struct_union.rb @@ -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 diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb index 640e78b8..9fde8fcc 100644 --- a/lib/rb/lib/thrift/union.rb +++ b/lib/rb/lib/thrift/union.rb @@ -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 diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift index b497a603..e4dece4b 100644 --- a/lib/rb/spec/ThriftSpec.thrift +++ b/lib/rb/spec/ThriftSpec.thrift @@ -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 } diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb index 4b46284a..c1271b90 100644 --- a/lib/rb/spec/struct_spec.rb +++ b/lib/rb/spec/struct_spec.rb @@ -68,6 +68,10 @@ class ThriftStructSpec < Spec::ExampleGroup StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "" end + it "should pretty print binary fields" do + Foo2.new(:my_binary => "\001\002\003").inspect.should == "" + end + it "should offer field? methods" do Foo.new.opt_string?.should be_false Foo.new(:simple => 52).simple?.should be_true diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb index f563b0b2..33df251a 100644 --- a/lib/rb/spec/union_spec.rb +++ b/lib/rb/spec/union_spec.rb @@ -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 == "" + end end end