Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/keenwrite.git

Guard against no variable file specified

AuthorDaveJarvis <email>
Date2022-01-05 22:47:18 GMT-0800
Commit22a0b199903b2663149abc4dd8e89afcc8fc4a60
Parent1faebe9
Delta23 lines added, 4 lines removed, 19-line increase
src/main/java/com/keenwrite/cmdline/Arguments.java
}
+ /**
+ * Parses the given YAML document into a map of key-value pairs.
+ *
+ * @param vars Variable definition file to read, may be {@code null} if no
+ * variables are specified.
+ * @return A non-interpolated variable map, or an empty map.
+ * @throws IOException Could not read the variable definition file
+ */
private static Map<String, String> parse( final Path vars )
throws IOException {
- final var yaml = read( vars );
- final var factory = new YAMLFactory();
- final var json = new ObjectMapper( factory ).readTree( yaml );
final var map = new HashMap<String, String>();
- parse( json, "", map );
+ if( vars != null ) {
+ final var yaml = read( vars );
+ final var factory = new YAMLFactory();
+ final var json = new ObjectMapper( factory ).readTree( yaml );
+
+ parse( json, "", map );
+ }
return map;
}
private static void parse(
final JsonNode json, final String parent, final Map<String, String> map ) {
+ assert json != null;
+ assert parent != null;
+ assert map != null;
+
json.fields().forEachRemaining( node -> parse( node, parent, map ) );
}
private static void parse(
final Entry<String, JsonNode> node,
final String parent,
final Map<String, String> map ) {
+ assert node != null;
+ assert parent != null;
+ assert map != null;
+
final var jsonNode = node.getValue();
final var keyName = parent + "." + node.getKey();