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