THRIFT-709. Print enum value names in Ruby

git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911500 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/Rakefile b/lib/rb/Rakefile
index a72074b..5453d58 100644
--- a/lib/rb/Rakefile
+++ b/lib/rb/Rakefile
@@ -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.1"
+    p.version = "0.2.2"
     p.rubygems_version = ">= 1.2.0"
   end
 
diff --git a/lib/rb/lib/thrift/struct.rb b/lib/rb/lib/thrift/struct.rb
index 9e52073..caa9064 100644
--- a/lib/rb/lib/thrift/struct.rb
+++ b/lib/rb/lib/thrift/struct.rb
@@ -72,7 +72,7 @@
         name = field_info[:name]
         value = instance_variable_get("@#{name}")
         unless skip_optional_nulls && field_info[:optional] && value.nil?
-          fields << "#{name}:#{value.inspect}"
+          fields << "#{name}:#{inspect_field(value, field_info)}"
         end
       end
       "<#{self.class} #{fields.join(", ")}>"
diff --git a/lib/rb/lib/thrift/struct_union.rb b/lib/rb/lib/thrift/struct_union.rb
index 9a5903f..2397822 100644
--- a/lib/rb/lib/thrift/struct_union.rb
+++ b/lib/rb/lib/thrift/struct_union.rb
@@ -122,5 +122,37 @@
         :value => field[:value],
         :element => field[:element] }
     end
+
+    def inspect_field(value, field_info)
+      if enum_class = field_info[:enum_class]
+        "#{enum_class.const_get(:VALUE_MAP)[value]} (#{value})"
+      elsif value.is_a? Hash 
+        if field_info[:type] == Types::MAP
+          map_buf = []
+          value.each do |k, v|
+            map_buf << inspect_field(k, field_info[:key]) + ": " + inspect_field(v, field_info[:value])
+          end
+          "{" + map_buf.join(", ") + "}"
+        else
+          # old-style set
+          inspect_collection(value.keys, field_info)
+        end
+      elsif value.is_a? Array
+        inspect_collection(value, field_info)
+      elsif value.is_a? Set
+        inspect_collection(value, field_info)
+      else
+        value.inspect
+      end
+    end
+    
+    def inspect_collection(collection, field_info)
+      buf = []
+      collection.each do |k|
+        buf << inspect_field(k, field_info[:element])
+      end
+      "[" + buf.join(", ") + "]"      
+    end
+    
   end
 end
\ No newline at end of file
diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb
index e443d42..4db820e 100644
--- a/lib/rb/lib/thrift/union.rb
+++ b/lib/rb/lib/thrift/union.rb
@@ -42,7 +42,7 @@
     end
 
     def inspect
-      "<#{self.class} #{@setfield}: #{@value}>"
+      "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
     end
 
     def read(iprot)
diff --git a/lib/rb/spec/ThriftSpec.thrift b/lib/rb/spec/ThriftSpec.thrift
index f5c8c09..84111c1 100644
--- a/lib/rb/spec/ThriftSpec.thrift
+++ b/lib/rb/spec/ThriftSpec.thrift
@@ -42,28 +42,15 @@
   1: string greeting = "hello world"
 }
 
-union My_union {
-  1: bool im_true,
-  2: byte a_bite,
-  3: i16 integer16,
-  4: i32 integer32,
-  5: i64 integer64,
-  6: double double_precision,
-  7: string some_characters,
-  8: i32 other_i32
-}
-
-struct Struct_with_union {
-  1: My_union fun_union
-  2: i32 integer32
-  3: string some_characters
-}
-
 enum SomeEnum {
   ONE
   TWO
 }
 
+struct StructWithSomeEnum {
+  1: SomeEnum some_enum;
+}
+
 union TestUnion {
   /**
    * A doc string
@@ -114,3 +101,26 @@
   oneway void shutdown()
   void sleep(1:double seconds)
 }
+
+union My_union {
+  1: bool im_true,
+  2: byte a_bite,
+  3: i16 integer16,
+  4: i32 integer32,
+  5: i64 integer64,
+  6: double double_precision,
+  7: string some_characters,
+  8: i32 other_i32
+  9: SomeEnum some_enum;
+  10: map<SomeEnum, list<SomeEnum>> my_map;
+}
+
+struct Struct_with_union {
+  1: My_union fun_union
+  2: i32 integer32
+  3: string some_characters
+}
+
+struct StructWithEnumMap {
+  1: map<SomeEnum, list<SomeEnum>> my_map;
+}
\ No newline at end of file
diff --git a/lib/rb/spec/struct_spec.rb b/lib/rb/spec/struct_spec.rb
index 237f982..045b963 100644
--- a/lib/rb/spec/struct_spec.rb
+++ b/lib/rb/spec/struct_spec.rb
@@ -62,6 +62,12 @@
       Foo.new(:simple => 52).should_not == Foo.new
     end
 
+    it "should print enum value names in inspect" do
+      StructWithSomeEnum.new(:some_enum => SomeEnum::ONE).inspect.should == "<SpecNamespace::StructWithSomeEnum some_enum:ONE (0)>"
+
+      StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "<SpecNamespace::StructWithEnumMap my_map:{ONE (0): [TWO (1)]}>"
+    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 07a3f94..f39d033 100644
--- a/lib/rb/spec/union_spec.rb
+++ b/lib/rb/spec/union_spec.rb
@@ -147,5 +147,11 @@
       union.get_set_field.should == :integer32
       union.get_value.should == 26
     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
   end
 end