Skip to content

Commit

Permalink
ArangoDB: UUID mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rashtao committed Dec 17, 2024
1 parent 65fa53d commit df91228
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private static JsonObject toJsonObject(Iterable<Element> elements) {
if (KEY.equals(document.name()) && Objects.isNull(document.get())) {
continue;
}
Object value = ValueUtil.convert(document.value());
Object value = ValueUtil.convert(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER);
builder.add(document.name(), toJsonValue(value));
}
return builder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.arangodb.communication;

import org.eclipse.jnosql.communication.ValueWriter;
import org.eclipse.jnosql.communication.ValueWriterDecorator;

import java.util.UUID;

final class ArangoDBValueWriteDecorator<T, S> implements ValueWriter<T, S> {

@SuppressWarnings("rawtypes")
static final ValueWriter ARANGO_DB_VALUE_WRITER = new ArangoDBValueWriteDecorator();

@SuppressWarnings("rawtypes")
private static final ValueWriter DEFAULT = ValueWriterDecorator.getInstance();

private static final UUIDValueWriter UUID_VALUE_WRITER = new UUIDValueWriter();


@Override
public boolean test(Class<?> type) {
return UUID_VALUE_WRITER.test(type) || DEFAULT.test(type);
}

@SuppressWarnings("unchecked")
@Override
public S write(T type) {
if(type != null && UUID_VALUE_WRITER.test(type.getClass())) {
return (S) UUID_VALUE_WRITER.write((UUID) type);
} else {
return (S) DEFAULT.write(type);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static void appendCondition(StringBuilder aql, Map<String, Object> param
aql.append(SEPARATOR).append(entity).append('.').append(document.name())
.append(condition).append(PARAM_APPENDER).append(nameParam);
if (IN.equals(condition)) {
params.put(nameParam, ValueUtil.convertToList(document.value()));
params.put(nameParam, ValueUtil.convertToList(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER));
} else {
params.put(nameParam, document.get());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.arangodb.communication;

import org.eclipse.jnosql.communication.ValueWriter;

import java.util.Objects;
import java.util.UUID;

public class UUIDValueWriter implements ValueWriter<UUID, String> {

@Override
public boolean test(Class<?> type) {
return UUID.class.equals(type);
}


@Override
public String write(UUID uuid) {
if(Objects.nonNull(uuid)) {
return uuid.toString();
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,21 @@ void shouldExposeArangoDB() {
assertThat(adb.getVersion()).isNotNull();
}

@Test
void shouldInsertUUID() {
var entity = getEntity();
entity.add("uuid", UUID.randomUUID());
var documentEntity = entityManager.insert(entity);
Optional<Element> uuid = documentEntity.find("uuid");
SoftAssertions.assertSoftly(soft -> {
soft.assertThat(uuid).isPresent();
Element element = uuid.orElseThrow();
soft.assertThat(element.name()).isEqualTo("uuid");
soft.assertThat(element.get(UUID.class)).isInstanceOf(UUID.class);
});

}

private CommunicationEntity getEntity() {
CommunicationEntity entity = CommunicationEntity.of(COLLECTION_NAME);
Map<String, Object> map = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2024 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*/
package org.eclipse.jnosql.databases.arangodb.communication;

import org.junit.jupiter.api.Test;

import java.util.UUID;

import static org.junit.Assert.assertTrue;

@SuppressWarnings("rawtypes")
class ArangoDBValueWriteDecoratorTest {

private final ArangoDBValueWriteDecorator<Object, String> valueWriter = new ArangoDBValueWriteDecorator<>();

@Test
void shouldTestUUIDType() {
assertTrue(valueWriter.test(UUID.class));
}

}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<modelVersion>4.0.0</modelVersion>

<parent>
<relativePath />
<groupId>org.eclipse.jnosql.mapping</groupId>
<artifactId>jnosql-mapping-parent</artifactId>
<version>1.1.4-SNAPSHOT</version>
Expand Down

0 comments on commit df91228

Please sign in to comment.