Author: bryanduxbury
Date: Thu Feb 18 17:42:06 2010
New Revision: 911500
URL: http://svn.apache.org/viewvc?rev=911500&view=rev
Log:
THRIFT-709. Print enum value names in Ruby
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/ThriftSpec.thrift
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=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/Rakefile (original)
+++ incubator/thrift/trunk/lib/rb/Rakefile Thu Feb 18 17:42:06 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.1"
+ p.version = "0.2.2"
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=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/struct.rb Thu Feb 18 17:42:06 2010
@@ -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(", ")}>"
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=911500&r1=911499&r2=911500&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
17:42:06 2010
@@ -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
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=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/union.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/union.rb Thu Feb 18 17:42:06 2010
@@ -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)
Modified: incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift
URL:
http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift?rev=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift (original)
+++ incubator/thrift/trunk/lib/rb/spec/ThriftSpec.thrift Thu Feb 18 17:42:06
2010
@@ -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
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=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/struct_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/struct_spec.rb Thu Feb 18 17:42:06 2010
@@ -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"))
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=911500&r1=911499&r2=911500&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/union_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/union_spec.rb Thu Feb 18 17:42:06 2010
@@ -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