From: Bryan Duxbury Date: Thu, 18 Feb 2010 17:42:06 +0000 (+0000) Subject: THRIFT-709. Print enum value names in Ruby X-Git-Tag: 0.3.0~120 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=3d03c527dfec72d10f885b3060ffdc35faa2cfb7;p=common%2Fthrift.git 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 a72074bf..5453d58a 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.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 9e52073b..caa90647 100644 --- a/lib/rb/lib/thrift/struct.rb +++ b/lib/rb/lib/thrift/struct.rb @@ -72,7 +72,7 @@ module Thrift 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 9a5903f1..2397822d 100644 --- a/lib/rb/lib/thrift/struct_union.rb +++ b/lib/rb/lib/thrift/struct_union.rb @@ -122,5 +122,37 @@ module Thrift :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 e443d427..4db820ee 100644 --- a/lib/rb/lib/thrift/union.rb +++ b/lib/rb/lib/thrift/union.rb @@ -42,7 +42,7 @@ module Thrift 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 f5c8c09a..84111c16 100644 --- a/lib/rb/spec/ThriftSpec.thrift +++ b/lib/rb/spec/ThriftSpec.thrift @@ -42,28 +42,15 @@ struct Hello { 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 @@ service NonblockingService { 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> my_map; +} + +struct Struct_with_union { + 1: My_union fun_union + 2: i32 integer32 + 3: string some_characters +} + +struct StructWithEnumMap { + 1: map> 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 237f9829..045b9635 100644 --- a/lib/rb/spec/struct_spec.rb +++ b/lib/rb/spec/struct_spec.rb @@ -62,6 +62,12 @@ class ThriftStructSpec < Spec::ExampleGroup 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 == "" + + StructWithEnumMap.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "" + 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 07a3f94b..f39d0337 100644 --- a/lib/rb/spec/union_spec.rb +++ b/lib/rb/spec/union_spec.rb @@ -147,5 +147,11 @@ class ThriftUnionSpec < Spec::ExampleGroup 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 == "" + + My_union.new(:my_map => {SomeEnum::ONE => [SomeEnum::TWO]}).inspect.should == "" + end end end