Configafe Config: better template for overriding substitutions in later settings?

Ok, I tried the following and it did not create the effect I would like. I have an reference.confindication of the location of the file system relative to the prefix, which looks something like this:

// reference.conf
myapp {
    // The dummy path makes the error message easy to diagnose
    s3.prefix = "s3://environment/variable/S3_PREFIX/missing"
    s3.prefix = ${?S3_PREFIX}

    file1 = ${myapp.s3.prefix}"/file1.csv"
    file2 = ${myapp.s3.prefix}"/file2.csv"
    // ...
}

And then I put in a file application.confthat looks something like this:

// application.conf
myapp.s3.prefix = "s3://some-bucket/some/path/to/the/files"
myapp.file2 = "s3://some-other-bucket/some/path/file2.csv"

Now that my application is running ConfigFactory.load(), from:

  • Parse the file reference.conf, make replacements and generate an object Configwhere:
    • myapp.s3.prefix = "/environment/variable/S3_PREFIX/missing"
    • myapp.file1 = "/environment/variable/S3_PREFIX/missing/file1.csv"
    • myapp.file2 = "/environment/variable/S3_PREFIX/missing/file2.csv".
  • Parse the file application.confand create an object Configwhere:
    • library.prefix = "/some/path/to/the/files"
    • myapp.file2 = "s3://some-other-bucket/some/path/file2.csv".
  • Merge two objects with the object reference.confas a backup. Thus, the resulting object Confighas:
    • library.prefix = "s3://some-bucket/some/path/to/the/files"(as in application.conf)
    • library.file1 = "s3://environment/variable/S3_PREFIX/missing/file1.csv"(as in reference.conf).
    • library.file2 = "s3://some-other-bucket/some/path/file2.csv"(as in application.conf).

, , , , , , reference.conf

  • application.conf, reference.conf.
  • application.conf, .

, , :

// reference.conf
myapp {
    // The dummy path makes the error message easy to diagnose
    s3.prefix = "s3:/environment/variable/S3_PREFIX/missing"
    s3.prefix = ${?S3_PREFIX}

    file1 = "file1.csv"
    file2 = "file2.csv"
    // ...
}

// application.conf
myapp.s3.prefix = "s3://some-bucket/some/path/to/the/files"
myapp.file2 = "s3://some-other-bucket/some/path/file2.csv"


// The Java code for using the config must append prefix and file location,
// and needs to have smarts about relative vs. absolute paths.

final Config CONF = ConfigFactory.load();

String getS3URLFor(String file) {
    String root = CONF.getString("myapp.s3.prefix");
    String path = CONF.getString("myapp." + file);
    return relativeVsAbsoluteSensitiveMerge(root, path);
}

String relativeVsAbsoluteSensitiveMerge(String root, String path) {
    if (isAbsoluteReference(path)) {
        return path; 
    } else {
        return root + "/" + path;
    }
}

boolean isAbsoluteReference(String path) {
   // ...
}

. - - ?

+4
1

, :

ConfigFactory.parseResources(s"file.conf")
  .withValue("myKey", ConfigValueFactory.fromAnyRef("newValue"))
  .resolve()
+1

All Articles