-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Refactor] Use
Conversions
API also for entities.
All data will get processed by the `Conversions` API instead of having the entity creation running in parallel. This introduces (for now?) a cyclic dependency to the `Conversions` infrastructure because the `EntityConverter` needs to be aware of the available conversions.
- Loading branch information
1 parent
4356985
commit 7ac4144
Showing
9 changed files
with
163 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
mapper/src/main/java/com/meistermeier/neo4j/toolbelt/conversion/EntityConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2022 Gerrit Meier | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.meistermeier.neo4j.toolbelt.conversion; | ||
|
||
import org.neo4j.driver.Record; | ||
import org.neo4j.driver.Value; | ||
import org.neo4j.driver.types.MapAccessor; | ||
import org.neo4j.driver.types.TypeSystem; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* Entity converter that delegates to the {@link ObjectInstantiator} | ||
* if it can convert the incoming structure. | ||
* | ||
* @author Gerrit Meier | ||
*/ | ||
public class EntityConverter implements TypeConverter<MapAccessor> { | ||
|
||
private final TypeSystem typeSystem = TypeSystem.getDefault(); | ||
private final ObjectInstantiator objectInstantiator = new ObjectInstantiator(); | ||
private final Converters converters; | ||
|
||
public EntityConverter(Converters converters) { | ||
this.converters = converters; | ||
} | ||
|
||
@Override | ||
public boolean canConvert(MapAccessor mapAccessor, Class<?> type) { | ||
if (mapAccessor instanceof Value value) { | ||
return typeSystem.NODE().isTypeOf(value) || typeSystem.MAP().isTypeOf(value); | ||
} | ||
return mapAccessor instanceof Record; | ||
} | ||
|
||
@Override | ||
public <T> T convert(MapAccessor record, Class<T> entityClass) { | ||
Map<String, Object> recordMap = record.asMap(); | ||
if (recordMap.size() == 1) { | ||
Value value = record.values().iterator().next(); | ||
if (value.type().equals(typeSystem.NODE()) || value.type().equals(typeSystem.MAP())) { | ||
return objectInstantiator.createInstance(entityClass, converters).apply(value); | ||
} | ||
} | ||
// plain property based result | ||
return objectInstantiator.createInstance(entityClass, converters).apply(record); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
mapper/src/main/java/com/meistermeier/neo4j/toolbelt/conversion/ValueConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright 2022 Gerrit Meier | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.meistermeier.neo4j.toolbelt.conversion; | ||
|
||
import org.neo4j.driver.Value; | ||
|
||
/** | ||
* Definition of a Neo4j Java driver value converter. | ||
* An implementation must provide the methods {@code canConvert} and {@code convert}. | ||
* | ||
* @author Gerrit Meier | ||
*/ | ||
public interface ValueConverter extends TypeConverter<Value> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.