Просмотр исходного кода

remove n2o from deps, JSON module in config(rest,json)

Maxim Sokhatsky 10 лет назад
Родитель
Сommit
42be13c50d
2 измененных файлов с 25 добавлено и 6 удалено
  1. 12 3
      src/rest.erl
  2. 13 3
      src/rest_cowboy.erl

+ 12 - 3
src/rest.erl

@@ -121,11 +121,20 @@ props_skip({<<BinaryKey/binary>>, Value}, Acc) ->
 props_skip({Key, Value}, Acc) -> [{Key, from_json(Value)} | Acc].
 
 to_json(Data) ->
-    case wf_utils:is_string(Data) of
-        true  -> wf:to_binary(Data);
+    case is_string(Data) of
+        true  -> to_binary(Data);
         false -> json_match(Data)
     end.
 
-json_match([{_, _} | _] = Props) -> [{wf:to_binary(Key), to_json(Value)} || {Key, Value} <- Props];
+json_match([{_, _} | _] = Props) -> [{to_binary(Key), to_json(Value)} || {Key, Value} <- Props];
 json_match([_ | _] = NonEmptyList) -> [to_json(X) || X <- NonEmptyList];
 json_match(Any) -> Any.
+
+is_char(C) -> is_integer(C) andalso C >= 0 andalso C =< 255.
+is_string([N | _] = PossibleString) when is_number(N) -> lists:all(fun is_char/1, PossibleString);
+is_string(_)                                          -> false.
+
+to_binary(A) when is_atom(A) -> to_binary(atom_to_list(A));
+to_binary(B) when is_binary(B) -> B;
+to_binary(I) when is_integer(I) -> to_binary(integer_to_list(I));
+to_binary(L) when is_list(L) ->  iolist_to_binary(L).

+ 13 - 3
src/rest_cowboy.erl

@@ -5,13 +5,17 @@
          to_html/2, to_json/2, content_types_accepted/2, delete_resource/2,
          handle_urlencoded_data/2, handle_json_data/2]).
 
+-ifndef(JSON).
+-define(JSON, (config(rest,json,n2o_json))).
+-endif.
+
 init(_, _, _) -> {upgrade, protocol, cowboy_rest}.
 
 rest_init(Req, _Opts) ->
     {Resource, Req1} = cowboy_req:binding(resource, Req),
     Module = case rest_module(Resource) of {ok, M} -> M; _ -> undefined end,
     {Id, Req2} = cowboy_req:binding(id, Req1),
-    Req3 = wf:header(<<"Access-Control-Allow-Origin">>, <<"*">>, Req2),
+    Req3 = cowboy_req:set_resp_header(<<"Access-Control-Allow-Origin">>, <<"*">>, Req2),
     {ok, Req3, #st{resource_module = Module, resource_id = Id}}.
 
 resource_exists(Req, #st{resource_module = undefined} = State)       -> {false, Req, State};
@@ -42,7 +46,7 @@ to_json(Req, #st{resource_module = M, resource_id = Id} = State) ->
     Struct = case Id of
                  undefined -> {struct, [{M, [{struct, M:to_json(Resource)} || Resource <- M:get()]}]};
                  _         -> {struct, M:to_json(M:get(Id))} end,
-    {iolist_to_binary(n2o_json:encode(Struct)), Req, State}.
+    {iolist_to_binary(?JSON:encode(Struct)), Req, State}.
 
 content_types_accepted(Req, State) -> {[{<<"application/x-www-form-urlencoded">>, handle_urlencoded_data},
                                         {<<"application/json">>, handle_json_data}], Req, State}.
@@ -53,7 +57,7 @@ handle_urlencoded_data(Req, #st{resource_module = M, resource_id = Id} = State)
 
 handle_json_data(Req, #st{resource_module = M, resource_id = Id} = State) ->
     {ok, Binary, Req2} = cowboy_req:body(Req),
-    Data = case n2o_json:decode(Binary) of {struct, Struct} -> Struct; _ -> [] end,
+    Data = case ?JSON:decode(Binary) of {struct, Struct} -> Struct; _ -> [] end,
     {handle_data(M, Id, Data), Req2, State}.
 
 handle_data(Mod, Id, Data) ->
@@ -99,3 +103,9 @@ rest_module(Module) ->
         true = lists:member(rest, proplists:get_value(behaviour, Info)),
         {ok, M}
     catch error:Error -> {error, Error} end.
+
+config(Key) -> config(rest, Key, "").
+config(App,Key) -> config(App,Key, "").
+config(App, Key, Default) -> case application:get_env(App,Key) of
+                              undefined -> Default;
+                              {ok,V} -> V end.