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