@Override public SimpleCommentedConfigurationNode setValue(Object value) { if (value instanceof CommentedConfigurationNode && ((CommentedConfigurationNode) value).getComment().isPresent()) { setComment(((CommentedConfigurationNode) value).getComment().get()); } return (SimpleCommentedConfigurationNode)super.setValue(value); }
@Override public SimpleCommentedConfigurationNode mergeValuesFrom(ConfigurationNode other) { if (other instanceof CommentedConfigurationNode) { Optional<String> otherComment = ((CommentedConfigurationNode) other).getComment(); if (otherComment.isPresent()) { comment.compareAndSet(null, otherComment.get()); } } return (SimpleCommentedConfigurationNode) super.mergeValuesFrom(other); }
@NonNull @Override public SimpleCommentedConfigurationNode setValue(@Nullable Object value) { if (value instanceof CommentedConfigurationNode && ((CommentedConfigurationNode) value).getComment().isPresent()) { setComment(((CommentedConfigurationNode) value).getComment().get()); } return (SimpleCommentedConfigurationNode) super.setValue(value); }
@NonNull @Override public SimpleCommentedConfigurationNode setValue(@Nullable Object value) { if (value instanceof CommentedConfigurationNode && ((CommentedConfigurationNode) value).getComment().isPresent()) { setComment(((CommentedConfigurationNode) value).getComment().get()); } return (SimpleCommentedConfigurationNode) super.setValue(value); }
@NonNull @Override public SimpleCommentedConfigurationNode mergeValuesFrom(@NonNull ConfigurationNode other) { if (other instanceof CommentedConfigurationNode) { Optional<String> otherComment = ((CommentedConfigurationNode) other).getComment(); if (otherComment.isPresent()) { comment.compareAndSet(null, otherComment.get()); } } return (SimpleCommentedConfigurationNode) super.mergeValuesFrom(other); }
@NonNull @Override public SimpleCommentedConfigurationNode mergeValuesFrom(@NonNull ConfigurationNode other) { if (other instanceof CommentedConfigurationNode) { Optional<String> otherComment = ((CommentedConfigurationNode) other).getComment(); if (comment == null && otherComment.isPresent()) { comment = otherComment.get(); } } return (SimpleCommentedConfigurationNode) super.mergeValuesFrom(other); }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
private static ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
private ConfigValue fromValue(ConfigurationNode node) { ConfigValue ret; if (node.hasMapChildren()) { Map<String, ConfigValue> children = node.getOptions().getMapFactory().create(); for (Map.Entry<Object, ? extends ConfigurationNode> ent : node.getChildrenMap().entrySet()) { children.put(String.valueOf(ent.getKey()), fromValue(ent.getValue())); } ret = newConfigObject(children); } else if (node.hasListChildren()) { List<ConfigValue> children = new ArrayList<>(); for (ConfigurationNode ent : node.getChildrenList()) { children.add(fromValue(ent)); } ret = newConfigList(children); } else { ret = ConfigValueFactory.fromAnyRef(node.getValue(), "configurate-hocon"); } if (node instanceof CommentedConfigurationNode) { CommentedConfigurationNode commentedNode = ((CommentedConfigurationNode) node); final ConfigValue finalRet = ret; ret = commentedNode.getComment().map(comment -> finalRet.withOrigin(finalRet.origin().withComments(LINE_SPLITTER.splitToList(comment)))).orElse(ret); } return ret; }
@Test public void testCommentsApplied() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); ObjectMapper<CommentedObject>.BoundInstance mapper = ObjectMapper.forClass(CommentedObject.class).bindToNew(); CommentedObject obj = mapper.populate(node); obj.color = "fuchsia"; obj.politician = "All of them"; mapper.serialize(node); assertEquals("You look nice today", node.getNode("commented-key").getComment().orElse(null)); assertEquals("fuchsia", node.getNode("commented-key").getString()); assertFalse(node.getNode("no-comment").getComment().isPresent()); }
@Test public void testNestedObjectWithComments() throws ObjectMappingException { CommentedConfigurationNode node = SimpleCommentedConfigurationNode.root(); final ObjectMapper<ParentObject>.BoundInstance mapper = ObjectMapper.forObject(new ParentObject()); mapper.populate(node); assertEquals("Comment on parent", node.getNode("inner").getComment().get()); assertTrue(node.getNode("inner").hasMapChildren()); assertEquals("Default value", node.getNode("inner", "test").getString()); assertEquals("Something", node.getNode("inner", "test").getComment().get()); }
@Test public void testSimpleLoading(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/example.conf"); final Path saveTest = tempDir.resolve("text1.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setSource(() -> new BufferedReader(new InputStreamReader(url.openStream(), UTF_8))) .setSink(AtomicFiles.createAtomicWriterFactory(saveTest, UTF_8)).build(); CommentedConfigurationNode node = loader.load(); assertEquals("unicorn", node.getNode("test", "op-level").getValue()); assertEquals("dragon", node.getNode("other", "op-level").getValue()); CommentedConfigurationNode testNode = node.getNode("test"); assertEquals(" Test node", testNode.getComment().orElse(null)); assertEquals("dog park", node.getNode("other", "location").getValue()); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/roundtrip-test.conf"), UTF_8), Files .readAllLines(saveTest, UTF_8)); }
@Test public void testCommentsMerged() { CommentedConfigurationNode source = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode target = SimpleCommentedConfigurationNode.root(); source.getNode("no-value").setValue("a").setComment("yeah"); source.getNode("existing-value-no-comment").setValue("orig").setComment("maybe"); source.getNode("existing-value").setValue("a").setComment("yeah"); source.getNode("no-parent", "child").setValue("x").setComment("always"); target.getNode("existing-value-no-comment").setValue("new"); target.getNode("existing-value").setValue("b").setComment("nope"); target.mergeValuesFrom(source); assertEquals("yeah", target.getNode("no-value").getComment().orElse(null)); assertEquals("maybe", target.getNode("existing-value-no-comment").getComment().orElse(null)); assertEquals("new", target.getNode("existing-value-no-comment").getString()); assertEquals("nope", target.getNode("existing-value").getComment().orElse(null)); assertEquals("always", target.getNode("no-parent", "child").getComment().orElse(null)); } }
assertEquals("Changed value", node.getNode("inner").getNode("test").getString()); assertEquals("Changed value", node.getNode("list").getChildrenList().get(0).getNode("test").getString()); assertEquals("Something", node.getNode("inner").getNode("test").getComment().get()); assertEquals("Something", node.getNode("list").getChildrenList().get(0).getNode("test").getComment().get()); assertEquals(ChildObject.class.getName(), node.getNode("inner").getNode("__class__").getString()); assertEquals(ChildObject.class.getName(), node.getNode("list").getChildrenList().get(0).getNode("__class__").getString());
@Test public void testCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); firstChild.setValue("test value"); firstChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getValue()); assertEquals("Such comment. Very wow.", secondChild.getComment().orElse(null)); }
@Test public void testNestedCommentsTransferred() { CommentedConfigurationNode subject = SimpleCommentedConfigurationNode.root(); CommentedConfigurationNode firstChild = subject.getNode("first"); CommentedConfigurationNode firstChildChild = firstChild.getNode("child"); firstChildChild.setValue("test value"); firstChildChild.setComment("Such comment. Very wow."); CommentedConfigurationNode secondChild = subject.getNode("second"); secondChild.setValue("test value's evil twin"); assertFalse(secondChild.isVirtual()); secondChild.setValue(firstChild); assertEquals("test value", secondChild.getNode("child").getValue()); assertEquals("Such comment. Very wow.", secondChild.getNode("child").getComment().orElse(null)); }