]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/union.rb
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
22 def initialize(name
=nil, value
=nil)
26 raise "#{self.class} cannot be instantiated with more than one field!"
29 name
, value
= name
.keys
.first
, name
.values
.first
32 if Thrift
.type_checking
33 raise Exception
, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name
.to_s
)
37 raise Exception
, "Union #{self.class} cannot be instantiated with setfield and nil value!"
40 Thrift
.check_type(value
, struct_fields
[name_to_id(name
.to_s
)], name
) if Thrift
.type_checking
42 raise Exception
, "Value provided, but no name!"
50 "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>"
57 iprot
.read_struct_begin
58 fname
, ftype
, fid
= iprot
.read_field_begin
59 handle_message(iprot
, fid
, ftype
)
62 fname
, ftype
, fid
= iprot
.read_field_begin
63 raise "Too many fields for union" unless (ftype
== Types
::STOP)
71 oprot
.write_struct_begin(self.class.name
)
73 fid
= self.name_to_id(@setfield.to_s
)
75 field_info
= struct_fields
[fid
]
76 type
= field_info
[:type]
78 oprot
.write_field_begin(@setfield, type
, fid
)
79 write_container(oprot
, @value, field_info
)
82 oprot
.write_field(@setfield, type
, fid
, @value)
85 oprot
.write_field_stop
86 oprot
.write_struct_end
90 other
.equal
?(self) || other
.instance_of
?(self.class) && @setfield == other
.get_set_field
&& @value == other
.get_value
92 alias_method
:eql?, :==
95 [self.class.name
, @setfield, @value].hash
98 def self.field_accessor(klass
, field_info
)
99 klass
.send
:define_method, field_info
[:name] do
100 if field_info
[:name].to_sym
== @setfield
103 raise RuntimeError
, "#{field_info[:name]} is not union's set field."
107 klass
.send
:define_method, "#{field_info[:name]}=" do |value
|
108 Thrift
.check_type(value
, field_info
, field_info
[:name]) if Thrift
.type_checking
109 @setfield = field_info
[:name].to_sym
114 def self.qmark_isset_method(klass
, field_info
)
115 klass
.send
:define_method, "#{field_info[:name]}?" do
116 get_set_field
== field_info
[:name].to_sym
&& !get_value
.nil?
120 def self.generate_accessors(klass
)
121 klass
::FIELDS.values
.each
do |field_info
|
122 field_accessor(klass
, field_info
)
123 qmark_isset_method(klass
, field_info
)
127 # get the symbol that indicates what the currently set field type is.
132 # get the current value of this union, regardless of what the set field is.
133 # generally, you should only use this method when you don't know in advance
134 # what field to expect.
140 if self.class == other
.class
141 if get_set_field
== other
.get_set_field
142 if get_set_field
.nil?
145 get_value
<=> other
.get_value
148 if get_set_field
&& other
.get_set_field
.nil?
150 elsif get_set_field
.nil? && other
.get_set_field
152 elsif get_set_field
.nil? && other
.get_set_field
.nil?
155 name_to_id(get_set_field
.to_s
) <=> name_to_id(other
.get_set_field
.to_s
)
159 self.class <=> other
.class
165 def handle_message(iprot
, fid
, ftype
)
166 field
= struct_fields
[fid
]
167 if field
and field
[:type] == ftype
168 @value = read_field(iprot
, field
)
169 name
= field
[:name].to_sym