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

I wish there was a getv equivalent that accepted a default value #219

Closed
larsks opened this issue Mar 11, 2015 · 18 comments
Closed

I wish there was a getv equivalent that accepted a default value #219

larsks opened this issue Mar 11, 2015 · 18 comments
Assignees

Comments

@larsks
Copy link

larsks commented Mar 11, 2015

My templates often end up being far too verbose because of things like this:

option = {{if exists "/example/app/option"}}{{getv "/example/app/option"}}{{else}}default_value{{end}}

That makes it quite hard to visually parse the config files. Much easier would be something like:

option = {{getv "/example/app/option" "default_value"}}
@kelseyhightower
Copy link
Owner

@larsks I wonder if we can do this on a way that backwards compatible.

@bacongobbler
Copy link
Collaborator

does {{ or (getv "/example/app/option") "default_value"}} accomplish the same thing?

@bacongobbler
Copy link
Collaborator

kelseyhightower/memkv#4

@larsks
Copy link
Author

larsks commented Mar 24, 2015

@bacongobbler While that expression accomplishes the same thing, for someone coming from a different background where things like ${somevar:-value} or {{somevar|default('value')}} are possible it seems needlessly verbose.

That linked patch looks like a step in the right direction, although it could use some documentation to go with it.

@bacongobbler
Copy link
Collaborator

@larsks that expression actually doesn't work; tried it myself today. However, with the proposed function it would look like {{ getord "/example/key" "default" }}.

@kelseyhightower
Copy link
Owner

@bacongobbler @larsks I wish go template function took optional params. Some people want thing to error if the key does not exist, some one a default value, some want a default empty value.

I guess we could solve this with more template functions, or figure out new functions that take options.

@bacongobbler
Copy link
Collaborator

Alternatively, a function which returns an empty string instead of an error would allow us to take advantage of the builtin or func. For example, {{ or (funcName "/example/app/option") "default_value"}} would default to "default_value" if the key did not exist and returned an empty string. This is thanks to how or works:

Returns the boolean OR of its arguments by returning the first non-empty argument or the last argument, that is, "or x y" behaves as "if x then x else y". All the arguments are evaluated.

That should minimize it down to 2 different functions while covering all use cases: one to return an empty string and one returning an error when the key does not exist.

@Dynom
Copy link

Dynom commented Jul 24, 2015

Highly desired feature from my part. Due to the limited scoping flexibility it's very hard to produce default values when dealing with single-line properties (such as some HAProxy configuration lines).

@technosophos
Copy link

Could adding a default function accomplish this?

getv "foo" | default "bar"

Where default is basically backed by func Default(def interface{}, given interface{}) interface{}. The Default func then evaluates given to see if it's "empty" (however you want to define that), and returns either given if non-empty, or def if empty.

@technosophos
Copy link

@Dynom
Copy link

Dynom commented Aug 14, 2015

I don't have a particular preference for syntax, so whatever fits the convention is fine by me. The proposed .. | default "value" sounds excellent 👍

@mishak87
Copy link

mishak87 commented Nov 2, 2015

👍 Any progress?

@fruch
Copy link

fruch commented Jul 27, 2016

👍

it's a bit confusing that the documetion mention this now, and the version 0.11.0 doesn't have it,
maybe adding a link in the documetion to 0.12.0-alpha3, or add a comment add in version.

@bacongobbler
Copy link
Collaborator

@fruch I'd accept a PR to remove that documentation.

@estahn
Copy link

estahn commented Nov 23, 2016

Any progress?

@bacongobbler
Copy link
Collaborator

none.

@qvdk
Copy link

qvdk commented Feb 8, 2017

The documentation should be fixed since the default value is not available as described in the documentation.

@okushchenko
Copy link
Collaborator

Fixed in 4388e4b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants