Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More Options for URL construction ( Retain query parameters and allow Key-Value pairs) #570

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kislayentronic
Copy link

In one of the requirements I needed all the Query Params for URL construction but this was impossible with current tag.
To illustrate it more lets have this forum question https://groups.google.com/forum/#!msg/chicagoboss/IQ0d1DBKgAM/j8Asd8WPjTYJ where Evan Miller suggested/intended doing {% url foo="bar" _req.query_params %} . However this doesn't work as Variables get values like

[
 {foo, <<"bar">>},
 [
  {first_param, "x"},
  {second_param, "y"}
 ]
].

which goes unhandled by lists:map.
This can be resolved if we do a lists:flatten.

In one of the requirements I needed all the Query Params for URL construction but this was impossible with current tag. 
To illustrate it more lets have this forum question https://groups.google.com/forum/#!msg/chicagoboss/IQ0d1DBKgAM/j8Asd8WPjTYJ where Evan Miller suggested/intended doing {% url foo="bar" _req.query_params %} . However this doesn't work as Variables get values like 
[
 {foo, <<"bar">>},
 [
  {first_param, "x"},
  {second_param, "y"}
 ]
].
This can be resolved if we do a list flatten.
@danikp
Copy link
Contributor

danikp commented Jun 17, 2015

We can send nested arrays in query parameters. If you flatten them, you can lost their structure and in most cases, make a huge mess with your data.
Actually, I will appreciate if you will explain bit more about your requirements

@kislayentronic
Copy link
Author

As I understand from the code, specifically from this expression:-

    ListVars = lists:map(fun
            ({K, V}) when is_binary(V) -> {K, binary_to_list(V)}; 
            ({K, V}) -> {K, V} 
        end, Variables),

only key-value pairs or elements of proplists are allowed in Variables List (I will say it fun constraint). So any nested arrays/List should be with their key. Since lists:flatten only works on deeplist it will not destroy any other list as if there is any list, it will obviously be inside a tuple (fun constraint). Example:-

%% This is what will happen (lists inside tuple are preserved) -->
lists:flatten([{k1,v1}, {k2, v2}, {k3, [v31, v32, v33, v34]} ]).
%% Output:- [{k1,v1},{k2,v2},{k3,[v31,v32,v33,v34]}]

%% Though this kind of list (nested array) will loose its flavor, but this is not gonna processed by our fun param constraint. It will be an error anyway. 
lists:flatten([{k1,v1}, {k2, v2}, {k3, [v31, v32, v33, v34]} , [discard1, discard2, discard3], {k4, v4} ]).
%% Output:- [{k1,v1}, {k2,v2}, {k3,[v31,v32,v33,v34]}, discard1,discard2,discard3, {k4,v4}]

My Requirement
Many times you might like to add another proplist in the url tag as in case, you need all the url parameters (which is already available as a list in _req.query_params). For that you might choose to operate on query params before, right in controller and make them suitable for url tag or in my case, just pass the proplist as it is, and url tag will automatically process it.

Please let me know if I am unclear somewhere or if I am missing something.

@danikp danikp added the routing label Oct 11, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants