diff options
author | Christopher Lenz <cmlenz@apache.org> | 2008-08-31 09:43:41 +0000 |
---|---|---|
committer | Christopher Lenz <cmlenz@apache.org> | 2008-08-31 09:43:41 +0000 |
commit | 15a175144d83d6177e9bbb923a7f7157e5ea8917 (patch) | |
tree | 92b7becc9610c46f87ddf7ab4c313642b007c4aa /src/couchdb/couch_doc.erl | |
parent | ac4075a7987dc43aadeb18a94e07f090d1b77546 (diff) |
Merged json_term_changes branch back into trunk.
git-svn-id: https://svn.apache.org/repos/asf/incubator/couchdb/trunk@690668 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_doc.erl')
-rw-r--r-- | src/couchdb/couch_doc.erl | 93 |
1 files changed, 50 insertions, 43 deletions
diff --git a/src/couchdb/couch_doc.erl b/src/couchdb/couch_doc.erl index 61685cfa..12b48dc2 100644 --- a/src/couchdb/couch_doc.erl +++ b/src/couchdb/couch_doc.erl @@ -19,109 +19,116 @@ -include("couch_db.hrl"). to_json_obj(#doc{id=Id,deleted=Del,body=Body,revs=Revs,meta=Meta}=Doc,Options)-> - {obj, [{"_id", Id}] ++ + {[{<<"_id">>, Id}] ++ case Revs of [] -> []; - _ -> [{"_rev", lists:nth(1, Revs)}] + _ -> [{<<"_rev">>, lists:nth(1, Revs)}] end ++ case Del of false -> - {obj, BodyProps} = Body, + {BodyProps} = Body, BodyProps; true -> - [{"_deleted", true}] + [{<<"_deleted">>, true}] end ++ case lists:member(revs, Options) of false -> []; true -> - [{"_revs", list_to_tuple(Revs)}] + [{<<"_revs">>, Revs}] end ++ lists:map( fun({revs_info, RevsInfo}) -> JsonRevsInfo = - [{obj, [{rev, Rev}, {status, atom_to_list(Status)}]} || + [{[{rev, Rev}, {status, atom_to_list(Status)}]} || {Rev, Status} <- RevsInfo], - {"_revs_info", list_to_tuple(JsonRevsInfo)}; + {<<"_revs_info">>, JsonRevsInfo}; ({conflicts, Conflicts}) -> - {"_conflicts", list_to_tuple(Conflicts)}; + {<<"_conflicts">>, Conflicts}; ({deleted_conflicts, Conflicts}) -> - {"_deleted_conflicts", list_to_tuple(Conflicts)} + {<<"_deleted_conflicts">>, Conflicts} end, Meta) ++ case lists:member(attachments, Options) of true -> % return the full rev list and the binaries as strings. BinProps = lists:map( fun({Name, {Type, BinValue}}) -> - {Name, {obj, [ - {"content_type", Type}, - {"data", couch_util:encodeBase64(bin_to_binary(BinValue))} + {Name, {[ + {<<"content_type">>, Type}, + {<<"data">>, couch_util:encodeBase64(bin_to_binary(BinValue))} ]}} end, Doc#doc.attachments), case BinProps of [] -> []; - _ -> [{"_attachments", {obj, BinProps}}] + _ -> [{<<"_attachments">>, {BinProps}}] end; false -> BinProps = lists:map( fun({Name, {Type, BinValue}}) -> - {Name, {obj, [ - {"stub", true}, - {"content_type", Type}, - {"length", bin_size(BinValue)} + {Name, {[ + {<<"stub">>, true}, + {<<"content_type">>, Type}, + {<<"length">>, bin_size(BinValue)} ]}} end, Doc#doc.attachments), case BinProps of [] -> []; - _ -> [{"_attachments", {obj, BinProps}}] + _ -> [{<<"_attachments">>, {BinProps}}] end end }. -from_json_obj({obj, Props}) -> - {obj,JsonBins} = proplists:get_value("_attachments", Props, {obj, []}), - Bins = lists:flatmap(fun({Name, {obj, BinProps}}) -> - case proplists:get_value("stub", BinProps) of +from_json_obj({Props}) -> + {JsonBins} = proplists:get_value(<<"_attachments">>, Props, {[]}), + Bins = lists:flatmap(fun({Name, {BinProps}}) -> + case proplists:get_value(<<"stub">>, BinProps) of true -> [{Name, stub}]; _ -> - Value = proplists:get_value("data", BinProps), - Type = proplists:get_value("content_type", BinProps, + Value = proplists:get_value(<<"data">>, BinProps), + Type = proplists:get_value(<<"content_type">>, BinProps, ?DEFAULT_ATTACHMENT_CONTENT_TYPE), [{Name, {Type, couch_util:decodeBase64(Value)}}] end end, JsonBins), - AllowedSpecialMembers = ["id", "revs", "rev", "attachments", "revs_info", - "conflicts", "deleted_conflicts", "deleted"], + AllowedSpecialMembers = [<<"id">>, <<"revs">>, <<"rev">>, <<"attachments">>, <<"revs_info">>, + <<"conflicts">>, <<"deleted_conflicts">>, <<"deleted">>], + % collect all the doc-members that start with "_" + % if any aren't in the AllowedSpecialMembers list + % then throw a doc_validation error [case lists:member(Name, AllowedSpecialMembers) of true -> ok; false -> throw({doc_validation, io_lib:format("Bad special document member: _~s", [Name])}) end - || {[$_|Name], _Value} <- Props], + || {<<$_,Name/binary>>, _Value} <- Props], Revs = - case tuple_to_list(proplists:get_value("_revs", Props, {})) of + case proplists:get_value(<<"_revs">>, Props, []) of [] -> - case proplists:get_value("_rev", Props) of + case proplists:get_value(<<"_rev">>, Props) of undefined -> []; Rev -> [Rev] end; Revs0 -> Revs0 end, - case proplists:get_value("_id", Props, "") of - Id when is_list(Id) -> - #doc{ - id = Id, - revs = Revs, - deleted = proplists:get_value("_deleted", Props, false), - body = {obj, [{Key, Value} || {[FirstChar|_]=Key, Value} <- Props, FirstChar /= $_]}, - attachments = Bins - }; - _ -> + case proplists:get_value(<<"_id">>, Props, <<>>) of + Id when is_binary(Id) -> ok; + Id -> + ?LOG_DEBUG("Document id is not a string: ~p", [Id]), throw({invalid_document_id, "Document id is not a string"}) - end. + end, + + % strip out the all props beginning with _ + NewBody = {[{K, V} || {<<First,_/binary>>=K, V} <- Props, First /= $_]}, + #doc{ + id = Id, + revs = Revs, + deleted = proplists:get_value(<<"_deleted">>, Props, false), + body = NewBody, + attachments = Bins + }. to_doc_info(#full_doc_info{id=Id,update_seq=Seq,rev_tree=Tree}) -> @@ -181,9 +188,9 @@ bin_to_binary({Fd, Sp, Len}) -> {ok, Bin, _Sp2} = couch_stream:read(Fd, Sp, Len), Bin. -get_view_functions(#doc{body={obj, Fields}}) -> - Lang = proplists:get_value("language", Fields, "javascript"), - {obj, Views} = proplists:get_value("views", Fields, {obj, []}), +get_view_functions(#doc{body={Fields}}) -> + Lang = proplists:get_value(<<"language">>, Fields, <<"javascript">>), + {Views} = proplists:get_value(<<"views">>, Fields, {[]}), {Lang, [{ViewName, Value} || {ViewName, Value} <- Views, is_list(Value)]}; get_view_functions(_Doc) -> none. |