encoding: ascii-8bit
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
type_checking | [RW] | |
type_checking | [RW] |
# File lib/thrift/types.rb, line 46 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
# File lib/thrift/types.rb, line 46 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
# File lib/thrift/types.rb, line 86 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