You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by ii...@apache.org on 2020/01/23 14:01:09 UTC

[couchdb-thrift-protocol] 05/07: Add `thrift_protocol:{decode_struct/2, encode_struct/2}`

This is an automated email from the ASF dual-hosted git repository.

iilyak pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/couchdb-thrift-protocol.git

commit 6395e2724fe2e71b03c60960e9703100d4fb456c
Author: Takeru Ohta <ph...@gmail.com>
AuthorDate: Sat Jan 18 13:29:40 2020 +0900

    Add `thrift_protocol:{decode_struct/2, encode_struct/2}`
---
 src/thrift_protocol.erl         | 16 +++++++++++++++-
 src/thrift_protocol_compact.erl |  2 +-
 2 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/src/thrift_protocol.erl b/src/thrift_protocol.erl
index 4c80023..f9ca1ef 100644
--- a/src/thrift_protocol.erl
+++ b/src/thrift_protocol.erl
@@ -33,7 +33,7 @@
 
 -include("thrift_protocol.hrl").
 
--export([encode_message/2, decode_message/2]).
+-export([encode_message/2, decode_message/2, encode_struct/2, decode_struct/2]).
 -export([data_type/1]).
 
 -export_type([message/0, message_type/0]).
@@ -114,6 +114,13 @@ encode_message(Message, binary) ->
 encode_message(Message, compact) ->
     thrift_protocol_compact:encode_message(Message).
 
+%% @doc Encodes `Struct'.
+-spec encode_struct(struct(), Format :: format()) -> iodata().
+encode_struct(Struct, binary) ->
+    thrift_protocol_binary:encode_struct(Struct);
+encode_struct(Struct, compact) ->
+    thrift_protocol_compact:encode_struct(Struct).
+
 %% @doc Decodes a message from the given binary.
 -spec decode_message(binary(), Format :: format()) -> {message(), binary()}.
 decode_message(Bin, binary) ->
@@ -121,6 +128,13 @@ decode_message(Bin, binary) ->
 decode_message(Bin, compact) ->
     thrift_protocol_compact:decode_message(Bin).
 
+%% @doc Decodes a struct from the given binary.
+-spec decode_struct(binary(), Format :: format()) -> {struct(), binary()}.
+decode_struct(Bin, binary) ->
+    thrift_protocol_binary:decode_struct(Bin, #{});
+decode_struct(Bin, compact) ->
+    thrift_protocol_compact:decode_struct(Bin, 0, #{}).
+
 %% @doc Returns the type of `Data'.
 -spec data_type(Data :: data()) -> data_type().
 data_type(X) when is_boolean(X) ->
diff --git a/src/thrift_protocol_compact.erl b/src/thrift_protocol_compact.erl
index 4d12e81..ca44a05 100644
--- a/src/thrift_protocol_compact.erl
+++ b/src/thrift_protocol_compact.erl
@@ -11,7 +11,7 @@
 
 -include("thrift_protocol.hrl").
 
--export([decode_message/1, encode_message/1]).
+-export([decode_message/1, encode_message/1, encode_struct/1, decode_struct/3]).
 
 -define(PROTOCOL_ID, 16#82).
 -define(VERSION, 1).