Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps support #125

Open
stofel opened this issue Feb 16, 2015 · 5 comments
Open

Maps support #125

stofel opened this issue Feb 16, 2015 · 5 comments

Comments

@stofel
Copy link

stofel commented Feb 16, 2015

Hi.
How about maps support?

Some like this couchbeam:save_doc/couchbeam:open_doc

diff --git a/src/couchbeam.erl b/src/couchbeam.erl
index 8b3c967..808f6ca 100644
--- a/src/couchbeam.erl
+++ b/src/couchbeam.erl
@@ -441,7 +441,14 @@ open_doc(Db, DocId) ->
 %% Params is a list of query argument. Have a look in CouchDb API
 %% @spec open_doc(Db::db(), DocId::string(), Params::list())
 %%          -> {ok, Doc}|{error, Error}
-open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
+open_doc(#db{server=Server, options=Opts}=Db, DocId, ParamsWithMaps) ->
+
+    {ReturnMaps, Params} = case lists:keytake(return_maps, 1, ParamsWithMaps) of
+        {value,{return_maps,true}, RestPatams} -> {true, RestPatams};
+        {value,{return_maps,_}, RestPatams} -> {false, RestPatams};
+        _ -> {false, ParamsWithMaps}
+    end,
+
     DocId1 = couchbeam_util:encode_docid(DocId),

     %% is there any accepted content-type passed to the params?
@@ -474,7 +481,11 @@ open_doc(#db{server=Server, options=Opts}=Db, DocId, Params) ->
                             end},
                     {ok, {multipart, InitialState}};
                 _ ->
-                    {ok, couchbeam_httpc:json_body(Ref)}
+                    Return = case ReturnMaps of 
+                        true -> couchbeam_httpc:json_body(Ref, [return_maps]); 
+                        _ -> couchbeam_httpc:json_body(Ref)
+                    end,
+                    {ok, Return}
             end;
         Error ->
             Error
@@ -604,7 +615,26 @@ save_doc(#db{server=Server, options=Opts}=Db, {Props}=Doc, Atts, Options) ->
                 Error ->
                     Error
             end
-    end.
+    end;
+save_doc(#db{server=Server, options=Opts}=Db, Doc, [], Options) ->
+  case maps:get(<<"_id">>, Doc, undefined) of
+    undefined -> {error, <<"_id field not exsists">>};
+    DocId ->
+      Url = hackney_url:make_url(server_url(Server), doc_url(Db, DocId), Options),
+      JsonDoc = jiffy:encode(Doc),
+      Headers = [{<<"Content-Type">>, <<"application/json">>}],
+      case couchbeam_httpc:db_request(put, Url, Headers, JsonDoc, Opts, [200, 201]) of
+          {ok, _, _, Ref} ->
+              {JsonProp} = couchbeam_httpc:json_body(Ref),
+              NewRev = couchbeam_util:get_value(<<"rev">>, JsonProp),
+              NewDocId = couchbeam_util:get_value(<<"id">>, JsonProp),
+              Doc1 = Doc#{<<"_rev">> => NewRev, <<"_id">> := NewDocId},
+              {ok, Doc1};
+          Error ->
+              Error
+      end
+  end.
+

 %% @doc delete a document
 %% @equiv delete_doc(Db, Doc, [])
diff --git a/src/couchbeam_httpc.erl b/src/couchbeam_httpc.erl
index 194177d..f7a11ab 100644
--- a/src/couchbeam_httpc.erl
+++ b/src/couchbeam_httpc.erl
@@ -7,7 +7,7 @@

 -export([request/5,
          db_request/5, db_request/6,
-         json_body/1,
+         json_body/1, json_body/2,
          db_resp/2,
          make_headers/4,
          maybe_oauth_header/4]).
@@ -28,6 +28,10 @@ db_request(Method, Url, Headers, Body, Options, Expect) ->
 json_body(Ref) ->
     {ok, Body} = hackney:body(Ref),
     couchbeam_ejson:decode(Body).
+json_body(Ref, [return_maps]) ->
+    {ok, Body} = hackney:body(Ref),
+    jiffy:decode(Body, [return_maps]).
+

 make_headers(Method, Url, Headers, Options) ->
     Headers1 = case couchbeam_util:get_value(<<"Accept">>, Headers) of
@stofel
Copy link
Author

stofel commented Feb 16, 2015

Oh sorry, work is being done now

@benoitc
Copy link
Owner

benoitc commented Feb 25, 2015

i fully support that feature. Could you make it avaialble a s a PR? Also it should be made optional for now for legacy users imo. Thoughts?

@stofel
Copy link
Author

stofel commented Feb 25, 2015

I do not have a sufficient qualification, but I'll try

@benoitc
Copy link
Owner

benoitc commented Feb 25, 2015

Thanks :)

On Wed, Feb 25, 2015 at 5:31 PM, stofel notifications@github.com wrote:

I do not have a sufficient qualification, but I'll try


Reply to this email directly or view it on GitHub
#125 (comment).

@stofel
Copy link
Author

stofel commented May 21, 2016

It is impossible while travis check this otp_releases:

  • R16B03-1
  • 17.0
  • 17.1
  • 18.0
  • 18.1

R16B03-1 not support maps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants