Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proplist to Json-done, Json to Proplist-work in progress #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ebank/my_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{id:1, details: {name:costin, balance:50, pin:1234, transaction: {id:1, date:2, value:5}}, test:5}
9 changes: 5 additions & 4 deletions ebank/src/ebank_web.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

-module(ebank_web).
-author("Mochi Media <[email protected]>").
-define(TEST, true).
-define(TESTING, true).

-compile(tuple_calls).
-include_lib("kernel/include/logger.hrl").

-export([start/1, stop/0, loop/2]).
-export([start/1, stop/0, loop/2, generate_body/1]).

%% External API


-record(accountDetails, {name, balance, pin}).
-record(account, {id, details=accountDetails#{}}).
-record(test, {id, accountDetails=accountDetails#{}}).

start(Options) ->
{DocRoot, Options1} = get_option(docroot, Options),
Expand Down Expand Up @@ -152,12 +153,12 @@ parse_body(Data) when is_binary(Data) ->
Json = binary_to_list(Data),
Strip_with = "\n\t",
To_stripp = Json,
S = lists:filter( fun(C) -> not lists:member(C, Strip_with) end, To_stripp ).
lists:filter( fun(C) -> not lists:member(C, Strip_with) end, To_stripp ).

%%
%% Tests
%%
-ifdef(TEST).
-ifdef(TESTING).
-include_lib("eunit/include/eunit.hrl").

generate_body_test_() ->
Expand Down
117 changes: 106 additions & 11 deletions ebank/src/rec2json.erl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

-define(JSON_WRAPPER(Proplist), {Proplist}).

-record(account_info, {name, balance, pin}).
-record(account_info, {name, balance, pin, transaction=transaction#{}}).
-record(transaction, {id, date, value}).
-record(account, {id, details=account_info#{}}).

Expand All @@ -25,6 +25,13 @@ to_json(Account) ->
erlang:display(hd(AccountList)).


is_proplist([]) -> true;
is_proplist([Tuple|List]) ->
if tuple_size(Tuple) == 2 -> is_proplist(List);
true -> false
end;
is_proplist(Val) -> false.

%% @spec from_record(any()) -> string().
% from_record([], _) ->
% [];
Expand All @@ -33,8 +40,7 @@ to_json(Account) ->

% from_record([_|T]) ->
% from_record(T).
%  

%

to_proplist(Record) ->
to_proplist(Record, []).
Expand All @@ -43,8 +49,8 @@ to_proplist(Type = #account{}, []) ->
lists:zip(record_info(fields, account), to_list(Type));
to_proplist(Type = #account_info{}, []) ->
lists:zip(record_info(fields, account_info), to_list(Type));


to_proplist(Type = #transaction{}, []) ->
lists:zip(record_info(fields, transaction), to_list(Type));
to_proplist(Val, []) ->
Val;
to_proplist([], Result) ->
Expand All @@ -56,14 +62,35 @@ to_list(Type) ->
[to_proplist(L, []) || L <- tl(tuple_to_list(Type))].

proplist2json(Proplist) ->
proplist2json(Proplist, "{").
{ok, Fd} = file:open("my_file.json", [write]),
io:format(Fd, "~s", ["{" ++ proplist2json(Proplist, "") ++ "}"]).

proplist2json([], Acc) ->
Acc ++ "}";
Acc;
proplist2json([{K,V}|T], Acc) when is_list(V) ->
proplist2json(T, Acc ++ "\"" ++ lists:flatten(io_lib:format("~p", [K])) ++ ": [" ++ proplist2json(V, "") ++ "]");
List_or_Record =
case is_proplist(V) of
true -> {": {", "}"};
false-> {": [", "]"}
end,
proplist2json(
T,
Acc ++ lists:flatten(io_lib:format("~p", [K]))
++ element(1, List_or_Record)
++ proplist2json(V, "")
++ element(2, List_or_Record)
);
proplist2json([{K,V}|T], Acc) ->
proplist2json(T, Acc ++ "\"" ++ lists:flatten(io_lib:format("~p", [K])) ++ ":" ++ "\"" ++ lists:flatten(io_lib:format("~p", [V])) ++ "\",").
proplist2json(
T,
Acc ++ lists:flatten(io_lib:format("~p", [K]))
++ ":"
++ lists:flatten(io_lib:format("~p", [V]))
++ get_separator(T)
).

get_separator([]) -> "";
get_separator(_) -> ", ".

% %% @spec from_list(json_proplist()) -> object().
% from_list([]) -> true; % new();
Expand All @@ -84,9 +111,66 @@ msg2json(Key, Value) when is_list(Key), is_list(Value) ->
msg2json(Key, Value) ->
"{\n\t\t\"" ++ Key ++ "\" : \"" ++ Value ++ "\"\n}".

json2proplist(JSON) ->
io:format("~p", JSON).

json2proplist(Json) ->
Content = read_file(Json),
io:format("~s", [Content]),
string_to_proplist(
re:replace(
string:slice(Content, 1, length(Content) - 2),
"\\s+", "", [global, {return, list}]
),
[]
).

read_file(File_name) ->
{ok, Fd} = file:open(File_name, [read]),
try get_all_lines(Fd)
after file:close(Fd)
end.

get_all_lines(Fd) ->
case io:get_line(Fd, "") of
eof -> [];
Line -> Line ++ get_all_lines(Fd)
end.

string_to_proplist("", Acc) -> Acc;
string_to_proplist(Chars, Acc) ->
Splited_string = get_string_splited(Chars),
Current_tuple = string_to_tuple(hd(Splited_string)),
Next_tuple =
case tl(Splited_string) of
[] -> "";
_ -> hd(tl(Splited_string))
end,
string_to_proplist(Next_tuple, Acc ++ [Current_tuple]).

get_string_splited(Chars) ->
case determine_split(Chars) of
single_object -> [string:slice(Chars, 0, length(Chars) - 2)];
atribute -> string:split(Chars, ",");
object -> string:split(Chars, "},")
end.

determine_split(Chars) ->
atribute.

string_to_tuple(S) ->
[K, V] = string:split(S, ":"),
Value = parse_value(V),
{erlang:list_to_atom(K), Value}.

parse_value(V = [123|_]) ->
string_to_proplist(string:slice(V, 1, length(V) - 1), []);
parse_value(V) ->
Float = (catch erlang:list_to_float(V)),
Integer = (catch erlang:list_to_integer(V)),
if
is_number(Float) -> Float;
is_number(Integer) -> Integer;
true -> erlang:list_to_atom(V)
end.
%  
% %% @spec recursive_from_proplist(any()) -> object().
% recursive_from_proplist([]) -> true; % new();
Expand All @@ -99,3 +183,14 @@ json2proplist(JSON) ->
%                                   ])
%         end;
%     recursive_from_proplist(Other) -> Other.

-ifdef(TEST).
-include_lib("eunit/include/eunit.hrl").

to_proplist_test() ->
Account_info = #account_info{name = costin, balance = 50, pin = 123},
Account = #account{id = 1, details=Account_info},
Proplist = "[{name, costin}, {balance, 50}, {pin, 123}]",
?_assertEqual(Proplist, to_proplist(Account)).

-endif.
12 changes: 12 additions & 0 deletions home/visan/erl_course/ebank/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/ebin
/doc
/_test
/.eunit
/docs
.DS_Store
/TEST-*.xml
/deps
/.rebar
*.swp
*.beam
*.dump
24 changes: 24 additions & 0 deletions home/visan/erl_course/ebank/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
PREFIX:=../
DEST:=$(PREFIX)$(PROJECT)

REBAR=./rebar

.PHONY: all edoc test clean build_plt dialyzer app

all:
@$(REBAR) prepare-deps

edoc: all
@$(REBAR) doc

test:
@rm -rf .eunit
@mkdir -p .eunit
@$(REBAR) eunit

clean:
@$(REBAR) clean

app:
@[ -z "$(PROJECT)" ] && echo "ERROR: required variable PROJECT missing" 1>&2 && exit 1 || true
@$(REBAR) -r create template=mochiwebapp dest=$(DEST) appid=$(PROJECT)
19 changes: 19 additions & 0 deletions home/visan/erl_course/ebank/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/sh

# workaround for rebar mustache template bug
DEFAULT_PORT=8080
HOST=${HOST:-127.0.0.1}
PORT=${PORT:-${DEFAULT_PORT}}

BENCH_RUN="siege -q -c400 -r100 -b http://$HOST:$PORT/hello_world"

sleep 120

echo ""
echo ""
for i in `seq 1 10`;
do
echo "Running test #$i:"
$BENCH_RUN
sleep 90
done
8 changes: 8 additions & 0 deletions home/visan/erl_course/ebank/priv/www/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>It Worked</title>
</head>
<body>
/home/visan/erl_course/ebank running.
</body>
</html>
Binary file added home/visan/erl_course/ebank/rebar
Binary file not shown.
7 changes: 7 additions & 0 deletions home/visan/erl_course/ebank/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%% -*- erlang -*-
{erl_opts, [debug_info]}.
{deps, [
{mochiweb, ".*",
{git, "git://github.com/mochi/mochiweb.git", {branch, "master"}}}]}.
{cover_enabled, true}.
{eunit_opts, [verbose, {report,{eunit_surefire,[{dir,"."}]}}]}.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%% -*- erlang -*-
{application, /home/visan/erl_course/ebank,
[{description, "/home/visan/erl_course/ebank"},
{vsn, "0.1"},
{modules, []},
{registered, []},
{mod, {'/home/visan/erl_course/ebank_app', []}},
{env, []},
{applications, [kernel, stdlib, crypto]}]}.
30 changes: 30 additions & 0 deletions home/visan/erl_course/ebank/src/home/visan/erl_course/ebank.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
%% @author Mochi Media <[email protected]>
%% @copyright 2010 Mochi Media <[email protected]>

%% @doc /home/visan/erl_course/ebank.

-module(/home/visan/erl_course/ebank).
-author("Mochi Media <[email protected]>").
-export([start/0, stop/0]).

ensure_started(App) ->
case application:start(App) of
ok ->
ok;
{error, {already_started, App}} ->
ok
end.


%% @spec start() -> ok
%% @doc Start the /home/visan/erl_course/ebank server.
start() ->
/home/visan/erl_course/ebank_deps:ensure(),
ensure_started(crypto),
application:start(/home/visan/erl_course/ebank).


%% @spec stop() -> ok
%% @doc Stop the /home/visan/erl_course/ebank server.
stop() ->
application:stop(/home/visan/erl_course/ebank).
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
%% @author Mochi Media <[email protected]>
%% @copyright /home/visan/erl_course/ebank Mochi Media <[email protected]>

%% @doc Callbacks for the /home/visan/erl_course/ebank application.

-module(/home/visan/erl_course/ebank_app).
-author("Mochi Media <[email protected]>").

-behaviour(application).
-export([start/2,stop/1]).


%% @spec start(_Type, _StartArgs) -> ServerRet
%% @doc application start callback for /home/visan/erl_course/ebank.
start(_Type, _StartArgs) ->
/home/visan/erl_course/ebank_deps:ensure(),
/home/visan/erl_course/ebank_sup:start_link().

%% @spec stop(_State) -> ServerRet
%% @doc application stop callback for /home/visan/erl_course/ebank.
stop(_State) ->
ok.
Loading