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

get key from dict with default #173

Open
stdweird opened this issue Oct 26, 2017 · 7 comments
Open

get key from dict with default #173

stdweird opened this issue Oct 26, 2017 · 7 comments
Milestone

Comments

@stdweird
Copy link
Member

stdweird commented Oct 26, 2017

related to discussion in #167

it would be nice if function get (or other function name) could support dicts, eg get(adict['key'], "somedefault") e.g.

"/path" = if (exists(adict['key']) && is_defined(adict['key'])) {
    adict["key"];
} else {
    2;
};

would be replaced by

"/path" = get(adict["key"], 2);
@ned21
Copy link
Contributor

ned21 commented Oct 26, 2017

Can't you write this using long path assignment?

'/path/to/akey/key' ?= 'somedefault';

Maybe the enhancement is to allow use of ?= to local variables?

@jouvin
Copy link
Contributor

jouvin commented Oct 27, 2017

I am not sure if ?= for local variables (if implementable) really makes sense. As the variable if local, if's generally easy to manage the "default value" for the local variable. It is really different for global variables where many templates can participate in the value definition, without the ability to predict which ones, in which order.

I guess that @stdweird RFE is more for global variables anyway... I agree that it would simplify some templates....

@stdweird stdweird added this to the 10.8 milestone Apr 18, 2018
@stdweird
Copy link
Member Author

i propose to rename it to get, if_value is too close to value

@jrha
Copy link
Member

jrha commented Apr 25, 2018

I like the idea and I like the name get

@stdweird stdweird changed the title if_value get key from dict with default Apr 25, 2018
@stdweird
Copy link
Member Author

@ned21 any objections?

@jouvin
Copy link
Contributor

jouvin commented Apr 25, 2018

As mentioned at LAL workshop, should check if it makes sense to implement it as an extension of value() which does something similar for configuration paths since 10.6 (or .7)?

@ned21
Copy link
Contributor

ned21 commented Apr 25, 2018

No objections. The default would only apply if the first arg evaluates to undef.

get(undef, "default"); --> "default"
get(null, "default"); --> null

@jrha jrha modified the milestones: 10.8, 10.9 Dec 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants