benoitc / couchbeam

Apache CouchDB client in Erlang
Other
242 stars 113 forks source link

couchbeam:query_view return is not consistent #7

Closed sendtopms closed 14 years ago

sendtopms commented 14 years ago

couchbeam:query_view return is not consistent with JSON which is not proplists or proper lists. It causes to use brute force to passthrou elements.

sendtopms commented 14 years ago

For ex. Futon result of my m/r query is {"total_rows":1,"offset":0,"rows":[ {"id":"0ef3751ea0dbc32513e78dc46a1c4129","key":"2","value":{"title":"111","slug":"2","body":" dsfkjs sdksj ks ds","imageurl":"krish2.jpg"}}

]}

Couchdb result is

{1,0, [{<<"total_rows">>,1},{<<"offset">>,0}], [{<<"0ef3751ea0dbc32513e78dc46a1c4129">>,<<"2">>, {[{<<"title">>,<<"111">>}, {<<"slug">>,<<"2">>}, {<<"body">>,<<" dsfkjs sdksj ks ds">>}, {<<"imageurl">>,<<"krish2.jpg">>}]}}]}

benoitc commented 14 years ago

Could you paste the code you use to get results ? Also I'm not sure how do you get thse results. Afaik couchbeam_view:fetch_view will decoded json view without any parsing and couchbeam_view:parse_view will parse it to extract total_rows & co. Let me know.

sendtopms commented 14 years ago

this is my code query_view(Key) -> DVName = {"_blog", "withattachments"}, Resp = couchbeam_utils:query_view(DVName, Key), Resp.


couchbeam_utils:query_view is here

query_view(DVName, []) -> Attrs = [{"key1",<<>>}], io:format("Attr is ~p~n", [Attrs]), ViewPid = couchbeam_db:query_view(couchbeam_client, DVName,Attrs), Resp = couchbeam_view:parse_view(ViewPid), io:format("Response is ~p~n", [Resp]), Resp; query_view(DVName, Key) -> Attrs = [{"key",list_to_binary(Key)}], io:format("Attr is ~p~n", [Attrs]), ViewPid = couchbeam_db:query_view(couchbeam_client, DVName,Attrs), Resp = couchbeam_view:parse_view(ViewPid), io:format("Response is ~p~n", [Resp]), Resp.

benoitc commented 14 years ago

so if you use parse_view yu get the answer in a tuple : {TotalRows, Offset, Meta, Rows};

Where meta is empty in case of normal view or contain infos from c-l or others. If you just want the view decoded but in the form you get from futon you have to use fetch_view function.

I'm not sure what is the real problem anyway ?

sendtopms commented 14 years ago

It is fine but what I found in "rows", it is tuple of proplists "{[{". If it is just proplists, it is easy to avoid one more translation in client side. In the above snippet
{[{<<"title">>,<<"111">>}, {<<"slug">>,<<"2">>}, {<<"body">>,<<" dsfkjs sdksj ks ds">>}, {<<"imageurl">>,<<"krish2.jpg">>}]} I needed to detuple to access it in the erldtl template engine. Thanks for the response.

benoitc commented 14 years ago

Yes I know :( That the only way currently with coiuchdb mochijson to know this is an object and it seem all erlang json libraries do the same. For object some add obj in tuple ({obj, [{ ...) other use struct. That's a pb in erlang. MAybe a pluging in erlydtl could do the trick anyway.

sendtopms commented 14 years ago

Thanks. I can deal with this issue in the erlang client, though it is trivial, i wanted to check if it is addressable. Thanks again.

benoitc commented 14 years ago

after discussing aroud, it seems the only way in erlang to detect object. So for now I'm closing. Feel free to reopen the bug if you think there is another solution. Thanks for your feedback anyway.