Skip to content

Commit df91228

Browse files
committed
ArangoDB: UUID mapping
1 parent 65fa53d commit df91228

File tree

7 files changed

+137
-2
lines changed

7 files changed

+137
-2
lines changed

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private static JsonObject toJsonObject(Iterable<Element> elements) {
123123
if (KEY.equals(document.name()) && Objects.isNull(document.get())) {
124124
continue;
125125
}
126-
Object value = ValueUtil.convert(document.value());
126+
Object value = ValueUtil.convert(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER);
127127
builder.add(document.name(), toJsonValue(value));
128128
}
129129
return builder.build();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.arangodb.communication;
16+
17+
import org.eclipse.jnosql.communication.ValueWriter;
18+
import org.eclipse.jnosql.communication.ValueWriterDecorator;
19+
20+
import java.util.UUID;
21+
22+
final class ArangoDBValueWriteDecorator<T, S> implements ValueWriter<T, S> {
23+
24+
@SuppressWarnings("rawtypes")
25+
static final ValueWriter ARANGO_DB_VALUE_WRITER = new ArangoDBValueWriteDecorator();
26+
27+
@SuppressWarnings("rawtypes")
28+
private static final ValueWriter DEFAULT = ValueWriterDecorator.getInstance();
29+
30+
private static final UUIDValueWriter UUID_VALUE_WRITER = new UUIDValueWriter();
31+
32+
33+
@Override
34+
public boolean test(Class<?> type) {
35+
return UUID_VALUE_WRITER.test(type) || DEFAULT.test(type);
36+
}
37+
38+
@SuppressWarnings("unchecked")
39+
@Override
40+
public S write(T type) {
41+
if(type != null && UUID_VALUE_WRITER.test(type.getClass())) {
42+
return (S) UUID_VALUE_WRITER.write((UUID) type);
43+
} else {
44+
return (S) DEFAULT.write(type);
45+
}
46+
}
47+
48+
}

jnosql-arangodb/src/main/java/org/eclipse/jnosql/databases/arangodb/communication/QueryAQLConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ private static void appendCondition(StringBuilder aql, Map<String, Object> param
195195
aql.append(SEPARATOR).append(entity).append('.').append(document.name())
196196
.append(condition).append(PARAM_APPENDER).append(nameParam);
197197
if (IN.equals(condition)) {
198-
params.put(nameParam, ValueUtil.convertToList(document.value()));
198+
params.put(nameParam, ValueUtil.convertToList(document.value(), ArangoDBValueWriteDecorator.ARANGO_DB_VALUE_WRITER));
199199
} else {
200200
params.put(nameParam, document.get());
201201
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.arangodb.communication;
16+
17+
import org.eclipse.jnosql.communication.ValueWriter;
18+
19+
import java.util.Objects;
20+
import java.util.UUID;
21+
22+
public class UUIDValueWriter implements ValueWriter<UUID, String> {
23+
24+
@Override
25+
public boolean test(Class<?> type) {
26+
return UUID.class.equals(type);
27+
}
28+
29+
30+
@Override
31+
public String write(UUID uuid) {
32+
if(Objects.nonNull(uuid)) {
33+
return uuid.toString();
34+
}
35+
return null;
36+
}
37+
38+
}

jnosql-arangodb/src/test/java/org/eclipse/jnosql/databases/arangodb/communication/ArangoDBDocumentManagerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,21 @@ void shouldExposeArangoDB() {
328328
assertThat(adb.getVersion()).isNotNull();
329329
}
330330

331+
@Test
332+
void shouldInsertUUID() {
333+
var entity = getEntity();
334+
entity.add("uuid", UUID.randomUUID());
335+
var documentEntity = entityManager.insert(entity);
336+
Optional<Element> uuid = documentEntity.find("uuid");
337+
SoftAssertions.assertSoftly(soft -> {
338+
soft.assertThat(uuid).isPresent();
339+
Element element = uuid.orElseThrow();
340+
soft.assertThat(element.name()).isEqualTo("uuid");
341+
soft.assertThat(element.get(UUID.class)).isInstanceOf(UUID.class);
342+
});
343+
344+
}
345+
331346
private CommunicationEntity getEntity() {
332347
CommunicationEntity entity = CommunicationEntity.of(COLLECTION_NAME);
333348
Map<String, Object> map = new HashMap<>();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (c) 2024 Contributors to the Eclipse Foundation
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* and Apache License v2.0 which accompanies this distribution.
6+
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
7+
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
8+
*
9+
* You may elect to redistribute this code under either of these licenses.
10+
*
11+
* Contributors:
12+
*
13+
* Otavio Santana
14+
*/
15+
package org.eclipse.jnosql.databases.arangodb.communication;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.UUID;
20+
21+
import static org.junit.Assert.assertTrue;
22+
23+
@SuppressWarnings("rawtypes")
24+
class ArangoDBValueWriteDecoratorTest {
25+
26+
private final ArangoDBValueWriteDecorator<Object, String> valueWriter = new ArangoDBValueWriteDecorator<>();
27+
28+
@Test
29+
void shouldTestUUIDType() {
30+
assertTrue(valueWriter.test(UUID.class));
31+
}
32+
33+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<modelVersion>4.0.0</modelVersion>
2020

2121
<parent>
22+
<relativePath />
2223
<groupId>org.eclipse.jnosql.mapping</groupId>
2324
<artifactId>jnosql-mapping-parent</artifactId>
2425
<version>1.1.4-SNAPSHOT</version>

0 commit comments

Comments
 (0)