Skip to content

Commit 99f2bf4

Browse files
authored
Merge pull request #284 from eclipse/add-uuid-mongodb
Add UUID mongodb
2 parents f25a650 + bc053ed commit 99f2bf4

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

CHANGELOG.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ and this project adheres to https://semver.org/spec/v2.0.0.html[Semantic Version
1212

1313
- Include between query support at MongoDB
1414
- Include Graph as Apache TinkerPop
15+
- Include UUID support to MongoDB
1516

1617
=== Changed
1718

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.mongodb.communication;
16+
17+
import org.eclipse.jnosql.communication.ValueReader;
18+
19+
import java.util.UUID;
20+
21+
public class UUIDValueReader implements ValueReader {
22+
23+
@Override
24+
public boolean test(Class<?> type) {
25+
return UUID.class.equals(type);
26+
}
27+
28+
29+
@SuppressWarnings("unchecked")
30+
@Override
31+
public <T> T read(Class<T> type, Object value) {
32+
if (value instanceof UUID) {
33+
return (T) value;
34+
}
35+
return null;
36+
}
37+
}
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.mongodb.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+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader
1+
org.eclipse.jnosql.databases.mongodb.communication.BinaryValueReader
2+
org.eclipse.jnosql.databases.mongodb.communication.UUIDValueReader
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.jnosql.databases.mongodb.communication.UUIDValueWriter

jnosql-mongodb/src/test/java/org/eclipse/jnosql/databases/mongodb/communication/MongoDBDocumentManagerTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,21 @@ void shouldUpdateNull(){
607607
});
608608
}
609609

610+
@Test
611+
void shouldInsertUUID() {
612+
var entity = getEntity();
613+
entity.add("uuid", UUID.randomUUID());
614+
var documentEntity = entityManager.insert(entity);
615+
Optional<Element> uuid = documentEntity.find("uuid");
616+
SoftAssertions.assertSoftly(soft -> {
617+
soft.assertThat(uuid).isPresent();
618+
Element element = uuid.orElseThrow();
619+
soft.assertThat(element.name()).isEqualTo("uuid");
620+
soft.assertThat(element.get(UUID.class)).isInstanceOf(UUID.class);
621+
});
622+
623+
}
624+
610625

611626
private CommunicationEntity createDocumentList() {
612627
CommunicationEntity entity = CommunicationEntity.of("AppointmentBook");

0 commit comments

Comments
 (0)