From: Bryan Duxbury Date: Thu, 18 Feb 2010 20:28:27 +0000 (+0000) Subject: THRIFT-708. rb: Is set checking methods X-Git-Tag: 0.3.0~118 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=0e4920c6b892ef46ae491fb1c65157ee6aab1367;p=common%2Fthrift.git THRIFT-708. rb: Is set checking methods git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911557 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 47fd8ec5..62234f6a 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -87,8 +87,6 @@ class t_rb_generator : public t_oop_generator { void generate_rb_simple_exception_constructor(std::ofstream& out, t_struct* tstruct); void generate_field_constants (std::ofstream& out, t_struct* tstruct); void generate_field_constructors (std::ofstream& out, t_struct* tstruct); - void generate_struct_accessors (std::ofstream& out, t_struct* tstruct); - void generate_union_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, bool optional); @@ -502,10 +500,11 @@ void t_rb_generator::generate_rb_struct(std::ofstream& out, t_struct* tstruct, b } generate_field_constants(out, tstruct); - generate_struct_accessors(out, tstruct); generate_field_defns(out, tstruct); generate_rb_struct_required_validator(out, tstruct); - + + indent(out) << "::Thrift::Struct.generate_accessors self" << endl; + indent_down(); indent(out) << "end" << endl << endl; } @@ -520,14 +519,15 @@ void t_rb_generator::generate_rb_union(std::ofstream& out, t_struct* tstruct, bo indent_up(); indent(out) << "include ::Thrift::Struct_Union" << endl; - + generate_field_constructors(out, tstruct); - + generate_field_constants(out, tstruct); - generate_union_accessors(out, tstruct); generate_field_defns(out, tstruct); generate_rb_union_validator(out, tstruct); + indent(out) << "::Thrift::Union.generate_accessors self" << endl; + indent_down(); indent(out) << "end" << endl << endl; } @@ -593,32 +593,6 @@ void t_rb_generator::generate_field_constants(std::ofstream& out, t_struct* tstr out << endl; } -void t_rb_generator::generate_struct_accessors(std::ofstream& out, t_struct* tstruct) { - const vector& members = tstruct->get_members(); - vector::const_iterator m_iter; - - if (members.size() > 0) { - indent(out) << "::Thrift::Struct.field_accessor self"; - for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { - out << ", :" << (*m_iter)->get_name(); - } - out << endl; - } -} - -void t_rb_generator::generate_union_accessors(std::ofstream& out, t_struct* tstruct) { - const vector& members = tstruct->get_members(); - vector::const_iterator m_iter; - - if (members.size() > 0) { - indent(out) << "field_accessor self"; - for (m_iter = members.begin(); m_iter != members.end(); ++m_iter) { - out << ", :" << (*m_iter)->get_name(); - } - out << endl; - } -} - void t_rb_generator::generate_field_defns(std::ofstream& out, t_struct* tstruct) { const vector& fields = tstruct->get_members(); vector::const_iterator f_iter; diff --git a/lib/rb/Rakefile b/lib/rb/Rakefile index 5453d58a..39334f04 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.2" + p.version = "0.2.3" p.rubygems_version = ">= 1.2.0" end diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb index caa90647..48353d30 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -145,13 +145,25 @@ module Thrift diffs end - def self.field_accessor(klass, *fields) - fields.each do |field| - klass.send :attr_reader, field - klass.send :define_method, "#{field}=" do |value| - Thrift.check_type(value, klass::FIELDS.values.find { |f| f[:name].to_s == field.to_s }, field) if Thrift.type_checking - instance_variable_set("@#{field}", value) - end + def self.field_accessor(klass, field_info) + field_name_sym = field_info[:name].to_sym + klass.send :attr_reader, field_name_sym + klass.send :define_method, "#{field_info[:name]}=" do |value| + Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking + instance_variable_set("@#{field_name_sym}", value) + end + end + + def self.generate_accessors(klass) + klass::FIELDS.values.each do |field_info| + field_accessor(klass, field_info) + qmark_isset_method(klass, field_info) + end + end + + def self.qmark_isset_method(klass, field_info) + klass.send :define_method, "#{field_info[:name]}?" do + !self.send(field_info[:name].to_sym).nil? end end diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb index 4db820ee..640e78b8 100644 --- a/lib/rb/lib/thrift/union.rb +++ b/lib/rb/lib/thrift/union.rb @@ -90,21 +90,32 @@ module Thrift [self.class.name, @setfield, @value].hash end - def self.field_accessor(klass, *fields) - fields.each do |field| - klass.send :define_method, "#{field}" do - if field == @setfield - @value - else - raise RuntimeError, "#{field} is not union's set field." - end + def self.field_accessor(klass, field_info) + klass.send :define_method, field_info[:name] do + if field_info[:name].to_sym == @setfield + @value + else + raise RuntimeError, "#{field_info[:name]} is not union's set field." end + end - klass.send :define_method, "#{field}=" do |value| - Thrift.check_type(value, klass::FIELDS.values.find {|f| f[:name].to_s == field.to_s }, field) if Thrift.type_checking - @setfield = field - @value = value - end + klass.send :define_method, "#{field_info[:name]}=" do |value| + Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking + @setfield = field_info[:name].to_sym + @value = value + end + end + + def self.qmark_isset_method(klass, field_info) + klass.send :define_method, "#{field_info[:name]}?" do + get_set_field == field_info[:name].to_sym && !get_value.nil? + end + end + + def self.generate_accessors(klass) + klass::FIELDS.values.each do |field_info| + field_accessor(klass, field_info) + qmark_isset_method(klass, field_info) end end diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift index 84111c16..b497a603 100644 --- a/lib/rb/spec/ThriftSpec.thrift +++ b/lib/rb/spec/ThriftSpec.thrift @@ -69,6 +69,7 @@ struct Foo { 5: map> complex, 6: set shorts = [5, 17, 239], 7: optional string opt_string + 8: bool my_bool } struct BoolStruct { diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb index 045b9635..4b46284a 100644 --- a/lib/rb/spec/struct_spec.rb +++ b/lib/rb/spec/struct_spec.rb @@ -68,6 +68,13 @@ class ThriftStructSpec < Spec::ExampleGroup StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "" end + it "should offer field? methods" do + Foo.new.opt_string?.should be_false + Foo.new(:simple => 52).simple?.should be_true + Foo.new(:my_bool => false).my_bool?.should be_true + Foo.new(:my_bool => true).my_bool?.should be_true + end + it "should read itself off the wire" do struct = Foo.new prot = BaseProtocol.new(mock("transport")) diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb index f39d0337..f563b0b2 100644 --- a/lib/rb/spec/union_spec.rb +++ b/lib/rb/spec/union_spec.rb @@ -153,5 +153,12 @@ class ThriftUnionSpec < Spec::ExampleGroup My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "" end + + it "should offer field? methods" do + My_union.new.some_enum?.should be_false + My_union.new(:some_enum => SomeEnum::ONE).some_enum?.should be_true + My_union.new(:im_true => false).im_true?.should be_true + My_union.new(:im_true => true).im_true?.should be_true + end end end