From 03023ad52c15f83dc9127469660cd1d46847e111 Mon Sep 17 00:00:00 2001 From: jdrueckert Date: Tue, 23 Aug 2022 10:27:49 +0200 Subject: [PATCH] feat: add type handlers for SimpleUri and BlockUri (#5061) --- .../BlockUriTypeHandlerTest.java | 50 +++++++++++++++++++ .../SimpleUriTypeHandlerTest.java | 34 +++++++++++++ .../typeHandling/TypeHandlerLibraryImpl.java | 6 +++ .../extensionTypes/BlockUriTypeHandler.java | 34 +++++++++++++ .../extensionTypes/SimpleUriTypeHandler.java | 34 +++++++++++++ 5 files changed, 158 insertions(+) create mode 100644 engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandlerTest.java create mode 100644 engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandlerTest.java create mode 100644 engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandler.java create mode 100644 engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandler.java diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandlerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandlerTest.java new file mode 100644 index 00000000000..ee0dd82c5f5 --- /dev/null +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandlerTest.java @@ -0,0 +1,50 @@ +// Copyright 2022 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.persistence.typeHandling.extensionTypes; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.terasology.engine.world.block.BlockUri; +import org.terasology.engine.world.block.BlockUriParseException; +import org.terasology.gestalt.assets.ResourceUrn; +import org.terasology.gestalt.naming.Name; +import org.terasology.persistence.typeHandling.PersistedData; +import org.terasology.persistence.typeHandling.inMemory.PersistedString; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class BlockUriTypeHandlerTest { + + private final BlockUriTypeHandler handler = new BlockUriTypeHandler(); + + @Test + public void testDeserializeValidShortUri() { + PersistedData data = new PersistedString("foo:bar"); + Optional uri = handler.deserialize(data); + assertTrue(uri.isPresent()); + assertEquals(new BlockUri(new ResourceUrn("foo", "bar")), uri.get()); + } + + @Test + public void testDeserializeValidFullUri() { + PersistedData data = new PersistedString("package:family:shapePackage:shapeName.identifier"); + Optional uri = handler.deserialize(data); + assertTrue(uri.isPresent()); + assertEquals(new BlockUri( + new ResourceUrn("package", "family"), new ResourceUrn("shapePackage", "shapeName"), new Name("identifier")), uri.get()); + } + + @Test + public void testDeserializeInvalidUri() { + PersistedData data = new PersistedString("baz"); + BlockUriParseException thrown = Assertions.assertThrows(BlockUriParseException.class, () -> { + handler.deserialize(data); + }); + + Assertions.assertEquals("Could not parse block uri: 'baz'", thrown.getMessage()); + } +} diff --git a/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandlerTest.java b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandlerTest.java new file mode 100644 index 00000000000..642422c4fcf --- /dev/null +++ b/engine-tests/src/test/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandlerTest.java @@ -0,0 +1,34 @@ +// Copyright 2022 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.persistence.typeHandling.extensionTypes; + +import org.junit.jupiter.api.Test; +import org.terasology.engine.core.SimpleUri; +import org.terasology.persistence.typeHandling.PersistedData; +import org.terasology.persistence.typeHandling.inMemory.PersistedString; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class SimpleUriTypeHandlerTest { + + private final SimpleUriTypeHandler handler = new SimpleUriTypeHandler(); + + @Test + public void testDeserializeValidUri() { + PersistedData data = new PersistedString("foo:bar"); + Optional uri = handler.deserialize(data); + assertTrue(uri.isPresent()); + assertEquals(new SimpleUri("foo", "bar"), uri.get()); + } + + @Test + public void testDeserializeInvalidUri() { + PersistedData data = new PersistedString("baz"); + Optional uri = handler.deserialize(data); + assertTrue(uri.isEmpty()); + } +} diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java index 1969f0361b3..d8b0d0d0f9c 100644 --- a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/TypeHandlerLibraryImpl.java @@ -18,12 +18,15 @@ import org.joml.Vector4i; import org.joml.Vector4ic; import org.reflections.Reflections; +import org.terasology.engine.core.SimpleUri; import org.terasology.engine.core.module.ModuleManager; import org.terasology.engine.math.IntegerRange; +import org.terasology.engine.persistence.typeHandling.extensionTypes.BlockUriTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.ChunkMeshTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.ColorTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.ColorcTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.NameTypeHandler; +import org.terasology.engine.persistence.typeHandling.extensionTypes.SimpleUriTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.TextureRegionTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.UITextureRegionTypeHandler; import org.terasology.engine.persistence.typeHandling.extensionTypes.factories.AssetTypeHandlerFactory; @@ -57,6 +60,7 @@ import org.terasology.engine.world.block.BlockArea; import org.terasology.engine.world.block.BlockAreac; import org.terasology.engine.world.block.BlockRegion; +import org.terasology.engine.world.block.BlockUri; import org.terasology.gestalt.naming.Name; import org.terasology.joml.geom.AABBf; import org.terasology.joml.geom.AABBi; @@ -113,6 +117,8 @@ private static void populateWithDefaultHandlers(TypeHandlerLibrary serialization serializationLibrary.addTypeHandlerFactory(new AssetTypeHandlerFactory()); serializationLibrary.addTypeHandler(Name.class, new NameTypeHandler()); + serializationLibrary.addTypeHandler(SimpleUri.class, new SimpleUriTypeHandler()); + serializationLibrary.addTypeHandler(BlockUri.class, new BlockUriTypeHandler()); serializationLibrary.addTypeHandler(TextureRegion.class, new TextureRegionTypeHandler()); serializationLibrary.addTypeHandler(UITextureRegion.class, new UITextureRegionTypeHandler()); serializationLibrary.addTypeHandler(ChunkMesh.class, new ChunkMeshTypeHandler()); diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandler.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandler.java new file mode 100644 index 00000000000..815596a9a0a --- /dev/null +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/BlockUriTypeHandler.java @@ -0,0 +1,34 @@ +// Copyright 2022 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.persistence.typeHandling.extensionTypes; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.terasology.engine.world.block.BlockUri; +import org.terasology.persistence.typeHandling.StringRepresentationTypeHandler; + +public class BlockUriTypeHandler extends StringRepresentationTypeHandler { + + private static final Logger logger = LoggerFactory.getLogger(BlockUriTypeHandler.class); + + @Override + public String getAsString(BlockUri uri) { + if (uri == null) { + return ""; + } + return uri.toString(); + } + + @Override + public BlockUri getFromString(String representation) { + BlockUri uri = new BlockUri(representation); + if (!uri.isValid()) { + logger.error("Failed to create valid BlockUri from string '{}'", representation); + // StringRepresentationTypeHandler will turn this 'null' value into an empty Optional + return null; + } + + return uri; + } +} diff --git a/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandler.java b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandler.java new file mode 100644 index 00000000000..db7619e6d6b --- /dev/null +++ b/engine/src/main/java/org/terasology/engine/persistence/typeHandling/extensionTypes/SimpleUriTypeHandler.java @@ -0,0 +1,34 @@ +// Copyright 2022 The Terasology Foundation +// SPDX-License-Identifier: Apache-2.0 + +package org.terasology.engine.persistence.typeHandling.extensionTypes; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.terasology.engine.core.SimpleUri; +import org.terasology.persistence.typeHandling.StringRepresentationTypeHandler; + +public class SimpleUriTypeHandler extends StringRepresentationTypeHandler { + + private static final Logger logger = LoggerFactory.getLogger(SimpleUriTypeHandler.class); + + @Override + public String getAsString(SimpleUri uri) { + if (uri == null) { + return ""; + } + return uri.toString(); + } + + @Override + public SimpleUri getFromString(String representation) { + SimpleUri uri = new SimpleUri(representation); + if (!uri.isValid()) { + logger.error("Failed to create valid SimpleURI from string '{}'", representation); + // StringRepresentationTypeHandler will turn this 'null' value into an empty Optional + return null; + } + + return uri; + } +}