]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/rb/lib/thrift/types.rb
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / rb / lib / thrift / types.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
20 require 'set'
21
22 module Thrift
23 module Types
24 STOP = 0
25 VOID = 1
26 BOOL = 2
27 BYTE = 3
28 DOUBLE = 4
29 I16 = 6
30 I32 = 8
31 I64 = 10
32 STRING = 11
33 STRUCT = 12
34 MAP = 13
35 SET = 14
36 LIST = 15
37 end
38
39 class << self
40 attr_accessor :type_checking
41 end
42
43 class TypeError < Exception
44 end
45
46 def self.check_type(value, field, name, skip_nil=true)
47 return if value.nil? and skip_nil
48 klasses = case field[:type]
49 when Types::VOID
50 NilClass
51 when Types::BOOL
52 [TrueClass, FalseClass]
53 when Types::BYTE, Types::I16, Types::I32, Types::I64
54 Integer
55 when Types::DOUBLE
56 Float
57 when Types::STRING
58 String
59 when Types::STRUCT
60 [Struct, Union]
61 when Types::MAP
62 Hash
63 when Types::SET
64 Set
65 when Types::LIST
66 Array
67 end
68 valid = klasses && [*klasses].any? { |klass| klass === value }
69 raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid
70 # check elements now
71 case field[:type]
72 when Types::MAP
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)
76 end
77 when Types::SET, Types::LIST
78 value.each do |el|
79 check_type(el, field[:element], "#{name}.element", false)
80 end
81 when Types::STRUCT
82 raise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.class
83 end
84 end
85
86 def self.type_name(type)
87 Types.constants.each do |const|
88 return "Types::#{const}" if Types.const_get(const) == type
89 end
90 nil
91 end
92
93 module MessageTypes
94 CALL = 1
95 REPLY = 2
96 EXCEPTION = 3
97 ONEWAY = 4
98 end
99 end
100
101 Thrift.type_checking = false if Thrift.type_checking.nil?