Created
March 10, 2016 20:34
-
-
Save alexblackie/536712846d17bb8f30a1 to your computer and use it in GitHub Desktop.
make maps from an epgsql query result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-module(db_object_serialiser). | |
-include_lib("eunit/include/eunit.hrl"). | |
-export([serialise/2]). | |
serialise(Columns, Datas) -> | |
Keys = lists:map(fun(Col) -> | |
element(2, Col) | |
end, Columns), | |
lists:map(fun(Val) -> | |
lists:foldl(fun(K, Acc2) -> | |
Idx = map_size(Acc2) + 1, | |
maps:put(K, element(Idx, Val), Acc2) | |
end, maps:new(), Keys) | |
end, Datas). | |
serialise_test() -> | |
% example data from the epgsql readme | |
% https://github.com/epgsql/epgsql#simple-query | |
Columns = [ | |
{column, <<"id">>, int4, 4, -1, 0}, | |
{column, <<"name">>, text, -1, -1, 0}], | |
Datas = [{<<"1">>, <<"alice">>}], | |
[Res] = serialise(Columns, Datas), | |
?assert(maps:get(<<"id">>, Res) == <<"1">>), | |
?assert(maps:get(<<"name">>, Res) == <<"alice">>). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment