Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to copy values as param with multiple values #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -165,7 +165,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 @@ -330,6 +330,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 @@ -560,6 +560,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