17

The docs say that I can access environment variables like this:

database_name = "${?DB_NAME}" 

Where DB_NAME is an environment variable. Do I have to put the question mark ? or is that there to prevent a crash if it doesn't exist?

Also, are we allowed to reference a variable in a file before it is declared?

foo = "hello-${bar}" bar = "baz" 
1
  • Better to ask two separate questions, one for each of your concerns. Commented Feb 15, 2017 at 4:10

1 Answer 1

18

1- ? means optional. If the environment variable does not exist at runtime, the whole line will be ignored, as if it didn't exists in the first place. So use it when you want optional overriding.

See Optional Env Vars section of the docs for more info.

Note that ? works for all substitutions and not just env vars.

2- Yes, you can reference a variable which is defined later in the config file (forward referencing). As long as the value exists and there is no cycle (circular dependency), the substitution will be done successfully.

Think of it like this: first the whole file will be parsed at runtime and the value of the literals will be bound to their vars, and then the substitutions will take place, so the order doesn't really matter.

Sign up to request clarification or add additional context in comments.

5 Comments

so do I HAVE to use ? with environmental variables? if not, how do I access them normally?
No, you don't have to use them. Only if you want optional overriding. Normal use case would be myVar = ${MY_ENV_VAR}.
I see, just confused how it knows it is a ENV VAR then, caps lock? :)
No, by default it takes all system properties and environment variables into account, the format doesn't really matter. BTW, be aware that even if you just have myVar = 2 but in runtime there is an env var with the same name, say export myVar = 10 your config value will be overridden! Personally I don't like this so try to use namespaced config params (e.g. app.settings.feature1.myVar or with curly braces app { settings { feature { myVar = ....)
By the way, the docs mentions everything in a much clearer way :) So I'd read the whole docs (it's just a few pages) to grasp a better understanding. You can also look at their code and debug it: github.com/typesafehub/config

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.