Skip to content

Commit f0978e4

Browse files
aburmeisrashtao
andauthored
Feature: allow embedded entities instead of resolvers (#270)
* refactor: expose specific owner for persistent properties * [#269] allow return merged entities to be mapped directly * revert change of deprecated usage * remove unused import * test enhancements * test fix --------- Co-authored-by: Michele Rastelli <[email protected]>
1 parent f430011 commit f0978e4

File tree

6 files changed

+291
-175
lines changed

6 files changed

+291
-175
lines changed

src/main/java/com/arangodb/springframework/core/convert/DefaultArangoConverter.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -336,9 +336,13 @@ private Optional<Object> readReference(
336336
final ArangoPersistentProperty property,
337337
final Annotation annotation) {
338338

339+
if (source.isNone() || source.isNull()) {
340+
return Optional.empty();
341+
}
342+
339343
final Optional<ReferenceResolver<Annotation>> resolver = resolverFactory.getReferenceResolver(annotation);
340344

341-
if (!resolver.isPresent() || source.isNone()) {
345+
if (!resolver.isPresent()) {
342346
return Optional.empty();
343347
}
344348

@@ -353,6 +357,11 @@ else if (property.isCollectionLike()) {
353357
return resolver.map(res -> res.resolveMultiple(ids, property.getTypeInformation(), annotation));
354358
}
355359

360+
else if (source.isObject()) {
361+
// source contains target
362+
return Optional.of(readInternal(property.getTypeInformation(), source));
363+
}
364+
356365
else {
357366
if (!source.isString()) {
358367
throw new MappingException(
@@ -370,6 +379,10 @@ private <A extends Annotation> Optional<Object> readRelation(
370379
final ArangoPersistentProperty property,
371380
final A annotation) {
372381

382+
if (source.isNull()) {
383+
return Optional.empty();
384+
}
385+
373386
final Class<? extends Annotation> collectionType = entity.findAnnotation(Edge.class) != null ? Edge.class
374387
: Document.class;
375388
final Optional<RelationResolver<Annotation>> resolver = resolverFactory.getRelationResolver(annotation,
@@ -383,16 +396,26 @@ private <A extends Annotation> Optional<Object> readRelation(
383396
}
384397

385398
else if (property.isCollectionLike()) {
399+
if (source.isArray()) {
400+
// source contains target array
401+
return Optional.of(readInternal(property.getTypeInformation(), source));
402+
}
386403
if (parentId == null) {
387404
return Optional.empty();
388405
}
389406
return resolver.map(res -> res.resolveMultiple(parentId, property.getTypeInformation(), traversedTypes, annotation));
390407
}
391408

392409
else if (source.isString()) {
410+
// resolve from/to using target id
393411
return resolver.map(res -> res.resolveOne(source.getAsString(), property.getTypeInformation(), traversedTypes, annotation));
394412
}
395413

414+
else if (source.isObject()) {
415+
// source contains target
416+
return Optional.of(readInternal(property.getTypeInformation(), source));
417+
}
418+
396419
else {
397420
return resolver.map(res -> res.resolveOne(parentId, property.getTypeInformation(), traversedTypes, annotation));
398421
}

src/main/java/com/arangodb/springframework/core/mapping/ArangoPersistentProperty.java

+4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.Optional;
2424

25+
import org.springframework.data.mapping.PersistentEntity;
2526
import org.springframework.data.mapping.PersistentProperty;
2627

2728
import com.arangodb.springframework.annotation.From;
@@ -43,6 +44,9 @@ public interface ArangoPersistentProperty extends PersistentProperty<ArangoPersi
4344

4445
String getFieldName();
4546

47+
@Override
48+
ArangoPersistentEntity<?> getOwner();
49+
4650
boolean isArangoIdProperty();
4751

4852
boolean isRevProperty();

src/main/java/com/arangodb/springframework/core/mapping/DefaultArangoPersistentProperty.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.util.Optional;
2424

25+
import com.arangodb.entity.CollectionType;
2526
import org.springframework.data.mapping.Association;
2627
import org.springframework.data.mapping.PersistentEntity;
2728
import org.springframework.data.mapping.model.AnnotationBasedPersistentProperty;
@@ -55,13 +56,18 @@ public class DefaultArangoPersistentProperty extends AnnotationBasedPersistentPr
5556
private final FieldNamingStrategy fieldNamingStrategy;
5657

5758
public DefaultArangoPersistentProperty(final Property property,
58-
final PersistentEntity<?, ArangoPersistentProperty> owner, final SimpleTypeHolder simpleTypeHolder,
59+
final ArangoPersistentEntity<?> owner, final SimpleTypeHolder simpleTypeHolder,
5960
final FieldNamingStrategy fieldNamingStrategy) {
6061
super(property, owner, simpleTypeHolder);
6162
this.fieldNamingStrategy = fieldNamingStrategy != null ? fieldNamingStrategy
6263
: PropertyNameFieldNamingStrategy.INSTANCE;
6364
}
6465

66+
@Override
67+
public ArangoPersistentEntity<?> getOwner() {
68+
return (ArangoPersistentEntity<?>) super.getOwner();
69+
}
70+
6571
@Override
6672
protected Association<ArangoPersistentProperty> createAssociation() {
6773
return new Association<>(this, null);
@@ -106,9 +112,9 @@ public String getFieldName() {
106112
fieldName = "_key";
107113
} else if (isRevProperty()) {
108114
fieldName = "_rev";
109-
} else if (getFrom().isPresent()) {
115+
} else if (getFrom().isPresent() && getOwner().getCollectionOptions().getType() == CollectionType.EDGES) {
110116
fieldName = "_from";
111-
} else if (getTo().isPresent()) {
117+
} else if (getTo().isPresent() && getOwner().getCollectionOptions().getType() == CollectionType.EDGES) {
112118
fieldName = "_to";
113119
} else {
114120
fieldName = getAnnotatedFieldName().orElse(fieldNamingStrategy.getFieldName(this));

0 commit comments

Comments
 (0)