From 34729ee7185d85758403d549a32daaa016c50b43 Mon Sep 17 00:00:00 2001 From: Tian Tan Date: Mon, 18 Dec 2023 19:20:41 +0800 Subject: [PATCH] Fix option parser, now treat only the first colon as delimiter between a key and a value (before each colon is treated as delimiter) --- .../java/pascal/taie/config/PlanConfig.java | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/main/java/pascal/taie/config/PlanConfig.java b/src/main/java/pascal/taie/config/PlanConfig.java index 4c7593242..de2505c57 100644 --- a/src/main/java/pascal/taie/config/PlanConfig.java +++ b/src/main/java/pascal/taie/config/PlanConfig.java @@ -37,6 +37,7 @@ import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.StringJoiner; /** * Configuration for an analysis to be executed. @@ -114,15 +115,12 @@ public static List readConfigs(Options options) { .stream() .map(entry -> { String id = entry.getKey(); - // Convert option string to a valid YAML string - String opts = entry.getValue() - .replace(';', '\n') - .replace(":", ": "); + String optStr = toYAMLString(entry.getValue()); try { - Map optsMap = opts.isEmpty() ? - Map.of() : + Map optsMap = optStr.isBlank() + ? Map.of() // Leverage Jackson to parse YAML string to Map - mapper.readValue(opts, mapType); + : mapper.readValue(optStr, mapType); return new PlanConfig(id, new AnalysisOptions(optsMap)); } catch (JsonProcessingException e) { throw new ConfigException("Invalid analysis options: " + @@ -132,6 +130,25 @@ public static List readConfigs(Options options) { .toList(); } + /** + * Converts option string to a valid YAML string. + * The option string is of format "key1:value1;key2:value2;...". + */ + private static String toYAMLString(String optValue) { + if (optValue.isBlank()) { + return optValue; + } + StringJoiner joiner = new StringJoiner("\n"); + for (String keyValue : optValue.split(";")) { + if (!keyValue.isBlank()) { + int i = keyValue.indexOf(':'); // split keyValue + joiner.add(keyValue.substring(0, i) + ": " + + keyValue.substring(i + 1)); + } + } + return joiner.toString(); + } + /** * Writes a list of PlanConfigs to given file. */