You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2010/02/18 21:28:27 UTC

svn commit: r911557 - in /incubator/thrift/trunk: compiler/cpp/src/generate/t_rb_generator.cc lib/rb/Rakefile lib/rb/lib/thrift/struct.rb lib/rb/lib/thrift/union.rb lib/rb/spec/ThriftSpec.thrift lib/rb/spec/struct_spec.rb lib/rb/spec/union_spec.rb

Author: bryanduxbury
Date: Thu Feb 18 20:28:27 2010
New Revision: 911557

URL: http://svn.apache.org/viewvc?rev=911557&view=rev
Log:
THRIFT-708. rb: Is set checking methods

Modified:
    incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
    incubator/thrift/trunk/lib/rb/Rakefile
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/union.rb
    incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift
    incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
    incubator/thrift/trunk/lib/rb/spec/union_spec.rb

Modified: incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc (original)
+++ incubator/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc Thu Feb 18 20:28:27 2010
@@ -87,8 +87,6 @@
   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 @@
   }
 
   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 @@
 
   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 @@
   out << endl;
 }
 
-void t_rb_generator::generate_struct_accessors(std::ofstream& out, t_struct* tstruct) {
-  const vector<t_field*>& members = tstruct->get_members();
-  vector<t_field*>::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<t_field*>& members = tstruct->get_members();
-  vector<t_field*>::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<t_field*>& fields = tstruct->get_members();
   vector<t_field*>::const_iterator f_iter;

Modified: incubator/thrift/trunk/lib/rb/Rakefile
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/Rakefile?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/Rakefile (original)
+++ incubator/thrift/trunk/lib/rb/Rakefile Thu Feb 18 20:28:27 2010
@@ -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.2"
+    p.version = "0.2.3"
     p.rubygems_version = ">= 1.2.0"
   end
 

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Thu Feb 18 20:28:27 2010
@@ -145,13 +145,25 @@
       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
 

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/union.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/union.rb?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/union.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/union.rb Thu Feb 18 20:28:27 2010
@@ -90,21 +90,32 @@
       [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
 

Modified: incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift (original)
+++ incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift Thu Feb 18 20:28:27 2010
@@ -69,6 +69,7 @@
   5: map<i32, map<string, double>> complex,
   6: set<i16> shorts = [5, 17, 239],
   7: optional string opt_string
+  8: bool my_bool
 }
 
 struct BoolStruct {

Modified: incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/struct_spec.rb?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Thu Feb 18 20:28:27 2010
@@ -68,6 +68,13 @@
       StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
     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"))

Modified: incubator/thrift/trunk/lib/rb/spec/union_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/union_spec.rb?rev=911557&r1=911556&r2=911557&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/union_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/union_spec.rb Thu Feb 18 20:28:27 2010
@@ -153,5 +153,12 @@
       
       My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::My_union my_map: {ONE (0): [TWO (1)]}>" 
     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