From: Bryan Duxbury Date: Wed, 17 Feb 2010 22:27:27 +0000 (+0000) Subject: THRIFT-707. Support old-style constructors for Union structs X-Git-Tag: 0.3.0~123 X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=5af64f03464b927bdc329065b24a063c02fe8412;p=common%2Fthrift.git THRIFT-707. Support old-style constructors for Union structs git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@911199 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/lib/rb/lib/thrift/union.rb b/lib/rb/lib/thrift/union.rb index 0b41ed49..e443d427 100644 --- a/lib/rb/lib/thrift/union.rb +++ b/lib/rb/lib/thrift/union.rb @@ -21,6 +21,14 @@ module Thrift class Union def initialize(name=nil, value=nil) if name + if name.is_a? Hash + if name.size > 1 + raise "#{self.class} cannot be instantiated with more than one field!" + end + + name, value = name.keys.first, name.values.first + end + if value.nil? raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!" end diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb index 48352884..07a3f94b 100644 --- a/lib/rb/spec/union_spec.rb +++ b/lib/rb/spec/union_spec.rb @@ -141,5 +141,11 @@ class ThriftUnionSpec < Spec::ExampleGroup 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 end end