Skip to content

Commit

Permalink
GH-157 - Provide a method to turn a Java map into an expression.
Browse files Browse the repository at this point in the history
This closes #157.
  • Loading branch information
michael-simons authored Mar 5, 2021
1 parent 0250694 commit 0a39867
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Map;
import java.util.regex.Pattern;

import org.assertj.core.api.Assertions;
Expand Down Expand Up @@ -102,4 +103,16 @@ void nestedProperties() {
.isEqualTo("MATCH (p:`Person`) WHERE p.`home.location`.y > 50 RETURN p.`home.location`.y");
// end::nested-properties[]
}

@Test
void usingExistingJavaMaps() {

var node = Cypher
.node("ANode")
.named("n")
.withProperties(Map.of("aProperty", 23));

assertThat(Cypher.match(node).returning(node).build().getCypher())
.isEqualTo("MATCH (n:`ANode` {aProperty: 23}) RETURN n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apiguardian.api.API;
import org.neo4j.cypherdsl.core.ListComprehension.OngoingDefinitionWithVariable;
Expand Down Expand Up @@ -393,6 +394,17 @@ public static MapExpression mapOf(Object... keysAndValues) {
return MapExpression.create(keysAndValues);
}

/**
* Creates a map of expression from a Java Map.
*
* @param map A map to be turned into a MapExpression
* @return A new map expression.
*/
public static MapExpression asExpression(Map<String, Object> map) {

return MapExpression.create(map);
}

/**
* Creates a {@link ListExpression list-expression} from several expressions.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.neo4j.cypherdsl.core;

import java.util.Map;

/**
* A container that exposes methods to add properties with values to nodes or relationships.
*
Expand Down Expand Up @@ -45,4 +47,15 @@ public interface ExposesProperties<T extends ExposesProperties<?> & PropertyCont
* @return The new property container.
*/
T withProperties(Object... keysAndValues);

/**
* Creates a a copy of this property container with additional properties.
*
* @param newProperties A map with the new properties
* @return The new property container.
*/
default T withProperties(Map<String, Object> newProperties) {

return withProperties(MapExpression.create(newProperties));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apiguardian.api.API;
Expand All @@ -43,6 +44,18 @@
@API(status = INTERNAL, since = "1.0")
public final class MapExpression extends TypedSubtree<Expression> implements Expression {

static MapExpression create(Map<String, Object> map) {

Object[] args = new Object[map.size() * 2];
int i = 0;
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object value = entry.getValue();
args[i++] = entry.getKey();
args[i++] = value instanceof Expression ? value : Cypher.literalOf(value);
}
return create(args);
}

static MapExpression create(Object... input) {

Assertions.isTrue(input.length % 2 == 0, "Need an even number of input parameters");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

Expand Down Expand Up @@ -2701,6 +2703,23 @@ void shouldRenderPointFunction() {
@Nested
class PropertyRendering {

@Test // GH-157
void usingExistingJavaMaps() {

Map<String, Object> newProperties = new LinkedHashMap<>();
newProperties.put("prop1", 23);
newProperties.put("theTruth", 42);
newProperties.put("somethingElse", "foobar");
newProperties.put("aParam", Cypher.parameter("x").withValue("y"));
Node node = Cypher
.node("ANode")
.named("n")
.withProperties(newProperties);

assertThat(Cypher.match(node).returning(node).build().getCypher())
.isEqualTo("MATCH (n:`ANode` {prop1: 23, theTruth: 42, somethingElse: 'foobar', aParam: $x}) RETURN n");
}

@Test // GH-114
void manuallyNested() {
Node node = Cypher.node("Person").named("p");
Expand Down

0 comments on commit 0a39867

Please sign in to comment.