Module Thrift::Struct_Union
In: lib/thrift/struct_union.rb
lib/thrift/struct_union.rb

Methods

Constants

CONTAINER_TYPES = []
CONTAINER_TYPES = []

Public Instance methods

[Source]

    # File lib/thrift/struct_union.rb, line 35
35:     def each_field
36:       struct_fields.keys.sort.each do |fid|
37:         data = struct_fields[fid]
38:         yield fid, data
39:       end
40:     end

[Source]

    # File lib/thrift/struct_union.rb, line 35
35:     def each_field
36:       struct_fields.keys.sort.each do |fid|
37:         data = struct_fields[fid]
38:         yield fid, data
39:       end
40:     end

[Source]

     # File lib/thrift/struct_union.rb, line 118
118:     def field_info(field)
119:       { :type => field[:type],
120:         :class => field[:class],
121:         :key => field[:key],
122:         :value => field[:value],
123:         :element => field[:element] }
124:     end

[Source]

     # File lib/thrift/struct_union.rb, line 118
118:     def field_info(field)
119:       { :type => field[:type],
120:         :class => field[:class],
121:         :key => field[:key],
122:         :value => field[:value],
123:         :element => field[:element] }
124:     end

[Source]

     # File lib/thrift/struct_union.rb, line 151
151:     def inspect_collection(collection, field_info)
152:       buf = []
153:       collection.each do |k|
154:         buf << inspect_field(k, field_info[:element])
155:       end
156:       "[" + buf.join(", ") + "]"      
157:     end

[Source]

     # File lib/thrift/struct_union.rb, line 151
151:     def inspect_collection(collection, field_info)
152:       buf = []
153:       collection.each do |k|
154:         buf << inspect_field(k, field_info[:element])
155:       end
156:       "[" + buf.join(", ") + "]"      
157:     end

[Source]

     # File lib/thrift/struct_union.rb, line 126
126:     def inspect_field(value, field_info)
127:       if enum_class = field_info[:enum_class]
128:         "#{enum_class.const_get(:VALUE_MAP)[value]} (#{value})"
129:       elsif value.is_a? Hash 
130:         if field_info[:type] == Types::MAP
131:           map_buf = []
132:           value.each do |k, v|
133:             map_buf << inspect_field(k, field_info[:key]) + ": " + inspect_field(v, field_info[:value])
134:           end
135:           "{" + map_buf.join(", ") + "}"
136:         else
137:           # old-style set
138:           inspect_collection(value.keys, field_info)
139:         end
140:       elsif value.is_a? Array
141:         inspect_collection(value, field_info)
142:       elsif value.is_a? Set
143:         inspect_collection(value, field_info)
144:       elsif value.is_a?(String) && field_info[:binary]
145:         value.unpack("H*").first
146:       else
147:         value.inspect
148:       end
149:     end

[Source]

     # File lib/thrift/struct_union.rb, line 126
126:     def inspect_field(value, field_info)
127:       if enum_class = field_info[:enum_class]
128:         "#{enum_class.const_get(:VALUE_MAP)[value]} (#{value})"
129:       elsif value.is_a? Hash 
130:         if field_info[:type] == Types::MAP
131:           map_buf = []
132:           value.each do |k, v|
133:             map_buf << inspect_field(k, field_info[:key]) + ": " + inspect_field(v, field_info[:value])
134:           end
135:           "{" + map_buf.join(", ") + "}"
136:         else
137:           # old-style set
138:           inspect_collection(value.keys, field_info)
139:         end
140:       elsif value.is_a? Array
141:         inspect_collection(value, field_info)
142:       elsif value.is_a? Set
143:         inspect_collection(value, field_info)
144:       elsif value.is_a?(String) && field_info[:binary]
145:         value.unpack("H*").first
146:       else
147:         value.inspect
148:       end
149:     end

[Source]

     # File lib/thrift/struct_union.rb, line 114
114:     def is_container?(type)
115:       CONTAINER_TYPES[type]
116:     end

[Source]

     # File lib/thrift/struct_union.rb, line 114
114:     def is_container?(type)
115:       CONTAINER_TYPES[type]
116:     end

[Source]

    # File lib/thrift/struct_union.rb, line 23
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

[Source]

    # File lib/thrift/struct_union.rb, line 23
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

[Source]

    # File lib/thrift/struct_union.rb, line 42
