flatiron/nconf — pull request #41 by digitalicarus (2012-05-30 15:07:22+00:00)
Title: Need ability to call get on multiple values until truthy one is reached

I'm not super familiar with the project so I may have gotten some things wrong. In my use case I want to deploy an app into multiple environments. Each of them surface the desired running port in a different environment variable:

Cloud Foundry: VCAP_APP_PORT
Cloud 9: PORT
Default dev port: port

New ports for new environments keep surfacing and I would like to keep this type of configuration detail out of my application logic.

My solution is that you have a new method called getFirst in the provider that takes a key to a list reference in the config. The getFirst method iterates over each item in the list and calls get on that key. The first truthy value is returned to the caller.

For example, I have the following test fixture:

{
"ports": ["VCAP_APP_PORT", "PORT", "devPort"],
"VCAP_APP_PORT": null,
"PORT": true,
"devPort": 8000,
"monPort": 8001
}

getFirst() returns the value for nconf.get("PORT") since that's the first truthy value.

This is my solution to this config problem. If you have a more novel solution that prevents this configuration detail from appearing in code logic I'd enjoy hearing it.