Skip to content

Commit

Permalink
add ability to copy values as param with multiple values
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanHealey committed Jan 12, 2023
1 parent 81182e5 commit da11bf3
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/main/java/com/lmax/simpledsl/api/DslValues.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ default <T> T[] valuesAs(final String name, final Class<T> type, final Function<
* <p>
* Returns an empty {@link Stream} if the parameter is optional and a value has not been supplied.
*
* @param name the name of the parameter.
* @param name the name of the parameter.
* @param enumType the {@link Enum} type.
* @param <T> the {@link Enum} type.
* @return a {@link Stream} of values supplied for the parameter.
Expand Down Expand Up @@ -307,6 +307,76 @@ default String valueAsParamNamed(final String oldParamName, final String newPara
return value != null ? newParamName + ": " + value : null;
}

/**
* 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},
* then {@code valuesAsParam("user")} would return {@code user: jenny,john}. The delimiter takes whichever value is defined in the parameter definition.
* <p>
* This is useful when reusing DSL methods to build higher level functions. e.g.
*
* <pre>{@code
* public void createUserAndLogin(String... args) {
* DslParams params = new DslParams(args,
* new RequiredParam("user"),
* new RequiredParam("accountTypes").setAllowMultipleValues(";")
* );
* createUser(params.valueAsParam("user"), "password: password", params.valuesAsParam("accountTypes");
* login(params.valueAsParam("user"), "password: password");
* }
* }</pre>
*
* @param name the name of the parameter.
* @return the value supplied for that parameter, formatted as a parameter ready to pass on to another method that uses Simple-DSL.
* @throws IllegalArgumentException if {@code name} does not match the name of a supported parameter.
*/
default String valuesAsParam(final String name)
{
final String[] values = values(name);
final String delimiter = stream(getParams())
.filter(arg -> arg.getName().equals(name.toLowerCase()))
.findFirst()
.map(DslArg::getMultipleValueSeparator)
.orElseThrow(() -> new IllegalArgumentException(name + " is not a parameter"));


return values.length != 0 ? name + ": " + String.join(delimiter, values) : null;
}

/**
* Retrieve the value supplied for a parameter formatted as a parameter with the given name.
* For example, if the parameter {@literal user} was given the values {@literal jenny, john}, then
* {@code valuesAsParamNamed("user", "person")} would return {@code person: jenny,john}.
* The delimiter takes whichever value is defined in the parameter definition.
* <p>
* This is useful when reusing DSL methods to build higher level functions. e.g.
*
* <pre>{@code
* public void createUserAndLogin(String... args) {
* DslParams params = new DslParams(args,
* new RequiredParam("user"),
* new RequiredParam("accountType").setAllowMultipleValues());
* generateRandomUser(params.valueAsParamNamed("user", "rememberUserAs"), params.valuesAsParamNamed("accountType", "accounts");
* login(params.valueAsParam("user"), "password: password");
* }
* }</pre>
*
* @param oldParamName the name of the parameter.
* @param newParamName the new name of the parameter.
* @return the value supplied for that parameter, formatted as a parameter ready to pass on to another method that uses Simple-DSL.
* @throws IllegalArgumentException if {@code name} does not match the name of a supported parameter.
*/
default String valuesAsParamNamed(final String oldParamName, final String newParamName)
{
final String[] values = values(oldParamName);
final String delimiter = stream(getParams())
.filter(arg -> arg.getName().equals(oldParamName.toLowerCase()))
.findFirst()
.map(DslArg::getMultipleValueSeparator)
.orElseThrow(() -> new IllegalArgumentException(oldParamName + " is not a parameter"));


return values.length != 0 ? newParamName + ": " + String.join(delimiter, values) : null;
}

/**
* 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.
*
Expand Down
68 changes: 68 additions & 0 deletions src/test/java/com/lmax/simpledsl/internal/DslParamsParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,74 @@ public void shouldBeAbleToSpecifyMultipleValuesForParamInGroupUsingACustomSepara
assertEquals("2", groups[1].values("value")[1]);
}

@Test
public void shouldGetValuesAsAParamString()
{
final String[] args = {
"a: value1", "a: value2"
};
final DslArg[] parameters = {
new RequiredArg("a").setAllowMultipleValues(),
};

final DslParamsParser parser = new DslParamsParser();

final DslParams params = parser.parse(args, parameters);

assertEquals("a: value1,value2", params.valuesAsParam("a"));
}

@Test
public void shouldGetValuesAsAParamStringWithCustomDelimiter()
{
final String[] args = {
"a: value1", "a: value2"
};
final DslArg[] parameters = {
new RequiredArg("a").setAllowMultipleValues(";"),
};

final DslParamsParser parser = new DslParamsParser();

final DslParams params = parser.parse(args, parameters);

assertEquals("a: value1;value2", params.valuesAsParam("a"));
}

@Test
public void shouldGetValuesAsAParamStringWithDifferentName()
{
final String[] args = {
"a: value1", "a: value2"
};
final DslArg[] parameters = {
new RequiredArg("a").setAllowMultipleValues(),
};

final DslParamsParser parser = new DslParamsParser();

final DslParams params = parser.parse(args, parameters);

assertEquals("b: value1,value2", params.valuesAsParamNamed("a", "b"));
}

@Test
public void shouldGetValuesAsAParamStringWithCustomDelimiterWithDifferentName()
{
final String[] args = {
"a: value1", "a: value2"
};
final DslArg[] parameters = {
new RequiredArg("a").setAllowMultipleValues(";"),
};

final DslParamsParser parser = new DslParamsParser();

final DslParams params = parser.parse(args, parameters);

assertEquals("b: value1;value2", params.valuesAsParamNamed("a", "b"));
}

@Test
public void shouldBeAbleToRetrieveGroupsOfParamsWhenSomeOptionalValuesAreOmitted()
{
Expand Down

0 comments on commit da11bf3

Please sign in to comment.