private <T> void setValueAndComment(ConfigurationNode node, TypeToken<T> type, T value, String comment) throws ObjectMappingException { node.setValue(type, value); if (node instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode) node).setComment(comment); } } }
private void setValueAndComment(ConfigurationNode node, Object value, String comment) { node.setValue(value); if (node instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode) node).setComment(comment); } } private <T> void setValueAndComment(ConfigurationNode node, TypeToken<T> type, T value, String comment)
public static void createDefaults(CommentedConfigurationNode config) { for (Field field : VoxelSniperConfiguration.class.getDeclaredFields()) { if (field.isAnnotationPresent(ConfigValue.class)) { ConfigValue anno = field.getAnnotation(ConfigValue.class); CommentedConfigurationNode node = config.getNode((Object[]) anno.name().split("\\.")); node.setComment(anno.comment()); try { node.setValue(field.get(null)); VoxelSniper.getLogger().debug("Created " + anno.name() + " = " + node.getValue()); } catch (IllegalArgumentException | IllegalAccessException e) { VoxelSniper.getLogger().error("Failed to extract default value for configuration field " + field.toGenericString()); e.printStackTrace(); } } } }
private T getValueInternal(ConfigurationNode configurationNode) { ConfigurationNode node = configurationNode.getNode(key); if(node.isVirtual()) { this.modified = true; } if(comment != null && node instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode)node).setComment(comment); } try { if(typeToken != null) return node.getValue(typeToken, defaultValue); else return node.getValue(new TypeToken<T>(defaultValue.getClass()){}, defaultValue); } catch(Exception e) { return defaultValue; } }
@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 void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
@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 void setValueInternal(ConfigurationNode configurationNode) { ConfigurationNode node = configurationNode.getNode(key); if(comment != null && node instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode)node).setComment(comment); } if (this.modified) { if (typeToken != null) { try { node.setValue(typeToken, value); } catch (ObjectMappingException e) { CraftBookAPI.inst().getLogger().error("Failed to map value!", e); } } else { node.setValue(value); } this.modified = false; } }
private static void readConfigValue(ConfigValue value, CommentedConfigurationNode node) { if (!value.origin().comments().isEmpty()) { node.setComment(CRLF_MATCH.matcher(Joiner.on('\n').join(value.origin().comments())).replaceAll("")); } switch (value.valueType()) { case OBJECT: if (((ConfigObject) value).isEmpty()) { node.setValue(ImmutableMap.of()); } else { for (Map.Entry<String, ConfigValue> ent : ((ConfigObject) value).entrySet()) { readConfigValue(ent.getValue(), node.getNode(ent.getKey())); } } break; case LIST: List<ConfigValue> values = (ConfigList) value; for (int i = 0; i < values.size(); ++i) { readConfigValue(values.get(i), node.getNode(i)); } break; case NULL: return; default: node.setValue(value.unwrapped()); } }
node.getNode("enabled").setComment( "bStats collects some data for plugin authors like how many servers are using their plugins.\n" + "To honor their work, you should not disable it.\n" +
n.setComment(anno.comment()); try { n.setValue(field.get(null));
/** * Loads the configuration */ private void loadConfiguration() { configurationFile = getConfigFile(); configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build(); try { if (!configurationFile.exists()) { configurationFile.createNewFile(); config = configurationLoader.load(); config.setComment("This contains settings for MCStats: http://mcstats.org"); config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString()); config.getNode("mcstats.opt-out").setValue(false); config.getNode("mcstats.debug").setValue(false); configurationLoader.save(config); } else { config = configurationLoader.load(); } guid = config.getNode("mcstats.guid").getString(); debug = config.getNode("mcstats.debug").getBoolean(); } catch (final IOException e) { e.printStackTrace(); } }
@Test public void testBooleansNotShared(@TempDirectory.TempDir Path tempDir) throws IOException { URL url = getClass().getResource("/comments-test.conf"); final Path saveTo = tempDir.resolve("text4.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo).setURL(url).build(); CommentedConfigurationNode node = loader.createEmptyNode(ConfigurationOptions.defaults()); node.getNode("test", "third").setValue(false).setComment("really?"); node.getNode("test", "apple").setComment("fruit").setValue(false); node.getNode("test", "donut").setValue(true).setComment("tasty"); node.getNode("test", "guacamole").setValue(true).setComment("and chips?"); loader.save(node); assertEquals(Resources.readLines(url, UTF_8), Files.readAllLines(saveTo, UTF_8)); }
node.getNode("enabled").setComment( "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" + "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" + "but look there for further control"); node.getNode("serverUuid").setComment( "bStats collects some data for plugin authors like how many servers are using their plugins.\n" + "To control whether this is enabled or disabled, see the Sponge configuration file.\n" + node.getNode("enabled").setComment( "Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" + "Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" + "but look there for further control"); node.getNode("serverUuid").setComment( "bStats collects some data for plugin authors like how many servers are using their plugins.\n" + "To control whether this is enabled or disabled, see the Sponge configuration file.\n" +
@Test public void testHeaderSaved(@TempDirectory.TempDir Path tempDir) throws IOException { final Path saveTo = tempDir.resolve("text3.txt"); HoconConfigurationLoader loader = HoconConfigurationLoader.builder() .setPath(saveTo) .build(); CommentedConfigurationNode node = loader.createEmptyNode(ConfigurationOptions.defaults().setHeader("Hi! I am a header!\n" + "Look at meeeeeee!!!")); node.getNode("node").setComment("I have a comment").getNode("party").setValue("now"); loader.save(node); assertEquals(Resources.readLines(getClass().getResource("/header.conf"), UTF_8), Files.readAllLines(saveTo, 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)); } }
obj.getFilter().writeToConfig(filterNode); if (filterNode instanceof CommentedConfigurationNode) { ((CommentedConfigurationNode) filterNode).setComment( "Filters are used to only send certain events to certain endpoints\n" + "Use the 'name' property to select a filter and pass additional options in the 'config' property" ((CommentedConfigurationNode) value.getNode("permissions")).setComment( "Permissions node same as the ones in the permissions.conf file,\n" + "use to configure which data is sent to this node");
@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)); }