]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/struct_union.rb
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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
24 names_to_ids
= self.class.instance_variable_get(:@names_to_ids)
27 struct_fields
.each
do |fid
, field_def
|
28 names_to_ids
[field_def
[:name]] = fid
30 self.class.instance_variable_set(:@names_to_ids, names_to_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
)
45 sorted_field_ids
.each
do |fid
|
46 data = struct_fields
[fid
]
51 def read_field(iprot
, field
= {})
54 value
= field
[:class].new
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]))
68 k
= read_field(iprot
, field_info(field
[:key]))
69 v
= read_field(iprot
, field_info(field
[:value]))
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])
83 value
= Array
.new(size
) do |n
|
84 read_field(iprot
, field_info(field
[:element]))
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])
98 element
= read_field(iprot
, field_info(field
[:element]))
104 value
= iprot
.read_type(field
)
109 def write_data(oprot
, value
, field
)
110 if is_container
? field
[:type]
111 write_container(oprot
, value
, field
)
113 oprot
.write_type(field
, value
)
117 def write_container(oprot
, value
, field
= {})
120 oprot
.write_map_begin(field
[:key][:type], field
[:value][:type], value
.size
)
122 write_data(oprot
, k
, field
[:key])
123 write_data(oprot
, v
, field
[:value])
127 oprot
.write_list_begin(field
[:element][:type], value
.size
)
129 write_data(oprot
, elem
, field
[:element])
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])
139 raise "Not a container type: #{field[:type]}"
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
]
151 def field_info(field
)
152 { :type => field
[:type],
153 :class => field
[:class],
155 :value => field
[:value],
156 :element => field
[:element] }
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
166 map_buf
<< inspect_field(k
, field_info
[:key]) + ": " + inspect_field(v
, field_info
[:value])
168 "{" + map_buf
.join(", ") + "}"
171 inspect_collection(value
.keys
, field_info
)
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
184 def inspect_collection(collection
, field_info
)
186 collection
.each
do |k
|
187 buf
<< inspect_field(k
, field_info
[:element])
189 "[" + buf
.join(", ") + "]"