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 62234f6..cfa47c5 100644
--- a/compiler/cpp/src/generate/t_rb_generator.cc
+++ b/compiler/cpp/src/generate/t_rb_generator.cc
@@ -651,6 +651,10 @@
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 39334f0..bd38561 100644
--- a/lib/rb/Rakefile
+++ b/lib/rb/Rakefile
@@ -82,7 +82,7 @@
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 2397822..ddd7938 100644
--- a/lib/rb/lib/thrift/struct_union.rb
+++ b/lib/rb/lib/thrift/struct_union.rb
@@ -141,6 +141,8 @@
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 640e78b..9fde8fc 100644
--- a/lib/rb/lib/thrift/union.rb
+++ b/lib/rb/lib/thrift/union.rb
@@ -29,6 +29,10 @@
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 b497a60..e4dece4 100644
--- a/lib/rb/spec/ThriftSpec.thrift
+++ b/lib/rb/spec/ThriftSpec.thrift
@@ -59,6 +59,7 @@
2: i32 i32_field;
3: i32 other_i32_field;
4: SomeEnum enum_field;
+ 5: binary binary_field;
}
struct Foo {
@@ -72,6 +73,10 @@
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 4b46284..c1271b9 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -68,6 +68,10 @@
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
diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb
index f563b0b..33df251 100644
--- a/lib/rb/spec/union_spec.rb
+++ b/lib/rb/spec/union_spec.rb
@@ -160,5 +160,9 @@
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