summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Joseph Davis <davisp@apache.org>2009-08-21 22:11:17 +0000
committerPaul Joseph Davis <davisp@apache.org>2009-08-21 22:11:17 +0000
commit4143b6805cddf61dfd0d43b712b598705bca1933 (patch)
tree57681015cfd6b9731386df9d052b9243796513b7
parent678512dcabec2a54071ed63ed786c4c0c3f1d647 (diff)
Munge all ErlJSON to use binaries.
Our flavor of ErlJSON has this interesting characteristic: {[{foo, bar}]} == {[{<<"foo">>, <<"bar">>}]} This is fine and dandy for writing ErlJSON that is going to go directly to a serializer, but when it goes to client code executing in couch_native_process.erl it becomes a pain in the but to know whether to use atoms or binaries. This patch munges all input to user functions to use binaries except for null, true, and false obviously. This fixes the commented out test in erlang_views.js as well as adds a show to poke into the request object that has known instances of where things get munged. git-svn-id: https://svn.apache.org/repos/asf/couchdb/trunk@806732 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--share/www/script/test/erlang_views.js29
-rw-r--r--src/couchdb/couch_native_process.erl20
-rwxr-xr-x[-rw-r--r--]test/run_native_process.es0
3 files changed, 41 insertions, 8 deletions
diff --git a/share/www/script/test/erlang_views.js b/share/www/script/test/erlang_views.js
index 24f6cd1a..2af26b5a 100644
--- a/share/www/script/test/erlang_views.js
+++ b/share/www/script/test/erlang_views.js
@@ -25,7 +25,7 @@ couchTests.erlang_views = function(debug) {
function() {
// Note we just do some basic 'smoke tests' here - the
// test/query_server_spec.rb tests have more comprehensive tests
- var doc = {integer: 1, string: "str1", array: [1, 2, 3]};
+ var doc = {_id: "1", integer: 1, string: "str1", array: [1, 2, 3]};
T(db.save(doc).ok);
var mfun = 'fun({Doc}) -> ' +
@@ -40,8 +40,9 @@ couchTests.erlang_views = function(debug) {
T(results.total_rows == 1);
T(results.rows[0].key == 1);
T(results.rows[0].value == "str1");
+
// check simple reduction - another doc with same key.
- var doc = {integer: 1, string: "str2"};
+ var doc = {_id: "2", integer: 1, string: "str2"};
T(db.save(doc).ok);
rfun = "fun(Keys, Values, ReReduce) -> length(Values) end."
results = db.query(mfun, rfun, null, null, "erlang");
@@ -51,12 +52,23 @@ couchTests.erlang_views = function(debug) {
var designDoc = {
_id:"_design/erlview",
language: "erlang",
+ shows: {
+ simple:
+ 'fun(Doc, {Req}) -> ' +
+ ' {Info} = proplists:get_value(<<"info">>, Req, {[]}), ' +
+ ' Purged = proplists:get_value(<<"purge_seq">>, Info, -1), ' +
+ ' Verb = proplists:get_value(<<"verb">>, Req, <<"not_get">>), ' +
+ ' R = list_to_binary(io_lib:format("~b - ~s", [Purged, Verb])), ' +
+ ' {[{<<"code">>, 200}, {<<"headers">>, {[]}}, {<<"body">>, R}]} ' +
+ 'end.'
+ },
lists: {
simple_list :
'fun(Head, {Req}) -> ' +
' Send(<<"head">>), ' +
' Fun = fun({Row}, _) -> ' +
- ' Send(proplists:get_value(<<"value">>, Row, null)), ' +
+ ' Val = proplists:get_value(<<"value">>, Row, -1), ' +
+ ' Send(list_to_binary(integer_to_list(Val))), ' +
' {ok, nil} ' +
' end, ' +
' {ok, _} = FoldRows(Fun, nil), ' +
@@ -72,11 +84,14 @@ couchTests.erlang_views = function(debug) {
};
T(db.save(designDoc).ok);
- // *sob* - show functions have problems :(
- /***
- var xhr = CouchDB.request("GET", "/test_suite_db/_design/erlview/_list/simple_list/simple_view");
+ var url = "/test_suite_db/_design/erlview/_show/simple/1";
+ var xhr = CouchDB.request("GET", url);
+ T(xhr.status == 200, "standard get should be 200");
+ T(xhr.responseText == "0 - GET");
+
+ var url = "/test_suite_db/_design/erlview/_list/simple_list/simple_view";
+ var xhr = CouchDB.request("GET", url);
T(xhr.status == 200, "standard get should be 200");
T(xhr.responseText == "head2tail");
- ***/
});
};
diff --git a/src/couchdb/couch_native_process.erl b/src/couchdb/couch_native_process.erl
index df879633..65b733bf 100644
--- a/src/couchdb/couch_native_process.erl
+++ b/src/couchdb/couch_native_process.erl
@@ -81,7 +81,7 @@ prompt(Pid, Data) when is_pid(Pid), is_list(Data) ->
_ ->
ok % Not listing
end,
- {NewState, Resp} = run(State, Data),
+ {NewState, Resp} = run(State, to_binary(Data)),
put(?STATE, NewState),
case Resp of
{error, Reason} ->
@@ -345,3 +345,21 @@ start_list_resp(Self, Sig) ->
_ ->
ok
end.
+
+to_binary({Data}) ->
+ Pred = fun({Key, Value}) ->
+ {to_binary(Key), to_binary(Value)}
+ end,
+ {lists:map(Pred, Data)};
+to_binary(Data) when is_list(Data) ->
+ lists:map(fun to_binary/1, Data);
+to_binary(null) ->
+ null;
+to_binary(true) ->
+ true;
+to_binary(false) ->
+ false;
+to_binary(Data) when is_atom(Data) ->
+ list_to_binary(atom_to_list(Data));
+to_binary(Data) ->
+ Data.
diff --git a/test/run_native_process.es b/test/run_native_process.es
index dfdc423e..dfdc423e 100644..100755
--- a/test/run_native_process.es
+++ b/test/run_native_process.es