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/19 00:19:43 UTC

svn commit: r911644 - in /incubator/thrift/trunk/lib/rb: Rakefile lib/thrift/struct.rb lib/thrift/struct_union.rb lib/thrift/union.rb spec/struct_spec.rb spec/union_spec.rb

Author: bryanduxbury
Date: Thu Feb 18 23:19:42 2010
New Revision: 911644

URL: http://svn.apache.org/viewvc?rev=911644&view=rev
Log:
THRIFT-553. rb: thrift structs should be comparable (<=>)
This patch adds the spaceship operator to the struct and union base classes, enabling object comparisons between objects without regenerating code.

Modified:
    incubator/thrift/trunk/lib/rb/Rakefile
    incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/struct_union.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/union.rb
    incubator/thrift/trunk/lib/rb/spec/struct_spec.rb
    incubator/thrift/trunk/lib/rb/spec/union_spec.rb

Modified: incubator/thrift/trunk/lib/rb/Rakefile
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/Rakefile?rev=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/Rakefile (original)
+++ incubator/thrift/trunk/lib/rb/Rakefile Thu Feb 18 23:19:42 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.4"
+    p.version = "0.2.5"
     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=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Thu Feb 18 23:19:42 2010
@@ -167,6 +167,30 @@
       end
     end
 
+    def <=>(other)
+      if self.class == other.class
+        each_field do |fid, field_info|
+          v1 = self.send(field_info[:name])
+          v1_set = !v1.nil?
+          v2 = other.send(field_info[:name])
+          v2_set = !v2.nil?
+          if v1_set && !v2_set
+            return -1
+          elsif !v1_set && v2_set
+            return 1
+          elsif v1_set && v2_set
+            cmp = v1 <=> v2
+            if cmp != 0
+              return cmp
+            end
+          end
+        end
+        0
+      else
+        self.class <=> other.class
+      end
+    end
+
     protected
 
     def self.append_features(mod)

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/struct_union.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/struct_union.rb?rev=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct_union.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct_union.rb Thu Feb 18 23:19:42 2010
@@ -155,6 +155,5 @@
       end
       "[" + buf.join(", ") + "]"      
     end
-    
   end
 end
\ No newline at end of file

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=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/union.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/union.rb Thu Feb 18 23:19:42 2010
@@ -46,7 +46,11 @@
     end
 
     def inspect
-      "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
+      if get_set_field
+        "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
+      else
+        "<#{self.class} >"
+      end
     end
 
     def read(iprot)
@@ -135,6 +139,30 @@
       @value
     end
 
+    def <=>(other)
+      if self.class == other.class
+        if get_set_field == other.get_set_field
+          if get_set_field.nil?
+            0
+          else
+            get_value <=> other.get_value
+          end
+        else
+          if get_set_field && other.get_set_field.nil?
+            -1
+          elsif get_set_field.nil? && other.get_set_field
+            1
+          elsif get_set_field.nil? && other.get_set_field.nil?
+            0
+          else
+            name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s)
+          end
+        end
+      else
+        self.class <=> other.class
+      end
+    end
+
     protected
 
     def handle_message(iprot, fid, ftype)

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=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Thu Feb 18 23:19:42 2010
@@ -79,6 +79,16 @@
       Foo.new(:my_bool => true).my_bool?.should be_true
     end
 
+    it "should be comparable" do
+      s1 = StructWithSomeEnum.new(:some_enum => SomeEnum::ONE)
+      s2 = StructWithSomeEnum.new(:some_enum => SomeEnum::TWO)
+
+      (s1 <=> s2).should == -1
+      (s2 <=> s1).should == 1
+      (s1 <=> s1).should == 0
+      (s1 <=> StructWithSomeEnum.new()).should == -1
+    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=911644&r1=911643&r2=911644&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/union_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/union_spec.rb Thu Feb 18 23:19:42 2010
@@ -141,28 +141,53 @@
       swu2.read(proto)
       swu2.should == swu
     end
-    
+
     it "should support old style constructor" do
       union = My_union.new(:integer32 => 26)
       union.get_set_field.should == :integer32
       union.get_value.should == 26
     end
-    
+
+    it "should not throw an error when inspected and unset" do
+      lambda{TestUnion.new().inspect}.should_not raise_error
+    end
+
     it "should print enum value name when inspected" do
       My_union.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::My_union some_enum: ONE (0)>"
-      
+
       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
-    
+
     it "should pretty print binary fields" do
       TestUnion.new(:binary_field => "\001\002\003").inspect.should == "<SpecNamespace::TestUnion binary_field: 010203>"
     end
+
+    it "should be comparable" do
+      relationships = [
+        [0,   -1, -1, -1],
+        [1,   0,  -1, -1],
+        [1,   1,  0,  -1],
+        [1,   1,  1,  0]]
+
+      objs = [
+        TestUnion.new(:string_field, "blah"), 
+        TestUnion.new(:string_field, "blahblah"),
+        TestUnion.new(:i32_field, 1),
+        TestUnion.new()]
+
+      for y in 0..3
+        for x in 0..3
+          # puts "#{objs[y].inspect} <=> #{objs[x].inspect} should == #{relationships[y][x]}"
+          (objs[y] <=> objs[x]).should == relationships[y][x]
+        end
+      end
+    end
   end
 end