Typesafe Config:在以后的设置中替代替换的最佳模式?

Lui*_*las 5 typesafe-stack typesafe-config

好的,我尝试了以下方法,但并没有达到我想要的效果。我有一个reference.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 = ${myapp.s3.prefix}"/file1.csv"
    file2 = ${myapp.s3.prefix}"/file2.csv"
    // ...
}
Run Code Online (Sandbox Code Playgroud)

然后,我提供一个application.conf看起来或多或少像这样的文件:

// application.conf
myapp.s3.prefix = "s3://some-bucket/some/path/to/the/files"
myapp.file2 = "s3://some-other-bucket/some/path/file2.csv"
Run Code Online (Sandbox Code Playgroud)

现在,当我的应用程序执行时ConfigFactory.load(),来自:

  1. 解析reference.conf文件,执行替换,并生成一个Config对象,其中:
    • 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"
  2. 解析application.conf文件并生成一个Config对象,其中:
    • library.prefix = "/some/path/to/the/files"
    • myapp.file2 = "s3://some-other-bucket/some/path/file2.csv"
  3. 合并两个对象,并将该reference.conf对象作为后备对象。Config因此,生成的对象具有:
    • library.prefix = "s3://some-bucket/some/path/to/the/files"(如application.conf
    • library.file1 = "s3://environment/variable/S3_PREFIX/missing/file1.csv"(如中的reference.conf)。
    • library.file2 = "s3://some-other-bucket/some/path/file2.csv"(如中的application.conf)。

但是,正如您可能从我的示例中猜到的那样,我正在尝试做的是具有对于根目录的reference.conf指定默认路径,并允许这两种方式的任意组合:

  1. 从中覆盖默认的根目录application.conf,以便从中reference.conf查找来自的默认相对路径。
  2. 覆盖中的任何单个文件的路径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) {
   // ...
}
Run Code Online (Sandbox Code Playgroud)

我不太喜欢这种解决方案。有人能想到更好的东西吗?