Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed
## 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.#require 'set'module Thriftmodule TypesSTOP = 0VOID = 1BOOL = 2BYTE = 3DOUBLE = 4I16 = 6I32 = 8I64 = 10STRING = 11STRUCT = 12MAP = 13SET = 14LIST = 15endclass << selfattr_accessor :type_checkingendclass TypeError < Exceptionenddef self.check_type(value, field, name, skip_nil=true)return if value.nil? and skip_nilklasses = case field[:type]when Types::VOIDNilClasswhen Types::BOOL[TrueClass, FalseClass]when Types::BYTE, Types::I16, Types::I32, Types::I64Integerwhen Types::DOUBLEFloatwhen Types::STRINGStringwhen Types::STRUCTStructwhen Types::MAPHashwhen Types::SETSetwhen Types::LISTArrayendvalid = klasses && [*klasses].any? { |klass| klass === value }raise TypeError, "Expected #{type_name(field[:type])}, received #{value.class} for field #{name}" unless valid# check elements nowcase field[:type]when Types::MAPvalue.each_pair do |k,v|check_type(k, field[:key], "#{name}.key", false)check_type(v, field[:value], "#{name}.value", false)endwhen Types::SET, Types::LISTvalue.each do |el|check_type(el, field[:element], "#{name}.element", false)endwhen Types::STRUCTraise TypeError, "Expected #{field[:class]}, received #{value.class} for field #{name}" unless field[:class] == value.classendenddef self.type_name(type)Types.constants.each do |const|return "Types::#{const}" if Types.const_get(const) == typeendnilendmodule MessageTypesCALL = 1REPLY = 2EXCEPTION = 3ONEWAY = 4endendThrift.type_checking = false if Thrift.type_checking.nil?