Skip to content

Commit 8e720f8

Browse files
committed
add ability to copy values as param with multiple values
1 parent e81843d commit 8e720f8

File tree

2 files changed

+139
-1
lines changed

2 files changed

+139
-1
lines changed

src/main/java/com/lmax/simpledsl/api/DslValues.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ default <T> T[] valuesAs(final String name, final Class<T> type, final Function<
165165
* <p>
166166
* Returns an empty {@link Stream} if the parameter is optional and a value has not been supplied.
167167
*
168-
* @param name the name of the parameter.
168+
* @param name the name of the parameter.
169169
* @param enumType the {@link Enum} type.
170170
* @param <T> the {@link Enum} type.
171171
* @return a {@link Stream} of values supplied for the parameter.
@@ -330,6 +330,76 @@ default String valueAsParamNamed(final String oldParamName, final String newPara
330330
return value != null ? newParamName + ": " + value : null;
331331
}
332332

333+
/**
334+
* Retrieve the value supplied for a parameter formatted as a parameter with multiple values. For example, if the parameter {@literal users} was given the value {@literal jenny, john},
335+
* then {@code valuesAsParam("user")} would return {@code user: jenny,john}. The delimiter takes whichever value is defined in the parameter definition.
336+
* <p>
337+
* This is useful when reusing DSL methods to build higher level functions. e.g.
338+
*
339+
* <pre>{@code
340+
* public void createUserAndLogin(String... args) {
341+
* DslParams params = new DslParams(args,
342+
* new RequiredParam("user"),
343+
* new RequiredParam("accountTypes").setAllowMultipleValues(";")
344+
* );
345+
* createUser(params.valueAsParam("user"), "password: password", params.valuesAsParam("accountTypes");
346+
* login(params.valueAsParam("user"), "password: password");
347+
* }
348+
* }</pre>
349+
*
350+
* @param name the name of the parameter.
351+
* @return the value supplied for that parameter, formatted as a parameter ready to pass on to another method that uses Simple-DSL.
352+
* @throws IllegalArgumentException if {@code name} does not match the name of a supported parameter.
353+
*/
354+
default String valuesAsParam(final String name)
355+
{
356+
final String[] values = values(name);
357+
final String delimiter = stream(getParams())
358+
.filter(arg -> arg.getName().equals(name.toLowerCase()))
359+
.findFirst()
360+
.map(DslArg::getMultipleValueSeparator)
361+
.orElseThrow(() -> new IllegalArgumentException(name + " is not a parameter"));
362+
363+
364+
return values.length != 0 ? name + ": " + String.join(delimiter, values) : null;
365+
}
366+
367+
/**
368+
* Retrieve the value supplied for a parameter formatted as a parameter with the given name.
369+
* For example, if the parameter {@literal user} was given the values {@literal jenny, john}, then
370+
* {@code valuesAsParamNamed("user", "person")} would return {@code person: jenny,john}.
371+
* The delimiter takes whichever value is defined in the parameter definition.
372+
* <p>
373+
* This is useful when reusing DSL methods to build higher level functions. e.g.
374+
*
375+
* <pre>{@code
376+
* public void createUserAndLogin(String... args) {
377+
* DslParams params = new DslParams(args,
378+
* new RequiredParam("user"),
379+
* new RequiredParam("accountType").setAllowMultipleValues());
380+
* generateRandomUser(params.valueAsParamNamed("user", "rememberUserAs"), params.valuesAsParamNamed("accountType", "accounts");
381+
* login(params.valueAsParam("user"), "password: password");
382+
* }
383+
* }</pre>
384+
*
385+
* @param oldParamName the name of the parameter.
386+
* @param newParamName the new name of the parameter.
387+
* @return the value supplied for that parameter, formatted as a parameter ready to pass on to another method that uses Simple-DSL.
388+
* @throws IllegalArgumentException if {@code name} does not match the name of a supported parameter.
389+
*/
390+
default String valuesAsParamNamed(final String oldParamName, final String newParamName)
391+
{
392+
final String[] values = values(oldParamName);
393+
final String delimiter = stream(getParams())
394+
.filter(arg -> arg.getName().equals(oldParamName.toLowerCase()))
395+
.findFirst()
396+
.map(DslArg::getMultipleValueSeparator)
397+
.orElseThrow(() -> new IllegalArgumentException(oldParamName + " is not a parameter"));
398+
399+
400+
return values.length != 0 ? newParamName + ": " + String.join(delimiter, values) : null;
401+
}
402+
333403
/**
334404
* Retrieve the values supplied for a parameter as a {@link List}. Returns an empty list if the parameter is optional and a value has not been supplied.
335405
*

src/test/java/com/lmax/simpledsl/internal/DslParamsParserTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,74 @@ public void shouldBeAbleToSpecifyMultipleValuesForParamInGroupUsingACustomSepara
560560
assertEquals("2", groups[1].values("value")[1]);
561561
}
562562

563+
@Test
564+
public void shouldGetValuesAsAParamString()
565+
{
566+
final String[] args = {
567+
"a: value1", "a: value2"
568+
};
569+
final DslArg[] parameters = {
570+
new RequiredArg("a").setAllowMultipleValues(),
571+
};
572+
573+
final DslParamsParser parser = new DslParamsParser();
574+
575+
final DslParams params = parser.parse(args, parameters);
576+
577+
assertEquals("a: value1,value2", params.valuesAsParam("a"));
578+
}
579+
580+
@Test
581+
public void shouldGetValuesAsAParamStringWithCustomDelimiter()
582+
{
583+
final String[] args = {
584+
"a: value1", "a: value2"
585+
};
586+
final DslArg[] parameters = {
587+
new RequiredArg("a").setAllowMultipleValues(";"),
588+
};
589+
590+
final DslParamsParser parser = new DslParamsParser();
591+
592+
final DslParams params = parser.parse(args, parameters);
593+
594+
assertEquals("a: value1;value2", params.valuesAsParam("a"));
595+
}
596+
597+
@Test
598+
public void shouldGetValuesAsAParamStringWithDifferentName()
599+
{
600+
final String[] args = {
601+
"a: value1", "a: value2"
602+
};
603+
final DslArg[] parameters = {
604+
new RequiredArg("a").setAllowMultipleValues(),
605+
};
606+
607+
final DslParamsParser parser = new DslParamsParser();
608+
609+
final DslParams params = parser.parse(args, parameters);
610+
611+
assertEquals("b: value1,value2", params.valuesAsParamNamed("a", "b"));
612+
}
613+
614+
@Test
615+
public void shouldGetValuesAsAParamStringWithCustomDelimiterWithDifferentName()
616+
{
617+
final String[] args = {
618+
"a: value1", "a: value2"
619+
};
620+
final DslArg[] parameters = {
621+
new RequiredArg("a").setAllowMultipleValues(";"),
622+
};
623+
624+
final DslParamsParser parser = new DslParamsParser();
625+
626+
final DslParams params = parser.parse(args, parameters);
627+
628+
assertEquals("b: value1;value2", params.valuesAsParamNamed("a", "b"));
629+
}
630+
563631
@Test
564632
public void shouldBeAbleToRetrieveGroupsOfParamsWhenSomeOptionalValuesAreOmitted()
565633
{

0 commit comments

Comments
 (0)