/** * Construct the IncludeYaml class wrapping the given yaml. Use, the default, file resolver to * find included configuration. * * @param yaml to wrap */ public IncludedYaml(Yaml yaml) { this(yaml, key -> new IncludedYaml(new YamlFile(new File(key)))); }
@SuppressWarnings({"unchecked"}) private Object processMap(Map<String, Object> unprocessed) throws IOException { Map<String, Object> processed = new LinkedHashMap<>(unprocessed.size()); for (Entry<String, Object> e : unprocessed.entrySet()) { String key = e.getKey(); Object value = e.getValue(); if (INCLUDE.equals(key)) { if (value instanceof String) { return mapping.apply((String) value).dataTree(); } else if (value instanceof List) { return getIncludedList((List<String>) value); } else { throw new IllegalArgumentException("include must be a string or list of strings"); } } else { processed.put(key, processObject(value)); } } return processed; }
@SuppressWarnings("unchecked") private Object processObject(Object value) throws IOException { if (value instanceof Map) { return processMap((Map<String, Object>) value); } else if (value instanceof List) { return processList((List<Object>) value); } else { return value; } }
@SuppressWarnings("unchecked") private List<Object> processList(List<Object> unprocessed) throws IOException { List<Object> processed = new ArrayList<>(unprocessed.size()); for (Object o : unprocessed) { Object processObject = processObject(o); if (processObject instanceof List) { processed.addAll((List<Object>) processObject); } else { processed.add(processObject); } } return processed; }
@SuppressWarnings("unchecked") private List<Object> processList(List<Object> unprocessed) throws IOException { List<Object> processed = new ArrayList<>(unprocessed.size()); for (Object o : unprocessed) { Object processObject = processObject(o); if (processObject instanceof List) { processed.addAll((List<Object>) processObject); } else { processed.add(processObject); } } return processed; }
/** * Construct the IncludeYaml class wrapping the given yaml. Use, the default, file resolver to * find included configuration. * * @param yaml to wrap */ public IncludedYaml(Yaml yaml) { this(yaml, key -> new IncludedYaml(new YamlFile(new File(key)))); }
@SuppressWarnings("unchecked") private Object processObject(Object value) throws IOException { if (value instanceof Map) { return processMap((Map<String, Object>) value); } else if (value instanceof List) { return processList((List<Object>) value); } else { return value; } }
@SuppressWarnings({"unchecked"}) private Object processMap(Map<String, Object> unprocessed) throws IOException { Map<String, Object> processed = new LinkedHashMap<>(unprocessed.size()); for (Entry<String, Object> e : unprocessed.entrySet()) { String key = e.getKey(); Object value = e.getValue(); if (INCLUDE.equals(key)) { if (value instanceof String) { Object includedTree = mapping.apply((String) value).dataTree(); if (includedTree instanceof Map) { Map<String, Object> includedMap = (Map<String, Object>) includedTree; includedMap.forEach((k, v) -> processed.put(k, v)); } else { return includedTree; } } else if (value instanceof List) { return getIncludedList((List<String>) value); } else { throw new IllegalArgumentException("include must be a string or list of strings"); } } else { processed.put(key, processObject(value)); } } return processed; }
@Override public Object dataTree() throws IOException { return processObject(yaml.dataTree()); }
/** * Construct configuration from yaml file, allows yaml to use include statements * * @param file to read */ public YamlPipelineConfiguration(File file) throws IOException { super(new IncludedYaml(new YamlFile(file))); }
@Override public Object dataTree() throws IOException { return processObject(yaml.dataTree()); }
/** * Construct configuration from yaml file, allows yaml to use include statements * * @param file to read */ public YamlPiplineConfiguration(File file) throws IOException { super(new IncludedYaml(new YamlFile(file))); }
Optional<File> configurationFile = getConfigurationFile(argList.get(0)); if (configurationFile.isPresent()) { yamls.add(new IncludedYaml(new YamlFile(configurationFile.get())));
source = "string"; } else if (!Strings.isNullOrEmpty(file)) { yaml = new IncludedYaml(new YamlFile(new File(file))); source = "file"; } else {
source = "string"; } else if (!Strings.isNullOrEmpty(file)) { yaml = new IncludedYaml(new YamlFile(new File(file))); source = "file"; } else {
@Override public Yaml apply(String key) { return new IncludedYaml(new YamlFile(YamlFileTest.class, key), this); } };
@Test public void mapInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, PARENT_INCLUDE_YAML), mapping); String expected = getRaw(PARENT_INCLUDE_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(2, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator", ((Map<String, Object>) annotators.get(1)).get("class")); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); }
@Test public void nestedInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, GRAND_PARENT_YAML), mapping); String expected = getRaw(GRAND_PARENT_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(4, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); assertEquals("uk.gov.dstl.baleen.testing.DummyAnnotator1", annotators.get(1)); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator2", ((Map<String, Object>) annotators.get(2)).get("class")); assertEquals(6, ((Map<String, Object>) annotators.get(2)).get("example.count")); assertEquals("uk.gov.dstl.baleen.testing.DummyAnnotator4", annotators.get(3)); }
@Test public void simpleInclude() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, PARENT_YAML), mapping); String expected = getRaw(PARENT_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertTrue(dataTree.containsKey("collectionreader")); List<Object> annotators = (List<Object>) dataTree.get("annotators"); assertEquals(2, annotators.size()); assertEquals( "uk.gov.dstl.baleen.testing.DummyAnnotator", ((Map<String, Object>) annotators.get(1)).get("class")); assertEquals( "uk.gov.dstl.baleen.testing.DummyResourceAnnotator", ((Map<String, Object>) annotators.get(0)).get("class")); }
@Test public void noIncludes() throws IOException { Yaml yaml = new IncludedYaml(new YamlFile(YamlFileTest.class, YAMLCONFIGURATION_YAML), mapping); String expected = getRaw(YAMLCONFIGURATION_YAML); assertEquals(expected, yaml.original()); Object data = yaml.dataTree(); assertTrue(data instanceof Map); Map<String, Object> dataTree = (Map<String, Object>) data; assertNotNull(dataTree); assertTrue(dataTree.containsKey("example")); assertTrue(dataTree.containsKey("collectionreader")); assertTrue(dataTree.containsKey("annotators")); assertTrue(dataTree.containsKey("consumers")); assertTrue(dataTree.get("example") instanceof Map); }