Skip to content
This repository was archived by the owner on Jul 6, 2023. It is now read-only.

Commit 1f68d8e

Browse files
committed
Refactor Parameter setting code into its own class.
1 parent c371924 commit 1f68d8e

File tree

13 files changed

+193
-125
lines changed

13 files changed

+193
-125
lines changed

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellPlainIntegrationTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.junit.rules.ExpectedException;
88

9+
import org.neo4j.cypher.internal.evaluator.EvaluationException;
910
import org.neo4j.shell.CypherShell;
1011
import org.neo4j.shell.ShellParameterMap;
1112
import org.neo4j.shell.StringLinePrinter;
@@ -81,7 +82,8 @@ public void cypherWithExplainStatements() throws CommandException {
8182
}
8283

8384
@Test
84-
public void shouldUseParamFromCLIArgs() throws CommandException {
85+
public void shouldUseParamFromCLIArgs() throws EvaluationException, CommandException
86+
{
8587
// given a CLI arg
8688
ShellParameterMap parameterMap = new ShellParameterMap();
8789
parameterMap.setParameter( "foo", "'bar'" );

cypher-shell/src/integration-test/java/org/neo4j/shell/commands/CypherShellVerboseIntegrationTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.junit.Test;
77
import org.junit.rules.ExpectedException;
88

9+
import org.neo4j.cypher.internal.evaluator.EvaluationException;
910
import org.neo4j.shell.CypherShell;
1011
import org.neo4j.shell.ShellParameterMap;
1112
import org.neo4j.shell.StringLinePrinter;
@@ -153,38 +154,38 @@ public void commitScenario() throws CommandException {
153154
}
154155

155156
@Test
156-
public void paramsAndListVariables() throws CommandException {
157-
assertTrue(shell.getParamaterMap().allParameterValues().isEmpty());
157+
public void paramsAndListVariables() throws EvaluationException, CommandException {
158+
assertTrue(shell.getParameterMap().allParameterValues().isEmpty());
158159

159160
long randomLong = System.currentTimeMillis();
160161
String stringInput = "\"randomString\"";
161-
shell.getParamaterMap().setParameter("string", stringInput);
162-
Object paramValue = shell.getParamaterMap().setParameter("bob", String.valueOf(randomLong));
162+
shell.getParameterMap().setParameter("string", stringInput);
163+
Object paramValue = shell.getParameterMap().setParameter("bob", String.valueOf(randomLong));
163164
assertEquals(randomLong, paramValue);
164165

165166
shell.execute("RETURN { bob }, $string");
166167

167168
String result = linePrinter.output();
168169
assertThat(result, containsString("| { bob }"));
169170
assertThat(result, containsString("| " + randomLong + " | " + stringInput + " |"));
170-
assertEquals(randomLong, shell.getParamaterMap().allParameterValues().get("bob"));
171-
assertEquals("randomString", shell.getParamaterMap().allParameterValues().get("string"));
171+
assertEquals(randomLong, shell.getParameterMap().allParameterValues().get( "bob"));
172+
assertEquals("randomString", shell.getParameterMap().allParameterValues().get( "string"));
172173
}
173174

174175
@Test
175-
public void paramsAndListVariablesWithSpecialCharacters() throws CommandException {
176-
assertTrue(shell.getParamaterMap().allParameterValues().isEmpty());
176+
public void paramsAndListVariablesWithSpecialCharacters() throws EvaluationException, CommandException {
177+
assertTrue(shell.getParameterMap().allParameterValues().isEmpty());
177178

178179
long randomLong = System.currentTimeMillis();
179-
Object paramValue = shell.getParamaterMap().setParameter("`bob`", String.valueOf(randomLong));
180+
Object paramValue = shell.getParameterMap().setParameter("`bob`", String.valueOf(randomLong));
180181
assertEquals(randomLong, paramValue);
181182

182183
shell.execute("RETURN { `bob` }");
183184

184185
String result = linePrinter.output();
185186
assertThat(result, containsString("| { `bob` }"));
186187
assertThat(result, containsString("\n| " + randomLong+ " |\n"));
187-
assertEquals(randomLong, shell.getParamaterMap().allParameterValues().get("bob"));
188+
assertEquals(randomLong, shell.getParameterMap().allParameterValues().get("bob"));
188189
}
189190

190191
@Test

cypher-shell/src/main/java/org/neo4j/shell/CypherShell.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public String getActualDatabaseAsReportedByServer()
212212
/**
213213
* @return the parameter map.
214214
*/
215-
public ParameterMap getParamaterMap()
215+
public ParameterMap getParameterMap()
216216
{
217217
return parameterMap;
218218
}

cypher-shell/src/main/java/org/neo4j/shell/ParameterMap.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.neo4j.shell;
22

3-
import org.neo4j.shell.exception.CommandException;
3+
import org.neo4j.cypher.internal.evaluator.EvaluationException;
44
import org.neo4j.shell.state.ParamValue;
55

66
import javax.annotation.Nonnull;
@@ -15,7 +15,7 @@ public interface ParameterMap {
1515
* @param valueString to interpret the value from
1616
* @return the evaluated value
1717
*/
18-
Object setParameter(@Nonnull String name, @Nonnull String valueString) throws CommandException;
18+
Object setParameter(@Nonnull String name, @Nonnull String valueString) throws EvaluationException;
1919

2020
/**
2121
* @return map of all currently set variables and their values

cypher-shell/src/main/java/org/neo4j/shell/ShellParameterMap.java

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import org.neo4j.cypher.internal.evaluator.EvaluationException;
99
import org.neo4j.cypher.internal.evaluator.Evaluator;
1010
import org.neo4j.cypher.internal.evaluator.ExpressionEvaluator;
11-
import org.neo4j.shell.exception.CommandException;
1211
import org.neo4j.shell.prettyprint.CypherVariablesFormatter;
1312
import org.neo4j.shell.state.ParamValue;
1413

@@ -21,16 +20,12 @@ public class ShellParameterMap implements ParameterMap
2120
private ExpressionEvaluator evaluator = Evaluator.expressionEvaluator();
2221

2322
@Override
24-
public Object setParameter( @Nonnull String name, @Nonnull String valueString ) throws CommandException
23+
public Object setParameter( @Nonnull String name, @Nonnull String valueString ) throws EvaluationException
2524
{
26-
try {
27-
String parameterName = CypherVariablesFormatter.unescapedCypherVariable( name);
28-
Object value = evaluator.evaluate(valueString, Object.class);
29-
queryParams.put(parameterName, new ParamValue(valueString, value));
30-
return value;
31-
} catch ( EvaluationException e ) {
32-
throw new CommandException(e.getMessage(), e);
33-
}
25+
String parameterName = CypherVariablesFormatter.unescapedCypherVariable( name );
26+
Object value = evaluator.evaluate( valueString, Object.class );
27+
queryParams.put( parameterName, new ParamValue( valueString, value ) );
28+
return value;
3429
}
3530

3631
@Nonnull

cypher-shell/src/main/java/org/neo4j/shell/cli/AddParamArgumentAction.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,32 @@
77

88
import java.util.Map;
99

10+
import org.neo4j.cypher.internal.evaluator.EvaluationException;
1011
import org.neo4j.shell.ParameterMap;
11-
import org.neo4j.shell.commands.Param;
12-
import org.neo4j.shell.exception.CommandException;
12+
import org.neo4j.shell.util.ParameterSetter;
1313

1414
/**
1515
* Action that adds arguments to a ParameterMap.
1616
* This action always consumes an argument.
1717
*/
18-
public class AddParamArgumentAction implements ArgumentAction
18+
public class AddParamArgumentAction extends ParameterSetter<ArgumentParserException> implements ArgumentAction
1919
{
20-
private final Param paramCommand;
21-
2220
/**
23-
* @param parameterMap the ParameterMap to add parameters to,
21+
* @param parameterMap the ParameterMap to add parameters to.
2422
*/
2523
AddParamArgumentAction( ParameterMap parameterMap )
2624
{
27-
paramCommand = new Param( parameterMap, false );
25+
super(parameterMap);
2826
}
2927

3028
@Override
3129
public void run( ArgumentParser parser, Argument arg, Map<String,Object> attrs, String flag, Object value ) throws ArgumentParserException
3230
{
3331
try
3432
{
35-
paramCommand.execute( value.toString() );
33+
execute( value.toString() );
3634
}
37-
catch ( CommandException e )
35+
catch ( Exception e )
3836
{
3937
throw new ArgumentParserException(e.getMessage(), e, parser);
4038
}
@@ -51,4 +49,22 @@ public boolean consumeArgument()
5149
{
5250
return true;
5351
}
52+
53+
@Override
54+
protected void onWrongUsage()
55+
{
56+
throw new IllegalArgumentException("Incorrect usage.\nusage: --param \"name => value\"");
57+
}
58+
59+
@Override
60+
protected void onWrongNumberOfArguments()
61+
{
62+
throw new IllegalArgumentException("Incorrect number of arguments.\nusage: --param \"name => value\"");
63+
}
64+
65+
@Override
66+
protected void onEvaluationException( EvaluationException e )
67+
{
68+
throw new RuntimeException( e.getMessage(), e );
69+
}
5470
}

cypher-shell/src/main/java/org/neo4j/shell/commands/CommandHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class CommandHelper {
2323
private final TreeMap<String, Command> commands = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
2424

2525
public CommandHelper(Logger logger, Historian historian, CypherShell cypherShell) {
26-
registerAllCommands(logger, historian, cypherShell, cypherShell, cypherShell.getParamaterMap());
26+
registerAllCommands(logger, historian, cypherShell, cypherShell, cypherShell.getParameterMap());
2727
}
2828

2929
private void registerAllCommands(Logger logger,
@@ -38,7 +38,7 @@ private void registerAllCommands(Logger logger,
3838
registerCommand(new Begin(transactionHandler));
3939
registerCommand(new Commit(transactionHandler));
4040
registerCommand(new Rollback(transactionHandler));
41-
registerCommand(new Param(parameterMap, true));
41+
registerCommand(new Param(parameterMap));
4242
registerCommand(new Params(logger, parameterMap));
4343
}
4444

Lines changed: 21 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package org.neo4j.shell.commands;
22

3+
import org.neo4j.cypher.internal.evaluator.EvaluationException;
34
import org.neo4j.shell.ParameterMap;
45
import org.neo4j.shell.exception.CommandException;
56
import org.neo4j.shell.log.AnsiFormattedText;
7+
import org.neo4j.shell.util.ParameterSetter;
68

79
import javax.annotation.Nonnull;
810
import java.util.Collections;
@@ -15,34 +17,20 @@
1517
/**
1618
* This command sets a variable to a name, for use as query parameter.
1719
*/
18-
public class Param implements Command {
19-
// Match arguments such as "(key) (value with possible spaces)" where key and value are any strings
20-
private static final Pattern backtickPattern = Pattern.compile("^\\s*(?<key>(`([^`])*`)+?):?\\s+(?<value>.+)$");
21-
private static final Pattern backtickLambdaPattern = Pattern.compile("^\\s*(?<key>(`([^`])*`)+?)\\s*=>\\s*(?<value>.+)$");
22-
private static final Pattern argPattern = Pattern.compile("^\\s*(?<key>[\\p{L}_][\\p{L}0-9_]*):?\\s+(?<value>.+)$");
23-
private static final Pattern lambdaPattern = Pattern.compile("^\\s*(?<key>[\\p{L}_][\\p{L}0-9_]*)\\s*=>\\s*(?<value>.+)$");
24-
private static final Pattern lambdaMapPattern = Pattern.compile("^\\s*(?<key>[\\p{L}_][\\p{L}0-9_]*):\\s*=>\\s*(?<value>.+)$");
25-
26-
private final String commandName;
27-
private final boolean fromWithinShell;
28-
private final ParameterMap parameterMap;
20+
public class Param extends ParameterSetter<CommandException> implements Command {
21+
private final static String COMMAND_NAME = ":param";
2922

3023
/**
3124
* @param parameterMap the map to set parameters in
32-
* @param fromWithinShell If {@code true}, this param evaluates ":param" commands from within the shell.
33-
* If {@code false}, this param evaluates "--param" commands from CLI arguments.
3425
*/
35-
public Param(@Nonnull final ParameterMap parameterMap,
36-
final boolean fromWithinShell) {
37-
this.parameterMap = parameterMap;
38-
this.commandName = fromWithinShell ? ":param" : "--param";
39-
this.fromWithinShell = fromWithinShell;
26+
public Param(@Nonnull final ParameterMap parameterMap) {
27+
super(parameterMap);
4028
}
4129

4230
@Nonnull
4331
@Override
4432
public String getName() {
45-
return commandName;
33+
return COMMAND_NAME;
4634
}
4735

4836
@Nonnull
@@ -54,7 +42,7 @@ public String getDescription() {
5442
@Nonnull
5543
@Override
5644
public String getUsage() {
57-
return fromWithinShell ? "name => value" : " \"name => value\"" ;
45+
return "name => value" ;
5846
}
5947

6048
@Nonnull
@@ -70,45 +58,22 @@ public List<String> getAliases() {
7058
}
7159

7260
@Override
73-
public void execute(@Nonnull final String argString) throws CommandException {
74-
Matcher lambdaMapMatcher = lambdaMapPattern.matcher(argString);
75-
if (lambdaMapMatcher.matches()) {
76-
throw new CommandException(AnsiFormattedText.from("Incorrect usage.\nusage: ")
77-
.bold().append( commandName ).boldOff().append( " ").append( getUsage()));
78-
}
79-
if (!assignIfValidParameter(argString)) {
80-
throw new CommandException(AnsiFormattedText.from("Incorrect number of arguments.\nusage: ")
81-
.bold().append( commandName ).boldOff().append( " ").append( getUsage()));
82-
}
83-
}
84-
85-
private boolean assignIfValidParameter(@Nonnull String argString) throws CommandException {
86-
return setParameterIfItMatchesPattern(argString, lambdaPattern, assignIfValidParameter())
87-
|| setParameterIfItMatchesPattern(argString, argPattern, assignIfValidParameter())
88-
|| setParameterIfItMatchesPattern(argString, backtickLambdaPattern, backTickMatchPattern())
89-
|| setParameterIfItMatchesPattern(argString, backtickPattern, backTickMatchPattern());
61+
protected void onWrongUsage() throws CommandException
62+
{
63+
throw new CommandException(AnsiFormattedText.from("Incorrect usage.\nusage: ")
64+
.bold().append( COMMAND_NAME ).boldOff().append( " ").append( getUsage()));
9065
}
9166

92-
private boolean setParameterIfItMatchesPattern(@Nonnull String argString, Pattern pattern,
93-
BiPredicate<String, Matcher> matchingFunction) throws CommandException {
94-
Matcher matcher = pattern.matcher(argString);
95-
if (matchingFunction.test(argString, matcher)) {
96-
parameterMap.setParameter(matcher.group("key"), matcher.group("value"));
97-
return true;
98-
} else {
99-
return false;
100-
}
101-
}
102-
103-
private BiPredicate<String, Matcher> assignIfValidParameter() {
104-
return (argString, matcher) -> matcher.matches();
67+
@Override
68+
protected void onWrongNumberOfArguments() throws CommandException
69+
{
70+
throw new CommandException(AnsiFormattedText.from("Incorrect number of arguments.\nusage: ")
71+
.bold().append( COMMAND_NAME ).boldOff().append( " ").append( getUsage()));
10572
}
10673

107-
private BiPredicate<String, Matcher> backTickMatchPattern() {
108-
return (argString, backtickLambdaMatcher) -> {
109-
return argString.trim().startsWith("`")
110-
&& backtickLambdaMatcher.matches()
111-
&& backtickLambdaMatcher.group("key").length() > 2;
112-
};
74+
@Override
75+
protected void onEvaluationException( EvaluationException e ) throws CommandException
76+
{
77+
throw new CommandException( e.getMessage(), e );
11378
}
11479
}

0 commit comments

Comments
 (0)