Access to array elements in TypeSafe configuration

For a nested JSON configuration, for example:

{ app: { id: "app1" instances: 2, servers: [ { host: "farm1.myco.com", port: 9876 } { host: "farm2.myco.com", port: 9876 } ] } } 

When using typeSafe configuration, is it possible to access array elements directly in the path?

At the moment, we need to do something like the following, which is pretty detailed:

 val servers = config.getObjectList("app.id") val server = servers.get(0).toConfig val host = server.getString("host") val port = server.getInt("port") 

Something like this would be ideal:

val host = config.getString("app.id.servers.0.host") ?

Does the TypeSafe API support something like this?

+5
source share
2 answers
+2
source

Implement a helper function that turns an object into something more manageable. This is what I used.

 def toList(jList:java.util.List[_ <: ConfigObject]) : List[Config] = { val l = jList.asScala.toList val slConfig = l.map(item => { item.toConfig }) slConfig } 

Using this function will look like

 val servers = toList(objectList) for(server <- servers) { println(server.getString("host")) println(server.getString("port")) } 
0
source

Source: https://habr.com/ru/post/1203216/


All Articles