module Thrift::Struct_Union
Constants
- CONTAINER_TYPES
Public Instance Methods
each_field() { |fid, data| ... }
click to toggle source
# File lib/thrift/struct_union.rb 44 def each_field 45 sorted_field_ids.each do |fid| 46 data = struct_fields[fid] 47 yield fid, data 48 end 49 end
field_info(field)
click to toggle source
# File lib/thrift/struct_union.rb 151 def field_info(field) 152 { :type => field[:type], 153 :class => field[:class], 154 :key => field[:key], 155 :value => field[:value], 156 :element => field[:element] } 157 end
inspect_collection(collection, field_info)
click to toggle source
# File lib/thrift/struct_union.rb 184 def inspect_collection(collection, field_info) 185 buf = [] 186 collection.each do |k| 187 buf << inspect_field(k, field_info[:element]) 188 end 189 "[" + buf.join(", ") + "]" 190 end
inspect_field(value, field_info)
click to toggle source
# File lib/thrift/struct_union.rb 159 def inspect_field(value, field_info) 160 if enum_class = field_info[:enum_class] 161 "#{enum_class.const_get(:VALUE_MAP)[value]} (#{value})" 162 elsif value.is_a? Hash 163 if field_info[:type] == Types::MAP 164 map_buf = [] 165 value.each do |k, v| 166 map_buf << inspect_field(k, field_info[:key]) + ": " + inspect_field(v, field_info[:value]) 167 end 168 "{" + map_buf.join(", ") + "}" 169 else 170 # old-style set 171 inspect_collection(value.keys, field_info) 172 end 173 elsif value.is_a? Array 174 inspect_collection(value, field_info) 175 elsif value.is_a? Set 176 inspect_collection(value, field_info) 177 elsif value.is_a?(String) && field_info[:binary] 178 value.unpack("H*").first 179 else 180 value.inspect 181 end 182 end
is_container?(type)
click to toggle source
# File lib/thrift/struct_union.rb 147 def is_container?(type) 148 CONTAINER_TYPES[type] 149 end
name_to_id(name)
click to toggle source
# File lib/thrift/struct_union.rb 23 def name_to_id(name) 24 names_to_ids = self.class.instance_variable_get(:@names_to_ids) 25 unless names_to_ids 26 names_to_ids = {} 27 struct_fields.each do |fid, field_def| 28 names_to_ids[field_def[:name]] = fid 29 end 30 self.class.instance_variable_set(:@names_to_ids, names_to_ids) 31 end 32 names_to_ids[name] 33 end
read_field(iprot, field = {})
click to toggle source
# File lib/thrift/struct_union.rb 51 def read_field(iprot, field = {}) 52 case field[:type] 53 when Types::STRUCT 54 value = field[:class].new 55 value.read(iprot) 56 when Types::MAP 57 key_type, val_type, size = iprot.read_map_begin 58 # Skip the map contents if the declared key or value types don't match the expected ones. 59 if (size != 0 && (key_type != field[:key][:type] || val_type != field[:value][:type])) 60 size.times do 61 iprot.skip(key_type) 62 iprot.skip(val_type) 63 end 64 value = nil 65 else 66 value = {} 67 size.times do 68 k = read_field(iprot, field_info(field[:key])) 69 v = read_field(iprot, field_info(field[:value])) 70 value[k] = v 71 end 72 end 73 iprot.read_map_end 74 when Types::LIST 75 e_type, size = iprot.read_list_begin 76 # Skip the list contents if the declared element type doesn't match the expected one. 77 if (e_type != field[:element][:type]) 78 size.times do 79 iprot.skip(e_type) 80 end 81 value = nil 82 else 83 value = Array.new(size) do |n| 84 read_field(iprot, field_info(field[:element])) 85 end 86 end 87 iprot.read_list_end 88 when Types::SET 89 e_type, size = iprot.read_set_begin 90 # Skip the set contents if the declared element type doesn't match the expected one. 91 if (e_type != field[:element][:type]) 92 size.times do 93 iprot.skip(e_type) 94 end 95 else 96 value = Set.new 97 size.times do 98 element = read_field(iprot, field_info(field[:element])) 99 value << element 100 end 101 end 102 iprot.read_set_end 103 else 104 value = iprot.read_type(field) 105 end 106 value 107 end
sorted_field_ids()
click to toggle source
# File lib/thrift/struct_union.rb 35 def sorted_field_ids 36 sorted_field_ids = self.class.instance_variable_get(:@sorted_field_ids) 37 unless sorted_field_ids 38 sorted_field_ids = struct_fields.keys.sort 39 self.class.instance_variable_set(:@sorted_field_ids, sorted_field_ids) 40 end 41 sorted_field_ids 42 end
write_container(oprot, value, field = {})
click to toggle source
# File lib/thrift/struct_union.rb 117 def write_container(oprot, value, field = {}) 118 case field[:type] 119 when Types::MAP 120 oprot.write_map_begin(field[:key][:type], field[:value][:type], value.size) 121 value.each do |k, v| 122 write_data(oprot, k, field[:key]) 123 write_data(oprot, v, field[:value]) 124 end 125 oprot.write_map_end 126 when Types::LIST 127 oprot.write_list_begin(field[:element][:type], value.size) 128 value.each do |elem| 129 write_data(oprot, elem, field[:element]) 130 end 131 oprot.write_list_end 132 when Types::SET 133 oprot.write_set_begin(field[:element][:type], value.size) 134 value.each do |v,| # the , is to preserve compatibility with the old Hash-style sets 135 write_data(oprot, v, field[:element]) 136 end 137 oprot.write_set_end 138 else 139 raise "Not a container type: #{field[:type]}" 140 end 141 end
write_data(oprot, value, field)
click to toggle source
# File lib/thrift/struct_union.rb 109 def write_data(oprot, value, field) 110 if is_container? field[:type] 111 write_container(oprot, value, field) 112 else 113 oprot.write_type(field, value) 114 end 115 end