Class | Thrift::Union |
In: |
ruby/lib/thrift/union.rb
|
Parent: | Object |
# File ruby/lib/thrift/union.rb, line 101 101: def self.field_accessor(klass, field_info) 102: klass.send :define_method, field_info[:name] do 103: if field_info[:name].to_sym == @setfield 104: @value 105: else 106: raise RuntimeError, "#{field_info[:name]} is not union's set field." 107: end 108: end 109: 110: klass.send :define_method, "#{field_info[:name]}=" do |value| 111: Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking 112: @setfield = field_info[:name].to_sym 113: @value = value 114: end 115: end
# File ruby/lib/thrift/union.rb, line 123 123: def self.generate_accessors(klass) 124: klass::FIELDS.values.each do |field_info| 125: field_accessor(klass, field_info) 126: qmark_isset_method(klass, field_info) 127: end 128: end
# File ruby/lib/thrift/union.rb, line 22 22: def initialize(name=nil, value=nil) 23: if name 24: if name.is_a? Hash 25: if name.size > 1 26: raise "#{self.class} cannot be instantiated with more than one field!" 27: end 28: 29: name, value = name.keys.first, name.values.first 30: end 31: 32: if Thrift.type_checking 33: raise Exception, "#{self.class} does not contain a field named #{name}!" unless name_to_id(name.to_s) 34: end 35: 36: if value.nil? 37: raise Exception, "Union #{self.class} cannot be instantiated with setfield and nil value!" 38: end 39: 40: Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking 41: elsif !value.nil? 42: raise Exception, "Value provided, but no name!" 43: end 44: @setfield = name 45: @value = value 46: end
# File ruby/lib/thrift/union.rb, line 117 117: def self.qmark_isset_method(klass, field_info) 118: klass.send :define_method, "#{field_info[:name]}?" do 119: get_set_field == field_info[:name].to_sym && !get_value.nil? 120: end 121: end
# File ruby/lib/thrift/union.rb, line 142 142: def <=>(other) 143: if self.class == other.class 144: if get_set_field == other.get_set_field 145: if get_set_field.nil? 146: 0 147: else 148: get_value <=> other.get_value 149: end 150: else 151: if get_set_field && other.get_set_field.nil? 152: -1 153: elsif get_set_field.nil? && other.get_set_field 154: 1 155: elsif get_set_field.nil? && other.get_set_field.nil? 156: 0 157: else 158: name_to_id(get_set_field.to_s) <=> name_to_id(other.get_set_field.to_s) 159: end 160: end 161: else 162: self.class <=> other.class 163: end 164: end
# File ruby/lib/thrift/union.rb, line 89 89: def ==(other) 90: other != nil && @setfield == other.get_set_field && @value == other.get_value 91: end
# File ruby/lib/thrift/union.rb, line 93 93: def eql?(other) 94: self.class == other.class && self == other 95: end
get the symbol that indicates what the currently set field type is.
# File ruby/lib/thrift/union.rb, line 131 131: def get_set_field 132: @setfield 133: 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.
# File ruby/lib/thrift/union.rb, line 138 138: def get_value 139: @value 140: end
# File ruby/lib/thrift/union.rb, line 97 97: def hash 98: [self.class.name, @setfield, @value].hash 99: end
# File ruby/lib/thrift/union.rb, line 48 48: def inspect 49: if get_set_field 50: "<#{self.class} #{@setfield}: #{inspect_field(@value, struct_fields[name_to_id(@setfield.to_s)])}>" 51: else 52: "<#{self.class} >" 53: end 54: end
# File ruby/lib/thrift/union.rb, line 56 56: def read(iprot) 57: iprot.read_struct_begin 58: fname, ftype, fid = iprot.read_field_begin 59: handle_message(iprot, fid, ftype) 60: iprot.read_field_end 61: 62: fname, ftype, fid = iprot.read_field_begin 63: raise "Too many fields for union" unless (ftype == Types::STOP) 64: 65: iprot.read_struct_end 66: validate 67: end
# File ruby/lib/thrift/union.rb, line 69 69: def write(oprot) 70: validate 71: oprot.write_struct_begin(self.class.name) 72: 73: fid = self.name_to_id(@setfield.to_s) 74: 75: field_info = struct_fields[fid] 76: type = field_info[:type] 77: if is_container? type 78: oprot.write_field_begin(@setfield, type, fid) 79: write_container(oprot, @value, field_info) 80: oprot.write_field_end 81: else 82: oprot.write_field(@setfield, type, fid, @value) 83: end 84: 85: oprot.write_field_stop 86: oprot.write_struct_end 87: end
# File ruby/lib/thrift/union.rb, line 168 168: def handle_message(iprot, fid, ftype) 169: field = struct_fields[fid] 170: if field and field[:type] == ftype 171: @value = read_field(iprot, field) 172: name = field[:name].to_sym 173: @setfield = name 174: else 175: iprot.skip(ftype) 176: end 177: end