Commits

Benoit Chesneau committed 9244a46

fix issue #16. extend doc by a property or a list of propery and update
existing.

Comments (0)

Files changed (2)

src/couchbeam_doc.erl

 %% @spec extend(Prop::property(), JsonObj::json_obj()) -> json_obj()
 %% @type property() = json_obj() | tuple()  
 %% @doc extend a jsonobject by a property or list of property
-extend({Prop}, JsonObj) ->
-    extend(Prop, JsonObj);
-extend(Prop, JsonObj) when is_list(Prop)->
-    extend1(Prop, JsonObj);
-extend(Prop, JsonObj) when is_tuple(Prop)->
-    {Props} = JsonObj,
-    {lists:reverse([Prop|lists:reverse(Props)])}.
+extend([], JsonObj) ->
+    JsonObj;
+extend([Prop|R], JsonObj)->
+    NewObj = extend(Prop, JsonObj),
+    extend(R, NewObj);
+extend({Key, Value}, JsonObj) ->
+    set_value(Key, Value, JsonObj).
 
 %% @spec add_attachment(Doc::json_obj(),Content::attachment_content(), 
 %%      AName::string()) -> json_obj()
  
 
 %% @private
-   
-extend1([], JsonObj) ->
-    {Props} = JsonObj,
-    {lists:reverse(Props)};
-extend1([Prop|T], JsonObj) ->
-    {Props} = JsonObj,
-    extend(T, {[Prop|Props]}).
-
-
 set_value1([], _Key, _Value, Acc) ->
     {lists:reverse(Acc)};
 set_value1([{K, V}|T], Key, Value, Acc) ->
 %%! -pa ./ebin
 
 main(_) ->
-    etap:plan(27),
+    etap:plan(29),
     start_app(),
     case (catch test()) of
         ok ->
     Doc8 = couchbeam_doc:extend([{<<"b">>, 1}, {<<"c">>, 1}], Doc7),
     etap:is(couchbeam_doc:get_value("b", Doc8), 1, "set value ok"),
     etap:is(couchbeam_doc:get_value("c", Doc8), 1, "set value ok"),
+    Doc81 = couchbeam_doc:extend([{<<"c">>, 3}, {<<"d">>, 1}], Doc8),
+    etap:is(couchbeam_doc:get_value("c", Doc81), 3, "set value ok"),
+    etap:is(couchbeam_doc:get_value("d", Doc81), 1, "set value ok"),
     
     Doc9 = {[{<<"_id">>, <<"~!@#$%^&*()_+-=[]{}|;':,./<> ?">>}]},
     Doc10 = couchbeam_db:save_doc(Db, Doc9),
     couchbeam_db:delete_docs(Db, [Doc13, Doc14]),
     
     etap:is(couchbeam_db:open_doc(Db, couchbeam_doc:get_id(Doc13)), not_found, "bulk docs delete ok"),
-    
     ok.