如何从类型安全配置中的列表中删除项目?

How to remove an item from list in typesafe config?

在 Typesafe 配置中,有一个非常有用的运算符 +=,它将一个值附加到现有的值列表中。有没有办法做相反的事情,即从现有列表中删除一个项目?

在较新版本的 Play Framework (2.4+) 中,+= 运算符用于告知依赖注入容器启用或禁用哪些模块。

play {
  modules {
    disabled += "play.api.cache.EhCacheModule"
    enabled += "com.github.mumoshu.play2.memcached.MemcachedModule"
  }
}

Typesafe 配置还支持将配置文件相互包含,这通常用于 partially override configuration in different environments. Unfortunately Play treats enabled and disabled lists as sets and once a module is added to disabled list there is no way to enable it back. This has been a source of issues and even a special note is given in Play's documentation 阻止使用 disabled 列表。

Note: If you are working on a library, it is highly discouraged to use play.modules.disabled to disable modules, as it can lead to nondeterministic results when modules are loaded by the application (see this issue for reasons on why you should not touch play.modules.disabled). In fact, play.modules.disabled is intended for end users to be able to override what modules are enabled by default.

为了能够有条件地禁用模块,我想出了一个丑陋的解决方法,使用允许变量替换的类型安全配置功能

application.conf 我有

play {
  modules {
    disabled +=  ${memcached.disabled}"com.github.mumoshu.play2.memcached.MemcachedModule"
  }
}

memcached.disabled = ""

然后在 production.conf 我放了这样的东西

include "application.conf"

memcached.disabled = "x"
play.modules.disabled += "play.api.cache.EhCacheModule"

所以当使用 production.conf 时,它会破坏 application.conf 中的禁用。显然,这不是一个可接受的解决方案。

下面的配置呢:

application.conf

play {
  modules {
    enabled += "play.api.cache.EhCacheModule"
  }
}

production.conf

include "application.conf"

play {
  modules {
    enabled += "com.github.mumoshu.play2.memcached.MemcachedModule"
    disabled += "play.api.cache.EhCacheModule"
  }
}