config icon indicating copy to clipboard operation
config copied to clipboard

Unable to merge two configs

Open hemantpandey17 opened this issue 4 years ago • 3 comments

I have two conf files - application.conf and sample.conf

application.conf

{
    "mylist" {
      "records": [
        {
          "fileName": "path/to/file1.java",
          "lastModified": "0",
        },
        {
          "fileName": "path/to/file2.java",
          "lastModified": "0",
        }
      ]
    }
    xyz : true
}

sample.conf

{
  "mylist": {
    "records": [
      {
        "fileName": "path/to/file3.java",
        "lastModified": "",
      },
      {
        "fileName": "path/to/file4.java",
        "lastModified": "",
      }
  ]
}
}

Now,

Config mylistConfig = ConfigFactory.parseFile(new File("sample.conf"));
Config appConfig = ConfigFactory.load();

public Config getMergedConfig() {
  	return ConfigFactory.load(mylistConfig).withFallback(appConfig);
  }

The above function instead of merging overwrites. Only shows me result of mylist from sample.conf or application.conf whatever is written first in load-fallback order.. Do the list in confs gets overwritten ?

Is there any way to get records from both conf files ?

hemantpandey17 avatar Apr 20 '20 22:04 hemantpandey17

only objects auto-merge. to concat lists or strings you need to put in a substitution like ${mylist.records} [ “concat this value” ]

havocp avatar Apr 20 '20 22:04 havocp

@havocp Thanks for replying.

I was also checking out the concatenation operatoe.

Let's say I want to take values of application.conf as :

{
    "mylist" {
      "records":[]
      "records" += [
        {
          "fileName": "path/to/file1.java",
          "lastModified": "0",
        },
        {
          "fileName": "path/to/file2.java",
          "lastModified": "0",
        }
      ]
    }
    xyz : true
}

But I get an error now : records has type list of LIST rather than list of OBJECT

Any idea with this ?

hemantpandey17 avatar Apr 21 '20 21:04 hemantpandey17

only objects auto-merge. to concat lists or strings you need to put in a substitution like ${mylist.records} [ “concat this value” ]

@havocp Are you suggesting :

{
    "mylist" {
      "records" : []
      "records" = ${?whitelist.records} [
        {
          "fileName": "path/to/file1.java",
          "lastModified": "0",
        },
        {
          "fileName": "path/to/file2.java",
          "lastModified": "0",
        }
      ]
    }
    xyz : true
}

Using this in application.conf should merge both whitelist object in both configs using withFallback() ?

hemantpandey17 avatar Apr 26 '20 00:04 hemantpandey17