Fixed extended data handling in `ts_json
` module.
* Refactored ``ts_json:ext_data_to_ejson{1,2}`` from a function that iterates over a list of extended data properties and returns a transformed list to ``ts_json:ext_data_to_ejson/1``, a function that transforms one extended data property. The iteration behavior is acheived using ``lists:map/2``. * Refactored ``ts_json:ejson_to_ext_data{1,2}`` in a similar fashion to ``ts_json:ext_data_to_ejson{1,2}``. * Fixed ``ts_json:construct_record/3`` to use ``ts_json:ejson_to_ext_data/1``.
This commit is contained in:
parent
8bb50c058d
commit
7d11112226
@ -19,7 +19,7 @@ record_to_ejson(Record=#ts_user{}, ExtData) ->
|
||||
{name, Record#ts_user.name},
|
||||
{email, Record#ts_user.email},
|
||||
{join_date, encode_datetime(Record#ts_user.join_date)}] ++
|
||||
ext_data_to_ejson(ExtData)};
|
||||
lists:map(fun ext_data_to_ejson/1, ExtData)};
|
||||
|
||||
% Timeline JSON record stucture:
|
||||
% {"user_id": "john_doe",
|
||||
@ -37,7 +37,7 @@ record_to_ejson(Record=#ts_timeline{}, ExtData) ->
|
||||
{id, TimelineId},
|
||||
{created, encode_datetime(Record#ts_timeline.created)},
|
||||
{description, Record#ts_timeline.desc}] ++
|
||||
ext_data_to_ejson(ExtData)};
|
||||
lists:map(fun ext_data_to_ejson/1, ExtData)};
|
||||
|
||||
% Entry JSON record structure:
|
||||
% {"user_id": "john_doe",
|
||||
@ -62,20 +62,16 @@ record_to_ejson(Record=#ts_entry{}, ExtData) ->
|
||||
{timestamp, encode_datetime(DateTime)},
|
||||
{mark, Record#ts_entry.mark},
|
||||
{notes, Record#ts_entry.notes}] ++
|
||||
ext_data_to_ejson(ExtData)}.
|
||||
lists:map(fun ext_data_to_ejson/1, ExtData)}.
|
||||
|
||||
encode_datetime({{Year, Month, Day}, {Hour, Minute, Second}}) ->
|
||||
lists:flatten(io_lib:format("~4.10.0B-~2.10.0B-~2.10.0BT~2.10.0B:~2.10.0B:~2.10.0B.~3.10.0BZ",
|
||||
[Year, Month, Day, Hour, Minute, Second, 000])).
|
||||
|
||||
ext_data_to_ejson(ExtData) -> ext_data_to_ejson(ExtData, []).
|
||||
|
||||
ext_data_to_ejson([], ExtData) -> ExtData;
|
||||
|
||||
ext_data_to_ejson([{Key, Value}|T], Acc) ->
|
||||
ext_data_to_ejson({Key, Value}) ->
|
||||
case Key of
|
||||
entry_exclusions -> ext_data_to_ejson(T, [{Key, {array, Value}}|Acc]);
|
||||
_Other -> ext_data_to_json(T, [{Key, Value}|Acc])
|
||||
entry_exclusions -> {Key, {array, Value}};
|
||||
_Other -> {Key, Value}
|
||||
end.
|
||||
|
||||
ejson_to_record(Empty, {struct, EJSONFields}) ->
|
||||
@ -118,7 +114,9 @@ construct_record(Timeline=#ts_timeline{}, [{Key, Val}|Fields], ExtData) ->
|
||||
Fields, ExtData);
|
||||
description -> construct_record(Timeline#ts_timeline{desc = Value},
|
||||
Fields, ExtData);
|
||||
Other -> construct_record(Timeline, Fields, [{Key, Value}|ExtData])
|
||||
Other ->
|
||||
ExtDataProp = ejson_to_ext_data({Key, Value}),
|
||||
construct_record(Timeline, Fields, [ExtDataProp|ExtData])
|
||||
end;
|
||||
|
||||
construct_record(Entry=#ts_entry{}, [{Key, Value}|Fields], ExtData) ->
|
||||
@ -129,7 +127,9 @@ construct_record(Entry=#ts_entry{}, [{Key, Value}|Fields], ExtData) ->
|
||||
Fields, ExtData);
|
||||
mark -> construct_record(Entry#ts_entry{mark=Value}, Fields, ExtData);
|
||||
notes -> construct_record(Entry#ts_entry{notes=Vale}, Fields, ExtData);
|
||||
Other -> construct_record(Entry, Fields, [{Key, Value}|ExtData])
|
||||
Other ->
|
||||
ExtDataProp = ejson_to_ext_data({Key, Value}),
|
||||
construct_record(Entry, Fields, [ExtDataProp|ExtData])
|
||||
end;
|
||||
|
||||
construct_record(Record, [], ExtData) -> {Record, ExtData}.
|
||||
@ -157,14 +157,12 @@ decode_datetime(DateTimeString) ->
|
||||
|
||||
{Date, Time}.
|
||||
|
||||
ejson_to_ext_data(ExtData) -> ejson_to_ext_data(ExtData, []).
|
||||
|
||||
ejson_to_ext_data([], ExtData) -> ExtData;
|
||||
|
||||
ejson_to_ext_data([{Key, Value}|T], Acc) ->
|
||||
ejson_to_ext_data({Key, Value}) ->
|
||||
case Key of
|
||||
entry_exclusions -> ext_data_to_ejson(T, [{Key, element(2, Value)}|Acc]);
|
||||
_Other -> ext_data_to_json(T, [{Key, Value}|Acc])
|
||||
entry_exclusions ->
|
||||
{array, ExclList} = Value,
|
||||
{Key, ExclList};
|
||||
_Other -> {Key, Value}
|
||||
end.
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user