]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/struct_union.rb
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / rb / lib / thrift / struct_union.rb
1 #
2 # Licensed to the Apache Software Foundation (ASF) under one
3 # or more contributor license agreements. See the NOTICE file
4 # distributed with this work for additional information
5 # regarding copyright ownership. The ASF licenses this file
6 # to you under the Apache License, Version 2.0 (the
7 # "License"); you may not use this file except in compliance
8 # with the License. You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing,
13 # software distributed under the License is distributed on an
14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 # KIND, either express or implied. See the License for the
16 # specific language governing permissions and limitations
17 # under the License.
18 #
19 require 'set'
20
21 module Thrift
22 module Struct_Union
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
34
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
43
44 def each_field
45 sorted_field_ids.each do |fid|
46 data = struct_fields[fid]
47 yield fid, data
48 end
49 end
50
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
108
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
116
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
142
143 CONTAINER_TYPES = []
144 CONTAINER_TYPES[Types::LIST] = true
145 CONTAINER_TYPES[Types::MAP] = true
146 CONTAINER_TYPES[Types::SET] = true
147 def is_container?(type)
148 CONTAINER_TYPES[type]
149 end
150
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
158
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
183
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
191 end
192 end