Skip to content

Instantly share code, notes, and snippets.

@alexblackie
Created March 10, 2016 20:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexblackie/536712846d17bb8f30a1 to your computer and use it in GitHub Desktop.
Save alexblackie/536712846d17bb8f30a1 to your computer and use it in GitHub Desktop.
make maps from an epgsql query result
-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