-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Introduce
ConverterInfo
so that individual converters can get…
… more info about the fields to be converted. Closes #1190.
- Loading branch information
1 parent
dcdabb9
commit ce0a0ab
Showing
6 changed files
with
203 additions
and
14 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
32 changes: 32 additions & 0 deletions
32
core/src/main/java/org/neo4j/ogm/typeconversion/ConverterInfo.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,32 @@ | ||
/* | ||
* Copyright (c) 2002-2024 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* 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 org.neo4j.ogm.typeconversion; | ||
|
||
import java.lang.reflect.Field; | ||
|
||
/** | ||
* Converters registered via {@link org.neo4j.ogm.annotation.typeconversion.Convert @Convert} can require this information | ||
* via a non-default, public constructor taking in one single {@link ConverterInfo argument}. | ||
* | ||
* @param field The field on which the converter was registered | ||
* @param fieldType The field type that OGM does assume, might be different of what the raw field would give you | ||
* @param defaultPropertyName The property that would be assumed for this field by OGM | ||
*/ | ||
public record ConverterInfo(Field field, Class<?> fieldType, String defaultPropertyName) { | ||
} |
54 changes: 54 additions & 0 deletions
54
...tests/neo4j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1190/MyEntity.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,54 @@ | ||
/* | ||
* Copyright (c) 2002-2024 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* 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 org.neo4j.ogm.domain.gh1190; | ||
|
||
import java.util.UUID; | ||
|
||
import org.neo4j.ogm.annotation.GeneratedValue; | ||
import org.neo4j.ogm.annotation.Id; | ||
import org.neo4j.ogm.annotation.NodeEntity; | ||
import org.neo4j.ogm.annotation.typeconversion.Convert; | ||
import org.neo4j.ogm.id.UuidStrategy; | ||
|
||
@NodeEntity | ||
public class MyEntity { | ||
@Id | ||
@GeneratedValue(strategy = UuidStrategy.class) | ||
@Convert(Uuid2LongConverter.class) | ||
private UUID uuid; | ||
|
||
@Convert(Uuid2LongConverter.class) | ||
private UUID otherUuid; | ||
|
||
public UUID getOtherUuid() { | ||
return otherUuid; | ||
} | ||
|
||
public void setOtherUuid(UUID otherUuid) { | ||
this.otherUuid = otherUuid; | ||
} | ||
|
||
public UUID getUuid() { | ||
return uuid; | ||
} | ||
|
||
public void setUuid(UUID uuid) { | ||
this.uuid = uuid; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...j-ogm-integration-tests/src/test/java/org/neo4j/ogm/domain/gh1190/Uuid2LongConverter.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 (c) 2002-2024 "Neo4j," | ||
* Neo4j Sweden AB [http://neo4j.com] | ||
* | ||
* This file is part of Neo4j. | ||
* | ||
* 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 org.neo4j.ogm.domain.gh1190; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
|
||
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter; | ||
import org.neo4j.ogm.typeconversion.ConverterInfo; | ||
|
||
public class Uuid2LongConverter implements CompositeAttributeConverter<UUID> { | ||
|
||
private final ConverterInfo converterInfo; | ||
private final String fieldName; | ||
|
||
public Uuid2LongConverter(ConverterInfo converterInfo) { | ||
this.converterInfo = converterInfo; | ||
this.fieldName = converterInfo.field().getName(); | ||
} | ||
|
||
@Override | ||
public Map<String, ?> toGraphProperties(UUID value) { | ||
|
||
var properties = new HashMap<String, Long>(); | ||
if (value != null) { | ||
properties.put(fieldName + "_most", value.getMostSignificantBits()); | ||
properties.put(fieldName + "_least", value.getLeastSignificantBits()); | ||
} | ||
|
||
return properties; | ||
} | ||
|
||
@Override | ||
public UUID toEntityAttribute(Map<String, ?> value) { | ||
var most = (Long) value.get(fieldName + "_most"); | ||
var least = (Long) value.get(fieldName + "_least"); | ||
|
||
if (most != null && least != null) { | ||
return new UUID(most, least); | ||
} | ||
|
||
return null; | ||
} | ||
} |
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