Permalink
Browse files

removed old project cruft

  • Loading branch information...
Justin Kirby
Justin Kirby committed Dec 24, 2011
1 parent 1f6f1ba commit c5712bd905ae832ff004e2a0c3354e4914e94b02
Showing with 92 additions and 30 deletions.
  1. +92 −30 src/json_rec.erl
View
@@ -52,41 +52,68 @@
-endif.
--include("vest_types.hrl").
--export([to_rec/3,
+
+-export([
+ to_rec/3,
to_json/2
- ]).
+ ]).
+
+-include("json_rec_types.hrl").
+
+
+%% note: I am using tuple() for record, since this is a generic record
+-spec to_json(Record :: tuple(), Module :: [atom()]) -> {struct, proplist()};
+ (Record :: tuple(), Module :: atom()) -> {struct, proplist()}.
+
+to_json(Record, Module) when is_list(Module) ->
+ Fields = module_rec_fields(Module,Record),
+ Pl = rec_keys(Fields, Record, Module, []),
+ {struct, Pl};
to_json(Record, Module) ->
- Fields = Module:'#info-'(fields,Record),
- Pl = rec_keys(Fields,Record,Module,[]),
+ Fields = module_rec_fields([Module],Record),
+ Pl = rec_keys(Fields,Record,[Module],[]),
{struct, Pl}.
+
rec_keys([], _Record, _Module, Acc) -> Acc;
rec_keys([Field|Rest],Record,Module,Acc) ->
- Value = Module:'#get-'(Field,Record),
+ Value = module_get(Module, Field, Record),
Key = list_to_binary(atom_to_list(Field)),
JsonValue = field_value(Value,Module,[]),
rec_keys(Rest, Record, Module,[{Key,JsonValue}|Acc]).
field_value(Value, Module, _Acc) when is_tuple(Value) ->
- case Module:rec(Value) of
- true ->
- to_json(Value,Module);
+ case module_has_rec(Module, Value, false) of
false ->
- Value
+ Value;
+ _M when is_atom(_M) ->
+ to_json(Value,Module)
end;
-field_value([],_Module, Acc) -> Acc;
+field_value(Value, _Module, _Acc) when is_atom(Value) ->
+ list_to_binary(atom_to_list(Value));
+
+field_value([],_Module, Acc) -> lists:reverse(Acc);
+field_value([{_,_}|_] = Pl, Module, Acc) ->
+ %% it is a proplist, make it a dict
+ {struct, [{Key, Value} || {Key, V2} <- Pl,
+ begin
+ Value = field_value(V2, Module, Acc),
+ true
+ end]};
+
field_value([Value|Rest], Module, Acc) ->
NewValue = case field_value(Value,Module,[]) of
IsRec when is_tuple(IsRec),
- is_atom(element(1,Value)) ->
- %% this returned a record, so get the first element fromthe rec tuple and do: {struct, atom
+ is_atom(element(1,Value)) ->
+ %% this returned a record, so get the first
+ %% element from the rec tuple and do: {struct,
+ %% atom
{struct, [{list_to_binary(atom_to_list(element(1,Value))),IsRec}]};
- IsTuple when is_tuple(IsTuple) ->
- tuple_to_list(IsTuple);
+ %% IsTuple when is_tuple(IsTuple) ->
+ %% tuple_to_list(IsTuple);
NotRec ->
- NotRec
+ NotRec
end,
field_value(Rest, Module,[NewValue|Acc]);
field_value(Value,_Module,_Acc) ->
@@ -105,43 +132,47 @@ field_value(Value,_Module,_Acc) ->
%% Rec is the initial empty record #record_name{} or `module:new(<<"record_name">>)'
%%
%% NOTE: it is up to you to export and define module:new/1
--spec to_rec(_Json :: json_dict(), Module :: atom(), undefined) ->
+-spec to_rec(_Json :: json_dict(), Module :: atom() | [atom()], undefined) ->
proplist();
- (_Json :: json_dict(), Module :: atom(), Rec :: tuple() ) ->
+ (_Json :: json_dict(), Module :: atom() | [atom()], Rec :: tuple() ) ->
Rec :: tuple().
-to_rec({struct, Pl} = _Json, Module, undefined) ->
+to_rec({struct, Pl} = _Json, Module, undefined) when is_list(Module) ->
pl(Pl, Module);
+to_rec({struct, Pl} = _Json, Module, undefined) ->
+ pl(Pl, [Module]);
+to_rec({struct, Pl} = _Json, Module, Rec) when is_list(Module) ->
+ keys_rec(Pl, Module, Rec);
to_rec({struct, Pl} = _Json, Module, Rec) ->
- keys_rec(Pl, Module, Rec).
+ keys_rec(Pl, [Module], Rec).
keys_rec([], _Module, Rec) -> Rec;
keys_rec([{Key, {struct, Pl}}|Rest], Module, Rec) ->
Field = list_to_atom(binary_to_list(Key)),
- Value = case Module:new(Key) of
+ Value = case module_new(Module, Key, undefined) of
undefined ->
%% this is not a sub record, so just pl it
pl(Pl,Module);
SubRec ->
%% we have a new record, go back go the topproplist
to_rec({struct,Pl}, Module, SubRec)
end,
- UpRec = Module:'#set-'([{Field,Value}],Rec),
+ UpRec = module_set(Module, {Field,Value}, Rec),
keys_rec(Rest, Module, UpRec);
keys_rec([{Key, Value}|Rest], Module, Rec) ->
Field = list_to_atom(binary_to_list(Key)),
NewValue = to_value(Value,Module),
- NewRec = Module:'#set-'([{Field,NewValue}],Rec),
+ NewRec = module_set(Module, {Field, NewValue}, Rec),
keys_rec(Rest,Module,NewRec).
pl(P, Module) ->
pl(P,Module,[]).
pl([],_M,[H]) -> H;
pl([],_M,Acc) -> Acc;
pl([{Key, {struct,Pl}}|Rest], Module, Acc) ->
- Value = case Module:new(Key) of
+ Value = case module_new(Module,Key,undefined) of
undefined ->
{Key, pl(Pl, Module, [])};
Rec ->
@@ -163,6 +194,43 @@ to_value(V,_Module,_Acc) -> V.
+module_new([], _Key, Rec) ->
+ Rec;
+module_new([H|T], Key, Rec) ->
+ case H:new(Key) of
+ undefined ->
+ module_new(T,Key,Rec);
+ SubRec ->
+ SubRec
+ end.
+
+
+module_has_rec(Ms, Rec) ->
+ module_has_rec(Ms, Rec, throw).
+
+module_has_rec([],_Rec, throw) -> throw(did_not_find_module);
+module_has_rec([],_Rec, V) -> V;
+module_has_rec([M|T],Rec, Act) ->
+ case M:rec(Rec) of
+ false ->
+ module_has_rec(T,Rec, Act);
+ true ->
+ M
+ end.
+
+
+
+module_set(Ms, Kv, Rec) ->
+ M = module_has_rec(Ms,Rec),
+ M:'#set-'([Kv],Rec).
+
+module_rec_fields(Ms, Rec ) ->
+ M = module_has_rec(Ms,Rec),
+ M:'#info-'(fields,Rec).
+
+module_get(Ms, Field, Rec) ->
+ M = module_has_rec(Ms, Rec),
+ M:'#get-'(Field,Rec).
@@ -258,10 +326,4 @@ to_json_deep_deep_test() ->
New = ?MODULE:to_rec(mochijson2:decode(Sjson), ?MODULE, ?MODULE:new(<<"deep">>)),
?assertEqual(Rec,New).
-to_json_tuple_to_list_test() ->
- [Json, Rec] = simple_json_t2l_data(),
- Conv = ?MODULE:to_json(Rec,?MODULE),
- Sjson = lists:flatten(mochijson2:encode(Conv)),
- ?assertEqual(Json,Sjson).
-
-endif.

0 comments on commit c5712bd

Please sign in to comment.