Search code examples
typesafehocon

How to make Array of objects


I have this file (campanhas.conf) working well:

campanhas = [
{
    codigo = CT1
    nome = Campanha 1
    descricao = Campanha de Teste
    modoAtendimento = power
    grupoDAC = grupo1

}
{
    codigo = CT2
    nome = Campanha 2
    descricao = Testeee
    modoAtendimento = preview
    grupoDAC= grupo2
}
]

But I'd like to declare objects before to be easier to order them later. Something like this:

CT1{
    codigo = CT1
    nome = Campanha 1
    descricao = Campanha de Teste
    modoAtendimento = power
    grupoDAC = grupo1

}

CT2{
    codigo = CT2
    nome = Campanha 2
    descricao = Testeee
    modoAtendimento = preview
    grupoDAC= grupo2
}

campanhas = [${CT2}, ${CT1}]

But it Says:

substitution not resolved: ConfigReference(${CT2})

How can I create an array of previously declared objects?


Solution

  • There wasn't any problem with hocon file itself.

    When reading the conf I was only using parseFile(). I had to use the resolve().

        private Config loadConfig(File file) {
    
        Config cfg = ConfigFactory.parseFile(file);
        if (cfg == null)
            throw new CoreRuntimeException(MessageFormatter.format(
                    "Arquivo {} não foi encontrado no CLASSPATH.", file));
        cfg = cfg.resolve();
        return cfg;
    }