| Module | Thrift::Struct |
| In: |
ruby/lib/thrift/struct.rb
|
# File ruby/lib/thrift/struct.rb, line 148
148: def self.field_accessor(klass, field_info)
149: field_name_sym = field_info[:name].to_sym
150: klass.send :attr_reader, field_name_sym
151: klass.send :define_method, "#{field_info[:name]}=" do |value|
152: Thrift.check_type(value, field_info, field_info[:name]) if Thrift.type_checking
153: instance_variable_set("@#{field_name_sym}", value)
154: end
155: end
# File ruby/lib/thrift/struct.rb, line 157
157: def self.generate_accessors(klass)
158: klass::FIELDS.values.each do |field_info|
159: field_accessor(klass, field_info)
160: qmark_isset_method(klass, field_info)
161: end
162: end
# File ruby/lib/thrift/struct.rb, line 24
24: def initialize(d={})
25: # get a copy of the default values to work on, removing defaults in favor of arguments
26: fields_with_defaults = fields_with_default_values.dup
27:
28: # check if the defaults is empty, or if there are no parameters for this
29: # instantiation, and if so, don't bother overriding defaults.
30: unless fields_with_defaults.empty? || d.empty?
31: d.each_key do |name|
32: fields_with_defaults.delete(name.to_s)
33: end
34: end
35:
36: # assign all the user-specified arguments
37: unless d.empty?
38: d.each do |name, value|
39: unless name_to_id(name.to_s)
40: raise Exception, "Unknown key given to #{self.class}.new: #{name}"
41: end
42: Thrift.check_type(value, struct_fields[name_to_id(name.to_s)], name) if Thrift.type_checking
43: instance_variable_set("@#{name}", value)
44: end
45: end
46:
47: # assign all the default values
48: unless fields_with_defaults.empty?
49: fields_with_defaults.each do |name, default_value|
50: instance_variable_set("@#{name}", (default_value.dup rescue default_value))
51: end
52: end
53: end
# File ruby/lib/thrift/struct.rb, line 164
164: def self.qmark_isset_method(klass, field_info)
165: klass.send :define_method, "#{field_info[:name]}?" do
166: !self.send(field_info[:name].to_sym).nil?
167: end
168: end
# File ruby/lib/thrift/struct.rb, line 196
196: def self.append_features(mod)
197: if mod.ancestors.include? ::Exception
198: mod.send :class_variable_set, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize', mod.instance_method(:initialize)
199: super
200: # set up our custom initializer so `raise Xception, 'message'` works
201: mod.send :define_method, :struct_initialize, mod.instance_method(:initialize)
202: mod.send :define_method, :initialize, mod.instance_method(:exception_initialize)
203: else
204: super
205: end
206: end
# File ruby/lib/thrift/struct.rb, line 170
170: def <=>(other)
171: if self.class == other.class
172: each_field do |fid, field_info|
173: v1 = self.send(field_info[:name])
174: v1_set = !v1.nil?
175: v2 = other.send(field_info[:name])
176: v2_set = !v2.nil?
177: if v1_set && !v2_set
178: return -1
179: elsif !v1_set && v2_set
180: return 1
181: elsif v1_set && v2_set
182: cmp = v1 <=> v2
183: if cmp != 0
184: return cmp
185: end
186: end
187: end
188: 0
189: else
190: self.class <=> other.class
191: end
192: end
# File ruby/lib/thrift/struct.rb, line 114
114: def ==(other)
115: each_field do |fid, field_info|
116: name = field_info[:name]
117: return false unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
118: end
119: true
120: end
# File ruby/lib/thrift/struct.rb, line 135
135: def differences(other)
136: diffs = []
137: unless other.is_a?(self.class)
138: diffs << "Different class!"
139: else
140: each_field do |fid, field_info|
141: name = field_info[:name]
142: diffs << "#{name} differs!" unless self.instance_variable_get("@#{name}") == other.instance_variable_get("@#{name}")
143: end
144: end
145: diffs
146: end
# File ruby/lib/thrift/struct.rb, line 122
122: def eql?(other)
123: self.class == other.class && self == other
124: end
# File ruby/lib/thrift/struct.rb, line 55
55: def fields_with_default_values
56: fields_with_default_values = self.class.instance_variable_get("@fields_with_default_values")
57: unless fields_with_default_values
58: fields_with_default_values = {}
59: struct_fields.each do |fid, field_def|
60: unless field_def[:default].nil?
61: fields_with_default_values[field_def[:name]] = field_def[:default]
62: end
63: end
64: self.class.instance_variable_set("@fields_with_default_values", fields_with_default_values)
65: end
66: fields_with_default_values
67: end
# File ruby/lib/thrift/struct.rb, line 126
126: def hash
127: field_values = []
128: each_field do |fid, field_info|
129: name = field_info[:name]
130: field_values << self.instance_variable_get("@#{name}")
131: end
132: field_values.hash
133: end
# File ruby/lib/thrift/struct.rb, line 69
69: def inspect(skip_optional_nulls = true)
70: fields = []
71: each_field do |fid, field_info|
72: name = field_info[:name]
73: value = instance_variable_get("@#{name}")
74: unless skip_optional_nulls && field_info[:optional] && value.nil?
75: fields << "#{name}:#{inspect_field(value, field_info)}"
76: end
77: end
78: "<#{self.class} #{fields.join(", ")}>"
79: end
# File ruby/lib/thrift/struct.rb, line 81
81: def read(iprot)
82: iprot.read_struct_begin
83: loop do
84: fname, ftype, fid = iprot.read_field_begin
85: break if (ftype == Types::STOP)
86: handle_message(iprot, fid, ftype)
87: iprot.read_field_end
88: end
89: iprot.read_struct_end
90: validate
91: end
# File ruby/lib/thrift/struct.rb, line 93
93: def write(oprot)
94: validate
95: oprot.write_struct_begin(self.class.name)
96: each_field do |fid, field_info|
97: name = field_info[:name]
98: type = field_info[:type]
99: value = instance_variable_get("@#{name}")
100: unless value.nil?
101: if is_container? type
102: oprot.write_field_begin(name, type, fid)
103: write_container(oprot, value, field_info)
104: oprot.write_field_end
105: else
106: oprot.write_field(name, type, fid, value)
107: end
108: end
109: end
110: oprot.write_field_stop
111: oprot.write_struct_end
112: end
# File ruby/lib/thrift/struct.rb, line 208
208: def exception_initialize(*args, &block)
209: if args.size == 1 and args.first.is_a? Hash
210: # looks like it's a regular Struct initialize
211: method(:struct_initialize).call(args.first)
212: else
213: # call the Struct initializer first with no args
214: # this will set our field default values
215: method(:struct_initialize).call()
216: # now give it to the exception
217: self.class.send(:class_variable_get, '@@__thrift_struct_real_initialize''@@__thrift_struct_real_initialize').bind(self).call(*args, &block) if args.size > 0
218: # self.class.instance_method(:initialize).bind(self).call(*args, &block)
219: end
220: end
# File ruby/lib/thrift/struct.rb, line 222
222: def handle_message(iprot, fid, ftype)
223: field = struct_fields[fid]
224: if field and field[:type] == ftype
225: value = read_field(iprot, field)
226: instance_variable_set("@#{field[:name]}", value)
227: else
228: iprot.skip(ftype)
229: end
230: end