Search in sources :

Example 31 with DataView

use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testNodeToData.

@Test
void testNodeToData() {
    final ConfigurationNode node = BasicConfigurationNode.root();
    node.node("foo", "int").raw(1);
    node.node("foo", "double").raw(10.0D);
    node.node("foo", "long").raw(Long.MAX_VALUE);
    final List<String> stringList = Lists.newArrayList();
    for (int i = 0; i < 100; i++) {
        stringList.add("String" + i);
    }
    node.node("foo", "stringList").raw(stringList);
    final List<SimpleData> dataList = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        dataList.add(new SimpleData(i, 10.0 + i, "String" + i, Collections.emptyList()));
    }
    node.node("foo", "nested", "Data").raw(dataList);
    final DataContainer manual = DataContainer.createNew();
    manual.set(DataQuery.of("foo", "int"), 1).set(DataQuery.of("foo", "double"), 10.0D).set(DataQuery.of("foo", "long"), Long.MAX_VALUE).set(DataQuery.of("foo", "stringList"), stringList).set(DataQuery.of("foo", "nested", "Data"), dataList);
    final DataView container = ConfigurateTranslator.instance().translate(node);
    assertEquals(manual, container);
    ConfigurateTranslator.instance().translate(container);
// assertEquals(node, translated); // TODO Test is broken, depends on quite a bit of init
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) DataContainer(org.spongepowered.api.data.persistence.DataContainer) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) ArrayList(java.util.ArrayList) Test(org.junit.jupiter.api.Test)

Example 32 with DataView

use of org.spongepowered.api.data.persistence.DataView in project SpongeCommon by SpongePowered.

the class ConfigurateDataViewTest method testNonRootNodeToData.

@Test
void testNonRootNodeToData() {
    final ConfigurationNode root = CommentedConfigurationNode.root(n -> {
        n.node("test").act(c -> {
            c.node("child").raw("hello");
            c.node("other").raw("world");
        });
    });
    final DataView view = ConfigurateTranslator.instance().translate(root.node("test"));
    assertEquals("hello", view.getString(DataQuery.of("child")).get());
    assertEquals("world", view.getString(DataQuery.of("other")).get());
    ConfigurateTranslator.instance().translateDataToNode(root.node("test2"), view);
    assertEquals(root.node("test").raw(), root.node("test2").raw());
}
Also used : DataView(org.spongepowered.api.data.persistence.DataView) CommentedConfigurationNode(org.spongepowered.configurate.CommentedConfigurationNode) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) BasicConfigurationNode(org.spongepowered.configurate.BasicConfigurationNode) Test(org.junit.jupiter.api.Test)

Aggregations

DataView (org.spongepowered.api.data.persistence.DataView)32 DataQuery (org.spongepowered.api.data.persistence.DataQuery)16 DataContainer (org.spongepowered.api.data.persistence.DataContainer)10 CompoundTag (net.minecraft.nbt.CompoundTag)9 ResourceKey (org.spongepowered.api.ResourceKey)8 Map (java.util.Map)6 InvalidDataException (org.spongepowered.api.data.persistence.InvalidDataException)6 Optional (java.util.Optional)5 Nullable (org.checkerframework.checker.nullness.qual.Nullable)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Collection (java.util.Collection)3 DataContentUpdater (org.spongepowered.api.data.persistence.DataContentUpdater)3 DataSerializable (org.spongepowered.api.data.persistence.DataSerializable)3 DataStore (org.spongepowered.api.data.persistence.DataStore)3 DataTranslator (org.spongepowered.api.data.persistence.DataTranslator)3 Vector3i (org.spongepowered.math.vector.Vector3i)3 ImmutableList (com.google.common.collect.ImmutableList)2 TypeToken (io.leangen.geantyref.TypeToken)2