Skip to content

Commit

Permalink
feat: add type handlers for SimpleUri and BlockUri (#5061)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdrueckert authored Aug 23, 2022
1 parent f3f9d92 commit 03023ad
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<BlockUri> 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<BlockUri> 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());
}
}
Original file line number Diff line number Diff line change
@@ -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<SimpleUri> uri = handler.deserialize(data);
assertTrue(uri.isPresent());
assertEquals(new SimpleUri("foo", "bar"), uri.get());
}

@Test
public void testDeserializeInvalidUri() {
PersistedData data = new PersistedString("baz");
Optional<SimpleUri> uri = handler.deserialize(data);
assertTrue(uri.isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BlockUri> {

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;
}
}
Original file line number Diff line number Diff line change
@@ -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<SimpleUri> {

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;
}
}

0 comments on commit 03023ad

Please sign in to comment.