basho / cuttlefish

never lose your childlike sense of wonder baby cuttlefish, promise me?
Apache License 2.0
205 stars 124 forks source link

translation should allow to be ignored #79

Closed Licenser closed 10 years ago

Licenser commented 10 years ago

Basically it is possible to have a mapping not take effect when they are not present, right now there isn't a way to have this behaviour for translations.

A simple way to implement this would to change the return value of translation functions to {ok, Value} or undefined in which case undefined would mean the translation has no effect.

joedevivo commented 10 years ago

@Licenser could you include the real-world example you ran in to?

Licenser commented 10 years ago

In the schema I tried to assing the mdns ip based on weather the ip/port is set, this needed a translation, which should not set anything in the app.config when ip/port is not set.

joedevivo commented 10 years ago

@Licenser Would having the translation return 'undefined' when the IP/Port combination solve the problem?

Licenser commented 10 years ago

Yap, it would turn into {ok, undefined} with the get_env which is different form undefined aside of adding a third fork in the code path it'd break the claim that we can use get_env the same way as before ;)

joedevivo commented 10 years ago

@Licenser what if you used get_env/3 instead of get_env/2?

Licenser commented 10 years ago

Not available in erlang R15 :(, that said in R16 I could put in the code to default to {ok, undefined} but that still would be kind of a horrible hackish solution.

joedevivo commented 10 years ago

So it's no longer 'undefined' it's "defined as undefined"

Licenser commented 10 years ago

yea but get_env treats those two situations differently:

joedevivo commented 10 years ago

I'll take this scenario under advisement, but for now, your best bet is to add the third clause for reading this value:

{ok, undefined} -> blah;
undefined -> blah;
{ok, Value} -> blah
Licenser commented 10 years ago

To add to this I just thought of another issue, if someone were to use get_env/3 the default would not be picked when the value is set to undefined instead of not being defined at all breaking that behavior and being quite counter intuitive.

joedevivo commented 10 years ago

get_env/3 has it's own issues, use at your own risk ;)

seancribbs commented 10 years ago

@Licenser We now have an API function cuttlefish:unset() which can be used to skip emitting a value to the generated configuration. Closing this issue.