42:     def read_field(iprot, field = {})
43:       case field[:type]
44:       when Types::STRUCT
45:         value = field[:class].new
46:         value.read(iprot)
47:       when Types::MAP
48:         key_type, val_type, size = iprot.read_map_begin
49:         value = {}
50:         size.times do
51:           k = read_field(iprot, field_info(field[:key]))
52:           v = read_field(iprot, field_info(field[:value]))
53:           value[k] = v
54:         end
55:         iprot.read_map_end
56:       when Types::LIST
57:         e_type, size = iprot.read_list_begin
58:         value = Array.new(size) do |n|
59:           read_field(iprot, field_info(field[:element]))
60:         end
61:         iprot.read_list_end
62:       when Types::SET
63:         e_type, size = iprot.read_set_begin
64:         value = Set.new
65:         size.times do
66:           element = read_field(iprot, field_info(field[:element]))
67:           value << element
68:         end
69:         iprot.read_set_end
70:       else
71:         value = iprot.read_type(field[:type])
72:       end
73:       value
74:     end

[Source]

    # File lib/thrift/struct_union.rb, line 42
42:     def read_field(iprot, field = {})
43:       case field[:type]
44:       when Types::STRUCT
45:         value = field[:class].new
46:         value.read(iprot)
47:       when Types::MAP
48:         key_type, val_type, size = iprot.read_map_begin
49:         value = {}
50:         size.times do
51:           k = read_field(iprot, field_info(field[:key]))
52:           v = read_field(iprot, field_info(field[:value]))
53:           value[k] = v
54:         end
55:         iprot.read_map_end
56:       when Types::LIST
57:         e_type, size = iprot.read_list_begin
58:         value = Array.new(size) do |n|
59:           read_field(iprot, field_info(field[:element]))
60:         end
61:         iprot.read_list_end
62:       when Types::SET
63:         e_type, size = iprot.read_set_begin
64:         value = Set.new
65:         size.times do
66:           element = read_field(iprot, field_info(field[:element]))
67:           value << element
68:         end
69:         iprot.read_set_end
70:       else
71:         value = iprot.read_type(field[:type])
72:       end
73:       value
74:     end

[Source]

     # File lib/thrift/struct_union.rb, line 84
 84:     def write_container(oprot, value, field = {})
 85:       case field[:type]
 86:       when Types::MAP
 87:         oprot.write_map_begin(field[:key][:type], field[:value][:type], value.size)
 88:         value.each do |k, v|
 89:           write_data(oprot, k, field[:key])
 90:           write_data(oprot, v, field[:value])
 91:         end
 92:         oprot.write_map_end
 93:       when Types::LIST
 94:         oprot.write_list_begin(field[:element][:type], value.size)
 95:         value.each do |elem|
 96:           write_data(oprot, elem, field[:element])
 97:         end
 98:         oprot.write_list_end
 99:       when Types::SET
100:         oprot.write_set_begin(field[:element][:type], value.size)
101:         value.each do |v,| # the , is to preserve compatibility with the old Hash-style sets
102:           write_data(oprot, v, field[:element])
103:         end
104:         oprot.write_set_end
105:       else
106:         raise "Not a container type: #{field[:type]}"
107:       end
108:     end

[Source]

     # File lib/thrift/struct_union.rb, line 84
 84:     def write_container(oprot, value, field = {})
 85:       case field[:type]
 86:       when Types::MAP
 87:         oprot.write_map_begin(field[:key][:type], field[:value][:type], value.size)
 88:         value.each do |k, v|
 89:           write_data(oprot, k, field[:key])
 90:           write_data(oprot, v, field[:value])
 91:         end
 92:         oprot.write_map_end
 93:       when Types::LIST
 94:         oprot.write_list_begin(field[:element][:type], value.size)
 95:         value.each do |elem|
 96:           write_data(oprot, elem, field[:element])
 97:         end
 98:         oprot.write_list_end
 99:       when Types::SET
100:         oprot.write_set_begin(field[:element][:type], value.size)
101:         value.each do |v,| # the , is to preserve compatibility with the old Hash-style sets
102:           write_data(oprot, v, field[:element])
103:         end
104:         oprot.write_set_end
105:       else
106:         raise "Not a container type: #{field[:type]}"
107:       end
108:     end

[Source]

    # File lib/thrift/struct_union.rb, line 76
76:     def write_data(oprot, value, field)
77:       if is_container? field[:type]
78:         write_container(oprot, value, field)
79:       else
80:         oprot.write_type(field[:type], value)
81:       end
82:     end

[Source]

    # File lib/thrift/struct_union.rb, line 76
76:     def write_data(oprot, value, field)
77:       if is_container? field[:type]
78:         write_container(oprot, value, field)
79:       else
80:         oprot.write_type(field[:type], value)
81:       end
82:     end

[Validate]