]>
git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/types.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
40 attr_accessor
:type_checking
43 class TypeError
< Exception
46 def self.check_type(value
, field
, name
, skip_nil
=true)
47 return if value
.nil? and skip_nil
48 klasses
= case field
[:type]
52 [TrueClass
, FalseClass
]
53 when Types
::BYTE, Types
::I16, Types
::I32, Types
::I64
68 valid
= klasses
&& [*klasses
].any
? { |klass
| klass
=== value
}
69 raise TypeError
, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
73 value
.each_pair
do |k
,v
|
74 check_type(k
, field
[:key], "#{name}.key", false)
75 check_type(v
, field
[:value], "#{name}.value", false)
77 when Types
::SET, Types
::LIST
79 check_type(el
, field
[:element], "#{name}.element", false)
82 raise TypeError
, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field
[:class] == value
.class
86 def self.type_name(type
)
87 Types
.constants
.each
do |const
|
88 return "Types::#{const}" if Types
.const_get(const
) == type
101 Thrift
.type_checking
= false if Thrift
.type_checking
.nil?