From 5e6637b0ffdf9130e7cac012a5c42235d2f74f7d Mon Sep 17 00:00:00 2001 From: David Reiss Date: Mon, 30 Aug 2010 22:05:18 +0000 Subject: [PATCH] erlang: Add some initial specs to thrift_client and thrift_protocol Also add a special header for use in thrift_protocol implementations that gives specs for the callbacks. git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@990972 13f79535-47bb-0310-9956-ffa450edef68 --- lib/erl/include/thrift_protocol_impl.hrl | 31 ++++++++++++++++++++++++ lib/erl/src/thrift_binary_protocol.erl | 2 ++ lib/erl/src/thrift_client.erl | 4 +++ lib/erl/src/thrift_protocol.erl | 8 ++++++ 4 files changed, 45 insertions(+) create mode 100644 lib/erl/include/thrift_protocol_impl.hrl diff --git a/lib/erl/include/thrift_protocol_impl.hrl b/lib/erl/include/thrift_protocol_impl.hrl new file mode 100644 index 00000000..7d8abae0 --- /dev/null +++ b/lib/erl/include/thrift_protocol_impl.hrl @@ -0,0 +1,31 @@ +%% +%% 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. +%% + +%% Signature specifications for protocol implementations. + +-ifndef(THRIFT_PROTOCOL_IMPL_INCLUDED). +-define(THRIFT_PROTOCOL_IMPL_INCLUDED, true). + +-spec flush_transport(state()) -> ok. +-spec close_transport(state()) -> ok. +-spec write(state(), term()) -> ok | {error, _Reason}. +-spec read(state(), term()) -> term(). + + +-endif. diff --git a/lib/erl/src/thrift_binary_protocol.erl b/lib/erl/src/thrift_binary_protocol.erl index a681acfa..a898dfe6 100644 --- a/lib/erl/src/thrift_binary_protocol.erl +++ b/lib/erl/src/thrift_binary_protocol.erl @@ -37,6 +37,8 @@ strict_read=true, strict_write=true }). +-type state() :: #binary_protocol{}. +-include("thrift_protocol_impl.hrl"). -define(VERSION_MASK, 16#FFFF0000). -define(VERSION_1, 16#80010000). diff --git a/lib/erl/src/thrift_client.erl b/lib/erl/src/thrift_client.erl index 9790250a..07c9f488 100644 --- a/lib/erl/src/thrift_client.erl +++ b/lib/erl/src/thrift_client.erl @@ -140,6 +140,7 @@ start(ProtocolFactory, Service, ClientOpts) Started end. +-spec call(term(), atom(), list()) -> {ok, term()} | {error, term()}. call(Client, Function, Args) when is_pid(Client), is_atom(Function), is_list(Args) -> case gen_server:call(Client, {call, Function, Args}) of @@ -148,6 +149,7 @@ call(Client, Function, Args) {exception, Exception} -> throw(Exception) end. +-spec cast(term(), atom(), list()) -> ok. cast(Client, Function, Args) when is_pid(Client), is_atom(Function), is_list(Args) -> gen_server:cast(Client, {call, Function, Args}). @@ -155,10 +157,12 @@ cast(Client, Function, Args) %% Sends a function call but does not read the result. This is useful %% if you're trying to log non-oneway function calls to write-only %% transports like thrift_disk_log_transport. +-spec send_call(term(), atom(), list()) -> ok. send_call(Client, Function, Args) when is_pid(Client), is_atom(Function), is_list(Args) -> gen_server:call(Client, {send_call, Function, Args}). +-spec close(term()) -> ok. close(Client) when is_pid(Client) -> gen_server:cast(Client, close). diff --git a/lib/erl/src/thrift_protocol.erl b/lib/erl/src/thrift_protocol.erl index 1bfb0a42..3ccb4eeb 100644 --- a/lib/erl/src/thrift_protocol.erl +++ b/lib/erl/src/thrift_protocol.erl @@ -49,10 +49,12 @@ new(Module, Data) when is_atom(Module) -> {ok, #protocol{module = Module, data = Data}}. +-spec flush_transport(#protocol{}) -> ok. flush_transport(#protocol{module = Module, data = Data}) -> Module:flush_transport(Data). +-spec close_transport(#protocol{}) -> ok. close_transport(#protocol{module = Module, data = Data}) -> Module:close_transport(Data). @@ -86,6 +88,7 @@ term_to_typeid({list, _}) -> ?tType_LIST. %% Structure is like: %% [{Fid, Type}, ...] +-spec read(#protocol{}, {struct, _StructDef}, atom()) -> {ok, tuple()}. read(IProto, {struct, Structure}, Tag) when is_list(Structure), is_atom(Tag) -> @@ -112,6 +115,8 @@ read(IProto, {struct, Structure}, Tag) RTuple2 = read_struct_loop(IProto, SDict, RTuple1), {ok, RTuple2}. +-spec read(#protocol{}, term()) -> term(). + read(IProto, {struct, {Module, StructureName}}) when is_atom(Module), is_atom(StructureName) -> read(IProto, Module:struct_info(StructureName), StructureName); @@ -183,6 +188,7 @@ skip_field(FType, IProto, SDict, RTuple) -> read(IProto, field_end), read_struct_loop(IProto, SDict, RTuple). +-spec skip(#protocol{}, term()) -> ok. skip(Proto, struct) -> ok = read(Proto, struct_begin), @@ -271,6 +277,8 @@ skip_list_loop(Proto, Map = #protocol_list_begin{etype = Etype, %% %% Description: %%-------------------------------------------------------------------- +-spec write(#protocol{}, term()) -> ok | {error, _Reason}. + write(Proto, {{struct, StructDef}, Data}) when is_list(StructDef), is_tuple(Data), length(StructDef) == size(Data) - 1 -> -- 2.17.1