Skip to content

Commit

Permalink
Revert "Add JSONE and MAP support"
Browse files Browse the repository at this point in the history
This reverts commit 4818656.
  • Loading branch information
Stephen Han committed Apr 15, 2019
1 parent 5a97e0d commit 1cba2ae
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 125 deletions.
4 changes: 2 additions & 2 deletions src/couchbeam.app.src
Expand Up @@ -6,7 +6,7 @@

{application, couchbeam,
[{description, "Erlang CouchDB client"},
{vsn, "1.4.2.1"},
{vsn, "1.4.2"},
{modules, []},
{registered, [
couchbeam_sup
Expand All @@ -15,9 +15,9 @@
stdlib,
asn1,
crypto,
idna,
public_key,
ssl,
idna,
hackney]},
{included_applications, []},
{env, []},
Expand Down
79 changes: 12 additions & 67 deletions src/couchbeam.erl
Expand Up @@ -400,20 +400,6 @@ doc_exists(#db{server=Server, options=Opts}=Db, DocId) ->
_Error -> false
end.

-ifdef('MAPS_SUPPORT').
get_return_params(Params) ->
case proplists:get_value(return_maps, Params) == true of
true -> {[return_maps], proplists:delete(return_maps, Params)};
false -> {[], Params}
end.
-else.
get_return_params(Params) ->
case proplists:get_value(return_maps, Params) == true of
true -> {[], proplists:delete(return_maps, Params)};
false -> {[], Params}
end.
-endif.

%% @doc open a document
%% @equiv open_doc(Db, DocId, [])
open_doc(Db, DocId) ->
Expand All @@ -431,10 +417,6 @@ open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
unefined -> {any, Params};
A -> {A, proplists:delete(accept, Params)}
end,

%% Set return format
{ReturnOptions, Params2} = get_return_params(Params1),

%% set the headers with the accepted content-type if needed
Headers = case {Accept, proplists:get_value("attachments", Params)} of
{any, true} ->
Expand All @@ -448,7 +430,7 @@ open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
[]
end,
Url = hackney_url:make_url(couchbeam_httpc:server_url(Server), couchbeam_httpc:doc_url(Db, DocId1),
Params2),
Params1),
case couchbeam_httpc:db_request(get, Url, Headers, <<>>, Opts,
[200, 201]) of
{ok, _, RespHeaders, Ref} ->
Expand All @@ -460,7 +442,7 @@ open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
end},
{ok, {multipart, InitialState}};
_ ->
{ok, couchbeam_httpc:json_body(Ref, ReturnOptions)}
{ok, couchbeam_httpc:json_body(Ref)}
end;
Error ->
Error
Expand Down Expand Up @@ -543,8 +525,14 @@ save_doc(Db, Doc, Options) ->

-spec save_doc(Db::db(), doc(), mp_attachments(), Options::list()) ->
{ok, doc()} | {error, term()}.
save_doc(#db{server=Server, options=Opts}=Db, Doc, Atts, Options) ->
DocId = get_doc_id(Doc, Server),
save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->
DocId = case couchbeam_util:get_value(<<"_id">>, Props) of
undefined ->
[Id] = get_uuid(Server),
Id;
DocId1 ->
couchbeam_util:encode_docid(DocId1)
end,
Url = hackney_url:make_url(couchbeam_httpc:server_url(Server), couchbeam_httpc:doc_url(Db, DocId),
Options),
case Atts of
Expand All @@ -557,7 +545,8 @@ save_doc(#db{server=Server, options=Opts}=Db, Doc, Atts, Options) ->
{JsonProp} = couchbeam_httpc:json_body(Ref),
NewRev = couchbeam_util:get_value(<<"rev">>, JsonProp),
NewDocId = couchbeam_util:get_value(<<"id">>, JsonProp),
Doc1 = form_return_doc(Doc, NewRev, NewDocId),
Doc1 = couchbeam_doc:set_value(<<"_rev">>, NewRev,
couchbeam_doc:set_value(<<"_id">>, NewDocId, Doc)),
{ok, Doc1};
Error ->
Error
Expand Down Expand Up @@ -585,50 +574,6 @@ save_doc(#db{server=Server, options=Opts}=Db, Doc, Atts, Options) ->
end
end.

-ifdef('MAPS_SUPPORT').
form_return_doc(Doc = #{}, NewRev, NewDocId) ->
Doc#{<<"_rev">> => NewRev, <<"_id">> => NewDocId};
form_return_doc(Doc, NewRev, NewDocId) ->
couchbeam_doc:set_value(<<"_rev">>, NewRev,
couchbeam_doc:set_value(<<"_id">>, NewDocId, Doc));
form_return_doc(Doc, NewRev, NewDocId) ->
couchbeam_doc:set_value(<<"_rev">>, NewRev,
couchbeam_doc:set_value(<<"_id">>, NewDocId, Doc)).
-else.
form_return_doc(Doc, NewRev, NewDocId) ->
couchbeam_doc:set_value(<<"_rev">>, NewRev,
couchbeam_doc:set_value(<<"_id">>, NewDocId, Doc)).
-endif.

-ifdef('MAPS_SUPPORT').
get_doc_id(Doc = #{<<"_id">> := DocId1}, _Server) ->
couchbeam_util:encode_docid(DocId1);
get_doc_id(Doc = #{'_id' := DocId1}, _Server) ->
couchbeam_util:encode_docid(DocId1);
get_doc_id(Doc = #{}, Server) ->
[Id] = get_uuid(Server),
Id;
get_doc_id({Props}, Server) ->
case couchbeam_util:get_value(<<"_id">>, Props) of
undefined ->
[Id] = get_uuid(Server),
Id;
DocId1 ->
couchbeam_util:encode_docid(DocId1)
end.
-else.
get_doc_id({Props}, Server) ->
case couchbeam_util:get_value(<<"_id">>, Props) of
undefined ->
[Id] = get_uuid(Server),
Id;
DocId1 ->
couchbeam_util:encode_docid(DocId1)
end.
-endif.



%% @doc delete a document
%% @equiv delete_doc(Db, Doc, [])
delete_doc(Db, Doc) ->
Expand Down
59 changes: 8 additions & 51 deletions src/couchbeam_ejson.erl
Expand Up @@ -6,25 +6,19 @@

-module(couchbeam_ejson).

-export([encode/1, decode/1, decode/2]).
-export([encode/1, decode/1]).
-export([post_decode/1]).

-include("couchbeam.hrl").


-ifdef('WITH_JSX').
-define(JSON_ENCODE(D), jsx:encode(map_or_pre_encode(D))).
-define(JSON_DECODE(D, DecodeOptions), map_or_post_decode(jsx:decode(D, DecodeOptions))).
-endif.

-ifdef('WITH_JSONE').
-define(JSON_ENCODE(D), jsone:encode(D)).
-define(JSON_DECODE(D, DecodeOptions), map_or_post_decode(jsone:decode(D, DecodeOptions))).
-endif.
-ifndef('WITH_JIFFY').
-define(JSON_ENCODE(D), jsx:encode(pre_encode(D))).
-define(JSON_DECODE(D), post_decode(jsx:decode(D))).

-ifdef('WITH_JIFFY').
-else.
-define(JSON_ENCODE(D), jiffy:encode(D, [uescape])).
-define(JSON_DECODE(D, DecodeOptions), jiffy:decode(D, DecodeOptions)).
-define(JSON_DECODE(D), jiffy:decode(D)).
-endif.


Expand All @@ -38,44 +32,15 @@ encode(D) ->
-spec decode(binary()) -> ejson().
%% @doc decode a binary to an EJSON term. Throw an exception if there is
%% any error.
decode(D) -> decode(D, []).

-ifdef('WITH_JSONE').
decode(D, Options) ->
DecodeOptions = case proplists:get_value(return_maps, Options) == true of
true -> [];
false -> [{object_format, proplist}]
end,
try
?JSON_DECODE(D, DecodeOptions)
catch
throw:Error ->
throw({invalid_json, Error});
error:badarg ->
throw({invalid_json, badarg})
end.
-else.
decode(D, Options) ->
DecodeOptions = case proplists:get_value(return_maps, Options) == true of
true -> [return_maps];
false -> []
end,
decode(D) ->
try
?JSON_DECODE(D, DecodeOptions)
?JSON_DECODE(D)
catch
throw:Error ->
throw({invalid_json, Error});
error:badarg ->
throw({invalid_json, badarg})
end.
-endif.

-ifdef('MAPS_SUPPORT').
map_or_pre_encode(Map = #{}) -> Map;
map_or_pre_encode(NotMap) -> pre_encode(NotMap).
-else.
map_or_pre_encode(NotMap) -> pre_encode(NotMap).
-endif.

pre_encode({[]}) ->
[{}];
Expand All @@ -96,14 +61,6 @@ pre_encode(Atom) when is_atom(Atom) ->
pre_encode(Term) when is_integer(Term); is_float(Term); is_binary(Term) ->
Term.


-ifdef('MAPS_SUPPORT').
map_or_post_decode(Map = #{}) -> Map;
map_or_post_decode(NotMap) -> post_decode(NotMap).
-else.
map_or_post_decode(NotMap) -> post_decode(NotMap).
-endif.

post_decode({[{}]}) ->
{[]};
post_decode([{}]) ->
Expand Down
6 changes: 2 additions & 4 deletions src/couchbeam_httpc.erl
Expand Up @@ -7,7 +7,7 @@

-export([request/5,
db_request/5, db_request/6,
json_body/1, json_body/2,
json_body/1,
db_resp/2,
make_headers/4,
maybe_oauth_header/4]).
Expand All @@ -32,10 +32,8 @@ db_request(Method, Url, Headers, Body, Options, Expect) ->
db_resp(Resp, Expect).

json_body(Ref) ->
json_body(Ref, []).
json_body(Ref, Options) ->
{ok, Body} = hackney:body(Ref),
couchbeam_ejson:decode(Body, Options).
couchbeam_ejson:decode(Body).

make_headers(Method, Url, Headers, Options) ->
Headers1 = case couchbeam_util:get_value(<<"Accept">>, Headers) of
Expand Down
2 changes: 1 addition & 1 deletion src/couchbeam_view.erl
Expand Up @@ -469,7 +469,7 @@ collect_view_results(Ref, Acc) ->
%% in case we got some results
Rows = lists:reverse(Acc),
{error, Error, Rows}
after 20000 ->
after 10000 ->
{error, timeout}
end.

Expand Down

0 comments on commit 1cba2ae

Please sign in to comment.