From: Kevin Clark Date: Tue, 26 Aug 2008 20:02:07 +0000 (+0000) Subject: rb: Add pretty inspect, optional field hint for Thrift::Struct X-Git-Tag: 0.2.0~444 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=5ad6d4a7e35dc8f7078529e8e8004eb90dae31e9;p=common%2Fthrift.git 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/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 3012ab82..acc87e6a 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -69,7 +69,7 @@ class t_rb_generator : public t_oop_generator { void generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct); void generate_accessors (std::ofstream& out, t_struct* tstruct); void generate_field_defns (std::ofstream& out, t_struct* tstruct); - void generate_field_data (std::ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value); + void generate_field_data (std::ofstream& out, t_type* field_type, const std::string& field_name, t_const_value* field_value, bool optional); /** * Service-level generation functions @@ -512,7 +512,8 @@ void t_rb_generator::generate_field_defns(std::ofstream& out, t_struct* tstruct) indent(out) << (*f_iter)->get_key() << " => "; - generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value()); + generate_field_data(out, (*f_iter)->get_type(), (*f_iter)->get_name(), (*f_iter)->get_value(), + (*f_iter)->get_req() == t_field::T_OPTIONAL); } indent_down(); out << endl; @@ -520,7 +521,7 @@ void t_rb_generator::generate_field_defns(std::ofstream& out, t_struct* tstruct) } void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type, - const std::string& field_name = "", t_const_value* field_value = NULL) { + const std::string& field_name = "", t_const_value* field_value = NULL, bool optional = false) { field_type = get_true_type(field_type); // Begin this field's defn @@ -550,6 +551,10 @@ void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type, generate_field_data(out, ((t_set*)field_type)->get_elem_type()); } } + + if(optional) { + out << ", :optional => true"; + } // End of this field's defn out << "}"; diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index 0fbb9e4a..6e6b8ad8 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -57,10 +57,21 @@ module Thrift 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 022c786b..e04662b5 100644 --- a/lib/rb/spec/ThriftSpec.thrift +++ b/lib/rb/spec/ThriftSpec.thrift @@ -10,7 +10,8 @@ struct Foo { 3: Hello hello = {'greeting' : "hello, world!"}, 4: list ints = [1, 2, 2, 3], 5: map> complex, - 6: set shorts = [5, 17, 239] + 6: set 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 24358dad..d7ad6006 100644 --- a/lib/rb/spec/gen-rb/ThriftSpec_types.rb +++ b/lib/rb/spec/gen-rb/ThriftSpec_types.rb @@ -17,7 +17,7 @@ module SpecNamespace 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 @@ module SpecNamespace 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 8e83d478..7c52d160 100644 --- a/lib/rb/spec/struct_spec.rb +++ b/lib/rb/spec/struct_spec.rb @@ -17,14 +17,15 @@ class ThriftStructSpec < Spec::ExampleGroup 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