Skip to content

Instantly share code, notes, and snippets.

@varnerac
Last active August 29, 2015 14:26
Show Gist options
  • Save varnerac/594b065efcc5b54e26d4 to your computer and use it in GitHub Desktop.
Save varnerac/594b065efcc5b54e26d4 to your computer and use it in GitHub Desktop.
%% Map keys known at compile-time
%% No boilerplate code or parse transform for record -> JSON
%% Matching on map keys in function signatures, not possible with proplists
%% JSONFrags = [#{...}, #{...}, #{...},
process_layers(JSONFrags) -> process_layers(JSONFrags, #{}).
process_layers([], Result) ->
jsx:encode(Result).
process_layers([#{<<"key">> := <<"value">>} | Rest], Result) ->
process_layers(Rest, Result{<<"new_name">> => <<"Value">>});
process_layers([#{<<"key2">> := <<"value2">>} | Rest], #{<<"key3">> := _} = Result) ->
process_layers(Rest, Result);
process_layers([#{<<"key2">> := <<"value2">>} = Layer | Rest], Result) ->
[<<"Name">>, <<"Label">>, <<"Type">>] = maps:get(<<"Columns">>,Layer),
[[Name, _Label, _Type]] = maps:get(<<"Rows">>,Layer),
process_layers(Rest, Result#{<<"new_name">> => Name}).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment