added method subset(…) to Configuration interface, added implementation in JsonConfig
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -11,7 +11,6 @@ spotless {
|
||||
target("**/src/**/java/**/*.java")
|
||||
removeUnusedImports()
|
||||
importOrder()
|
||||
clangFormat("18.1.8").style("file:config/clang-format")
|
||||
licenseHeader("/* © SRSoftware 2024 */")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ object Meta {
|
||||
val COMPONENT_TYPE = "java" // "java" or "versionCatalog"
|
||||
val GROUP = "de.srsoftware"
|
||||
val ARTIFACT_ID = "configuration.api"
|
||||
val VERSION = "1.0.0"
|
||||
val VERSION = "1.0.1"
|
||||
val PUBLISHING_TYPE = "AUTOMATIC" // USER_MANAGED or AUTOMATIC
|
||||
val SHA_ALGORITHMS = listOf("SHA-256", "SHA-512") // sha256 and sha512 are supported but not mandatory. Only sha1 is mandatory but it is supported by default.
|
||||
val DESC = "SRSoftware Configuration API"
|
||||
|
||||
@@ -44,4 +44,12 @@ public interface Configuration {
|
||||
* @throws IOException if altering the configuration fails
|
||||
*/
|
||||
<C extends Configuration> C set(String key, Object value) throws IOException;
|
||||
|
||||
/**
|
||||
* get a subset of this configuration
|
||||
*
|
||||
* @param key specifies, which subset is requested
|
||||
* @return the part of the Configuration which is located at the key
|
||||
*/
|
||||
Optional<? extends Configuration> subset(String key);
|
||||
}
|
||||
@@ -14,7 +14,7 @@ object Meta {
|
||||
val COMPONENT_TYPE = "java" // "java" or "versionCatalog"
|
||||
val GROUP = "de.srsoftware"
|
||||
val ARTIFACT_ID = "configuration.json"
|
||||
val VERSION = "1.0.1"
|
||||
val VERSION = "1.0.2"
|
||||
val PUBLISHING_TYPE = "AUTOMATIC" // USER_MANAGED or AUTOMATIC
|
||||
val SHA_ALGORITHMS = listOf("SHA-256", "SHA-512") // sha256 and sha512 are supported but not mandatory. Only sha1 is mandatory but it is supported by default.
|
||||
val DESC = "SRSoftware Configuration: Json-based Implementation"
|
||||
|
||||
@@ -36,6 +36,15 @@ public class JsonConfig implements Configuration {
|
||||
json = new JSONObject(Files.readString(file.toPath()));
|
||||
}
|
||||
|
||||
/**
|
||||
* create a JsonConfig instance from a Json object
|
||||
* @param json the data to use
|
||||
*/
|
||||
public JsonConfig(JSONObject json){
|
||||
file = null;
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new JsonConfig using the passed applicationName
|
||||
* @param applicationName this determines the name of the file, to which data are stored
|
||||
@@ -148,6 +157,12 @@ public class JsonConfig implements Configuration {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<JsonConfig> subset(String key) {
|
||||
Optional<JSONObject> json = get(key);
|
||||
return json.map(JsonConfig::new);
|
||||
}
|
||||
|
||||
private Stack<String> toPath(String key) {
|
||||
var parts = key.split("\\.");
|
||||
var path = new Stack<String>();
|
||||
|
||||
@@ -117,4 +117,19 @@ public class JsonConfigTest {
|
||||
config.drop("hello");
|
||||
assertEquals("{}", config.flat());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSubset() throws IOException {
|
||||
config.set("a.a.a", "aaa");
|
||||
config.set("a.a.b", "aab");
|
||||
config.set("a.b.a", "aba");
|
||||
config.set("a.b.b", "abb");
|
||||
assertEquals("{\"a\":{\"a\":{\"a\":\"aaa\",\"b\":\"aab\"},\"b\":{\"a\":\"aba\",\"b\":\"abb\"}}}",config.flat());
|
||||
var subset = config.subset("a");
|
||||
assertTrue(subset.isPresent());
|
||||
assertEquals("{\"a\":{\"a\":\"aaa\",\"b\":\"aab\"},\"b\":{\"a\":\"aba\",\"b\":\"abb\"}}",subset.get().flat());
|
||||
subset = config.subset("a.b");
|
||||
assertTrue(subset.isPresent());
|
||||
assertEquals("{\"a\":\"aba\",\"b\":\"abb\"}",subset.get().flat());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user