Search in sources :

Example 6 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class DuplicateRemovalVisitorTest method testEqualListCleared.

@Test
void testEqualListCleared() {
    final ConfigurationNode parent = BasicConfigurationNode.root(n -> {
        n.appendListNode().raw("one");
        n.appendListNode().raw("two");
        n.appendListNode().act(c -> {
            c.node("zombie").raw(false);
            c.node("villager").raw(false);
            c.node("cat").raw(true);
            c.node("ocelot").raw(true);
        });
        n.appendListNode().raw("blue");
    });
    final ConfigurationNode child = BasicConfigurationNode.root(n -> {
        n.appendListNode().raw("one");
        n.appendListNode().raw("two");
        n.appendListNode().act(c -> {
            c.node("zombie").raw(false);
            c.node("villager").raw(false);
            c.node("cat").raw(true);
            c.node("ocelot").raw(true);
        });
        n.appendListNode().raw("blue");
    });
    DuplicateRemovalVisitor.visit(child, parent);
    assertTrue(child.empty());
    assertNull(child.raw());
}
Also used : CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) Test(org.junit.jupiter.api.Test)

Example 7 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class DuplicateRemovalVisitorTest method testEmptyMapsPreservedInList.

@Test
void testEmptyMapsPreservedInList() {
    final ConfigurationNode parent = BasicConfigurationNode.root(n -> {
        n.appendListNode().raw("one");
        n.appendListNode().raw("two");
        n.appendListNode().act(c -> {
            c.node("zombie").raw(false);
            c.node("villager").raw(false);
            c.node("cat").raw(true);
            c.node("ocelot").raw(true);
        });
        n.appendListNode().raw("blue");
    });
    final ConfigurationNode child = BasicConfigurationNode.root(n -> {
        n.appendListNode().raw("one");
        n.appendListNode().raw("two");
        n.appendListNode().act(c -> {
            c.node("zombie").raw(false);
            c.node("villager").raw(false);
            c.node("cat").raw(true);
            c.node("ocelot").raw(true);
        });
        n.appendListNode().raw("green");
    });
    DuplicateRemovalVisitor.visit(child, parent);
    final ConfigurationNode expected = BasicConfigurationNode.root(n -> {
        n.appendListNode().raw("one");
        n.appendListNode().raw("two");
        n.appendListNode().raw(ImmutableMap.of());
        n.appendListNode().raw("green");
    });
    assertEquals(expected, child);
}
Also used : CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) Test(org.junit.jupiter.api.Test)

Example 8 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class TimingsExport method serializeConfigNode.

private static JsonElement serializeConfigNode(final ConfigurationNode node) {
    if (node.isMap()) {
        final JsonObject object = new JsonObject();
        for (final Entry<Object, ? extends ConfigurationNode> entry : node.childrenMap().entrySet()) {
            final String fullPath = TimingsExport.CONFIG_PATH_JOINER.join(entry.getValue().path());
            if (fullPath.equals("sponge.sql") || TimingsManager.hiddenConfigs.contains(fullPath)) {
                continue;
            }
            object.add(entry.getKey().toString(), TimingsExport.serializeConfigNode(entry.getValue()));
        }
        return object;
    }
    if (node.isList()) {
        final JsonArray array = new JsonArray();
        for (final ConfigurationNode child : node.childrenList()) {
            array.add(TimingsExport.serializeConfigNode(child));
        }
        return array;
    }
    return JSONUtil.toJsonElement(node.raw());
}
Also used : JsonArray(com.google.gson.JsonArray) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject)

Example 9 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testTypesAreCoerced.

@Test
void testTypesAreCoerced() {
    final DataContainer source = DataContainer.createNew();
    source.set(DataQuery.of("i'm a short"), (short) 5);
    final ConfigurationNode destination = BasicConfigurationNode.root(ConfigurationOptions.defaults().nativeTypes(Collections.singleton(Integer.class)));
    ConfigurateTranslator.instance().translateDataToNode(destination, source);
    assertEquals(5, destination.node("i'm a short").raw());
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Test(org.junit.jupiter.api.Test)

Example 10 with ConfigurationNode

use of org.spongepowered.configurate.ConfigurationNode in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testMapInsideList.

@Test
void testMapInsideList() throws IOException {
    final JsonDataFormat json = new JsonDataFormat();
    final ConfigurationNode node = CommentedConfigurationNode.root();
    final Map<String, String> map = Collections.singletonMap("mkey", "mvalue");
    final List<Object> list = Arrays.asList("lelement", map);
    node.node("foo").raw("bar");
    node.node("l").raw(list);
    final DataContainer jc = json.read("{\"foo\":\"bar\",\"l\":[\"lelement\",{\"mkey\":\"mvalue\"}]}");
    final DataContainer hc = ConfigurateTranslator.instance().translate(node);
    assertEquals(jc.getMap(DataQuery.of()), hc.getMap(DataQuery.of()));
}
Also used : DataContainer(org.spongepowered.api.data.persistence.DataContainer) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationNode (org.spongepowered.configurate.ConfigurationNode)24 CommentedConfigurationNode (org.spongepowered.configurate.CommentedConfigurationNode)20 Test (org.junit.jupiter.api.Test)19 BasicConfigurationNode (org.spongepowered.configurate.BasicConfigurationNode)19 DataContainer (org.spongepowered.api.data.persistence.DataContainer)7 DataView (org.spongepowered.api.data.persistence.DataView)2 ConfigurateException (org.spongepowered.configurate.ConfigurateException)2 JsonArray (com.google.gson.JsonArray)1 JsonObject (com.google.gson.JsonObject)1 IOException (java.io.IOException)1 InvalidPathException (java.nio.file.InvalidPathException)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 ResourceKey (org.spongepowered.api.ResourceKey)1 RespawnLocation (org.spongepowered.api.util.RespawnLocation)1 NodePath (org.spongepowered.configurate.NodePath)1 HoconConfigurationLoader (org.spongepowered.configurate.hocon.HoconConfigurationLoader)1