rb: Add pretty inspect, optional field hint for Thrift::Struct
Author: Bryan Duxbury
git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@689193 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb
index 0fbb9e4..6e6b8ad 100644
--- a/lib/rb/lib/thrift/struct.rb
+++ b/lib/rb/lib/thrift/struct.rb
@@ -57,10 +57,21 @@
def each_field
struct_fields.each do |fid, data|
- yield fid, data[:type], data[:name], data[:default]
+ yield fid, data[:type], data[:name], data[:default], data[:optional]
end
end
+ def inspect(skip_optional_nulls = true)
+ fields = []
+ each_field do |fid, type, name, default, optional|
+ value = instance_variable_get("@#{name}")
+ unless skip_optional_nulls && optional && value.nil?
+ fields << "#{name}:#{value.inspect}"
+ end
+ end
+ "<#{self.class} #{fields.join(", ")}>"
+ end
+
def read(iprot)
# TODO(kevinclark): Make sure transport is C readable
if iprot.respond_to?(:decode_binary)
diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift
index 022c786..e04662b 100644
--- a/lib/rb/spec/ThriftSpec.thrift
+++ b/lib/rb/spec/ThriftSpec.thrift
@@ -10,7 +10,8 @@
3: Hello hello = {'greeting' : "hello, world!"},
4: list<i32> ints = [1, 2, 2, 3],
5: map<i32, map<string, double>> complex,
- 6: set<i16> shorts = [5, 17, 239]
+ 6: set<i16> shorts = [5, 17, 239],
+ 7: optional string opt_string
}
struct BoolStruct {
diff --git a/lib/rb/spec/gen-rb/ThriftSpec_types.rb b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
index 24358da..d7ad600 100644
--- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb
+++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb
@@ -17,7 +17,7 @@
class Foo
include Thrift::Struct
- Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts
+ Thrift::Struct.field_accessor self, :simple, :words, :hello, :ints, :complex, :shorts, :opt_string
FIELDS = {
1 => {:type => Thrift::Types::I32, :name => 'simple', :default => 53},
2 => {:type => Thrift::Types::STRING, :name => 'words', :default => 'words'},
@@ -34,7 +34,8 @@
6 => {:type => Thrift::Types::SET, :name => 'shorts', :default => Set.new([ 5,
17,
239,
- ]), :element => {:type => Thrift::Types::I16}}
+ ]), :element => {:type => Thrift::Types::I16}},
+ 7 => {:type => Thrift::Types::STRING, :name => 'opt_string', :optional => true}
}
end
diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb
index 8e83d47..7c52d16 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -17,14 +17,15 @@
describe Struct do
it "should iterate over all fields properly" do
fields = {}
- Foo.new.each_field { |fid,type,name,default| fields[fid] = [type,name,default] }
+ Foo.new.each_field { |fid,type,name,default,optional| fields[fid] = [type,name,default,optional] }
fields.should == {
- 1 => [Types::I32, 'simple', 53],
- 2 => [Types::STRING, 'words', "words"],
- 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!')],
- 4 => [Types::LIST, 'ints', [1, 2, 2, 3]],
- 5 => [Types::MAP, 'complex', nil],
- 6 => [Types::SET, 'shorts', Set.new([5, 17, 239])]
+ 1 => [Types::I32, 'simple', 53, nil],
+ 2 => [Types::STRING, 'words', "words", nil],
+ 3 => [Types::STRUCT, 'hello', Hello.new(:greeting => 'hello, world!'), nil],
+ 4 => [Types::LIST, 'ints', [1, 2, 2, 3], nil],
+ 5 => [Types::MAP, 'complex', nil, nil],
+ 6 => [Types::SET, 'shorts', Set.new([5, 17, 239]), nil],
+ 7 => [Types::STRING, 'opt_string', nil, true]
}
end