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
diff --git a/lib/erl/include/thrift_protocol_impl.hrl b/lib/erl/include/thrift_protocol_impl.hrl
new file mode 100644
index 0000000..7d8abae
--- /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 a681acf..a898dfe 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 9790250..07c9f48 100644
--- a/lib/erl/src/thrift_client.erl
+++ b/lib/erl/src/thrift_client.erl
@@ -140,6 +140,7 @@
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 @@
{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 @@
%% 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 1bfb0a4..3ccb4ee 100644
--- a/lib/erl/src/thrift_protocol.erl
+++ b/lib/erl/src/thrift_protocol.erl
@@ -49,10 +49,12 @@
{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 @@
%% 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 @@
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 @@
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 @@
%%
%% 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 ->