class Thrift::Union
Public Class Methods
Source
# File lib/thrift/union.rb 114 def self.field_accessor(klass, field_info) 115 klass.send :define_method, field_info[:name] do 116 if field_info[:name].to_sym == @setfield 117 @value 118 else 119 raise "#{field_info[:name]} is not union's set field." 120 end 121 end 122 123 klass.send :define_method, "#{field_info[:name]}=" do |value| 124 Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 125 @setfield = field_info[:name].to_sym 126 @value = value 127 end 128 end
Source
# File lib/thrift/union.rb 136 def self.generate_accessors(klass) 137 klass::FIELDS.values.each do |field_info| 138 field_accessor(klass, field_info) 139 qmark_isset_method(klass, field_info) 140 end 141 end
Source
# File lib/thrift/union.rb 25 def initialize(name = nil, value = nil) 26 if name 27 if name.is_a? Hash 28 if name.size > 1 29 raise "#{self.class} cannot be instantiated with more than one field!" 30 end 31 32 name, value = name.keys.first, name.values.first 33 end 34 35 if Thrift.type_checking 36 raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s) 37 end 38 39 if value.nil? 40 raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!" 41 end 42 43 Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 44 elsif !value.nil? 45 raise Exception, "Value provided, but no name!" 46 end 47 @setfield = name 48 @value = value 49 end
Source
# File lib/thrift/union.rb 130 def self.qmark_isset_method(klass, field_info) 131 klass.send :define_method, "#{field_info[:name]}?" do 132 get_set_field == field_info[:name].to_sym && !get_value.nil? 133 end 134 end
Public Instance Methods
Source
# File lib/thrift/union.rb 155 def <=>(other) 156 if self.class == other.class 157 if get_set_field == other.get_set_field 158 if get_set_field.nil? 159 0 160 else 161 get_value <=> other.get_value 162 end 163 else 164 if get_set_field && other.get_set_field.nil? 165 -1 166 elsif get_set_field.nil? && other.get_set_field 167 1 168 elsif get_set_field.nil? && other.get_set_field.nil? 169 0 170 else 171 name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s) 172 end 173 end 174 else 175 self.class <=> other.class 176 end 177 end
Source
# File lib/thrift/union.rb 105 def ==(other) 106 other.equal?(self) || (other.instance_of?(self.class) && @setfield == other.get_set_field && @value == other.get_value) 107 end
Also aliased as: eql?
Source
# File lib/thrift/union.rb 144 def get_set_field 145 @setfield 146 end
get the symbol that indicates what the currently set field type is.
Source
# File lib/thrift/union.rb 151 def get_value 152 @value 153 end
get the current value of this union, regardless of what the set field is. generally, you should only use this method when you don’t know in advance what field to expect.
Source
# File lib/thrift/union.rb 110 def hash 111 [self.class.name, @setfield, @value].hash 112 end
Source
# File lib/thrift/union.rb 51 def inspect 52 if get_set_field 53 "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>" 54 else 55 "<#{self.class} >" 56 end 57 end
Source
# File lib/thrift/union.rb 59 def read(iprot, remaining_depth = DEFAULT_RECURSION_DEPTH) 60 raise ProtocolException.new(ProtocolException::DEPTH_LIMIT, "Maximum recursion depth exceeded") if remaining_depth <= 0 61 @setfield = nil 62 @value = nil 63 64 iprot.read_struct_begin 65 _, ftype, fid = iprot.read_field_begin 66 handle_message(iprot, fid, ftype, remaining_depth) 67 iprot.read_field_end 68 69 _, ftype, _ = iprot.read_field_begin 70 unless (ftype == Types::STOP) 71 raise ProtocolException.new(ProtocolException::INVALID_DATA, "Too many fields for union") 72 end 73 74 iprot.read_struct_end 75 validate 76 end
Source
# File lib/thrift/union.rb 78 def write(oprot, remaining_depth = DEFAULT_RECURSION_DEPTH) 79 raise ProtocolException.new(ProtocolException::DEPTH_LIMIT, "Maximum recursion depth exceeded") if remaining_depth <= 0 80 validate 81 oprot.write_struct_begin(self.class.name) 82 83 fid = name_to_id(@setfield.to_s) 84 85 field_info = struct_fields[fid] 86 unless field_info 87 raise ProtocolException.new(ProtocolException::INVALID_DATA, "set_field is not valid for this union!") 88 end 89 90 type = field_info[:type] 91 if is_container? type 92 oprot.write_field_begin(@setfield, type, fid) 93 write_container(oprot, @value, field_info, remaining_depth) 94 oprot.write_field_end 95 elsif type == Types::STRUCT 96 oprot.write_field(field_info, fid, @value, remaining_depth) 97 else 98 oprot.write_field(@setfield, type, fid, @value) 99 end 100 101 oprot.write_field_stop 102 oprot.write_struct_end 103 end
Protected Instance Methods
Source
# File lib/thrift/union.rb 181 def handle_message(iprot, fid, ftype, remaining_depth) 182 field = struct_fields[fid] 183 if field and field[:type] == ftype 184 @value = read_field(iprot, field, remaining_depth) 185 name = field[:name].to_sym 186 @setfield = name 187 else 188 iprot.skip(ftype) 189 end 190 end