Typesafe配置:在以后的设置中覆盖替换的最佳模式?

时间:2014-11-26 23:46:49

标签: 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"
    // ...
}

然后我提供了一个看似或多或少的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"

现在,当我的应用程序执行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)。
  4. 但正如您可能已经从我的示例中猜到的那样,我尝试做的是让reference.conf指定默认路径 relative 到根目录,并允许任何混合两者

    1. 覆盖application.conf的默认根目录,以便从那里查找reference.conf的默认相对路径。
    2. 覆盖application.conf中任何单个文件的路径,以便应用程序可以使用不在根目录中的路径。
    3. 到目前为止,我唯一能够提出的是:

      // 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) {
         // ...
      }
      

      我不太喜欢这个解决方案。谁能想到更好的东西?

1 个答案:

答案 0 :(得分:1)

您可以先解析然后解决:

var currentTR = $("<tr><td><input type='file'/></td></tr>");
$(this).closest('tr').before(currentTR);
相关问题