summaryrefslogtreecommitdiff
path: root/src/couchdb/couch_doc.erl
diff options
context:
space:
mode:
authorJohn Christopher Anderson <jchris@apache.org>2009-01-10 06:18:16 +0000
committerJohn Christopher Anderson <jchris@apache.org>2009-01-10 06:18:16 +0000
commitb9831e19e8637688a77c03f2790503bdff59fff3 (patch)
tree5a59a3a5034a19b5fbd6403cadda5820dfb0d562 /src/couchdb/couch_doc.erl
parent34ad265bc0bb517acc4d5292e8025cc8e05931b7 (diff)
refactor couch_doc:to_json_obj for easier debugging
git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@733232 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/couchdb/couch_doc.erl')
-rw-r--r--src/couchdb/couch_doc.erl133
1 files changed, 75 insertions, 58 deletions
diff --git a/src/couchdb/couch_doc.erl b/src/couchdb/couch_doc.erl
index 56d559ea..3e0a8614 100644
--- a/src/couchdb/couch_doc.erl
+++ b/src/couchdb/couch_doc.erl
@@ -18,65 +18,82 @@
-include("couch_db.hrl").
-to_json_obj(#doc{id=Id,deleted=Del,body=Body,revs=Revs,meta=Meta}=Doc,Options)->
- {[{<<"_id">>, Id}] ++
- case Revs of
+% helpers used by to_json_obj
+to_json_rev([]) ->
+ [];
+to_json_rev(Revs) ->
+ [{<<"_rev">>, lists:nth(1, Revs)}].
+
+to_json_body(true, _Body) ->
+ [{<<"_deleted">>, true}];
+to_json_body(false, {Body}) ->
+ Body.
+
+to_json_revs(Options, Revs) ->
+ case lists:member(revs, Options) of
+ false -> [];
+ true ->
+ [{<<"_revs">>, Revs}]
+ end.
+
+to_json_revs_info(Meta) ->
+ lists:map(
+ fun({revs_info, RevsInfo}) ->
+ JsonRevsInfo =
+ [{[{rev, Rev}, {status, list_to_binary(atom_to_list(Status))}]} ||
+ {Rev, Status} <- RevsInfo],
+ {<<"_revs_info">>, JsonRevsInfo};
+ ({conflicts, Conflicts}) ->
+ {<<"_conflicts">>, Conflicts};
+ ({deleted_conflicts, Conflicts}) ->
+ {<<"_deleted_conflicts">>, Conflicts}
+ end, Meta).
+
+to_json_attachment_stubs(Attachments) ->
+ BinProps = lists:map(
+ fun({Name, {Type, BinValue}}) ->
+ {Name, {[
+ {<<"stub">>, true},
+ {<<"content_type">>, Type},
+ {<<"length">>, bin_size(BinValue)}
+ ]}}
+ end,
+ Attachments),
+ case BinProps of
[] -> [];
- _ -> [{<<"_rev">>, lists:nth(1, Revs)}]
- end ++
- case Del of
- false ->
- {BodyProps} = Body,
- BodyProps;
- true ->
- [{<<"_deleted">>, true}]
- end ++
- case lists:member(revs, Options) of
- false -> [];
- true ->
- [{<<"_revs">>, Revs}]
- end ++
- lists:map(
- fun({revs_info, RevsInfo}) ->
- JsonRevsInfo =
- [{[{rev, Rev}, {status, list_to_binary(atom_to_list(Status))}]} ||
- {Rev, Status} <- RevsInfo],
- {<<"_revs_info">>, JsonRevsInfo};
- ({conflicts, Conflicts}) ->
- {<<"_conflicts">>, Conflicts};
- ({deleted_conflicts, 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, {[
- {<<"content_type">>, Type},
- {<<"data">>, couch_util:encodeBase64(bin_to_binary(BinValue))}
- ]}}
- end,
- Doc#doc.attachments),
- case BinProps of
- [] -> [];
- _ -> [{<<"_attachments">>, {BinProps}}]
- end;
- false ->
- BinProps = lists:map(
- fun({Name, {Type, BinValue}}) ->
- {Name, {[
- {<<"stub">>, true},
- {<<"content_type">>, Type},
- {<<"length">>, bin_size(BinValue)}
- ]}}
- end,
- Doc#doc.attachments),
- case BinProps of
- [] -> [];
- _ -> [{<<"_attachments">>, {BinProps}}]
- end
- end
- }.
+ _ -> [{<<"_attachments">>, {BinProps}}]
+ end.
+
+to_json_attachments(Attachments) ->
+ BinProps = lists:map(
+ fun({Name, {Type, BinValue}}) ->
+ {Name, {[
+ {<<"content_type">>, Type},
+ {<<"data">>, couch_util:encodeBase64(bin_to_binary(BinValue))}
+ ]}}
+ end,
+ Attachments),
+ case BinProps of
+ [] -> [];
+ _ -> [{<<"_attachments">>, {BinProps}}]
+ end.
+
+to_json_attachments(Attachments, Options) ->
+ case lists:member(attachments, Options) of
+ true -> % return the full rev list and the binaries as strings.
+ to_json_attachments(Attachments);
+ false ->
+ to_json_attachment_stubs(Attachments)
+ end.
+
+to_json_obj(#doc{id=Id,deleted=Del,body=Body,revs=Revs,meta=Meta}=Doc,Options)->
+ {[{<<"_id">>, Id}]
+ ++ to_json_rev(Revs)
+ ++ to_json_body(Del, Body)
+ ++ to_json_revs(Options, Revs)
+ ++ to_json_revs_info(Meta)
+ ++ to_json_attachments(Doc#doc.attachments, Options)
+ }.
from_json_obj({Props}) ->
{JsonBins} = proplists:get_value(<<"_attachments">>, Props, {[]}),