Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing boolean values from application.conf in a scala template

I have a boolean parameter in application.conf:

system.debugMode = false

And I'm trying to branch based on the value of this in my scala template:

<p>Debug mode parameter value: @Play.current.configuration.getBoolean("system.debugMode")</p>

@if(Play.current.configuration.getBoolean("system.debugMode")) {
    <p>Debug mode on</p>
} else {
    <p>Debug mode off</p>
}

I would expect to see the output 'debug mode off', but what I actually see rendered is:

Debug mode parameter value: false

Debug mode on

Do I have a casting issue here? It seems my value is coming back from the config file as 'false' but the @if statement is evaluating it as true. I note that the API states that the getBoolean method returns an option containing a boolean, so maybe this can't be put into an if evaluation?

like image 857
Matt Avatar asked Sep 17 '13 12:09

Matt


People also ask

What is ConfigFactory in Scala?

Configurations from a fileBy default, the ConfigFactory looks for a configuration file called application. conf. If willing to use a different configuration file (e.g.: another. conf), we just need to indicate a different file name and path to load (e.g.: ConfigFactory. load("another")).

What is Boolean in Scala?

Boolean (equivalent to Java's boolean primitive type) is a subtype of scala. AnyVal. Instances of Boolean are not represented by an object in the underlying runtime system. There is an implicit conversion from scala.

What is application conf?

application. conf is a file for application developers. A developer can set values for properties used by libraries in case they are not the same as defaults. Also he/she can declare his own properties and refer them from the code.


1 Answers

play.api.Configuration.getBoolean() returns an Option[Boolean]. In Play's template engine, an Option containing Some(...) will always evaluate to true in an if conditional, even if the option contains Some(false).

As a test, I've created all possible values for an Option[Boolean] and tested what happened with them inside an @if(...) in a template.

Controller:

object Application extends Controller {
  def index = Action {
    val a: Option[Boolean] = None
    val b: Option[Boolean] = Some(true)
    val c: Option[Boolean] = Some(false)
    Ok(views.html.index(a, b, c))
  }
}

Template:

@(a: Option[Boolean], b: Option[Boolean], c: Option[Boolean])

@if(a) { a }
@if(b) { b }
@if(c) { c }

Running it gives the output "b c".

If your config parameter has a default value, get the Option's value with getOrElse:

Play.current.configuration.getBoolean("system.debugMode").getOrElse(defaultValue)

If you're sure that the config parameter will always be there (or you're happy with your template reporting that debug mode is off if the parameter is not set), you can also flatten the option:

Play.current.configuration.getBoolean("system.debugMode").flatten
like image 171
Carsten Avatar answered Sep 19 '22 01:09

Carsten