Skip to content

Commit

Permalink
Add convenience method for copying out dsl arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
swarren12 committed Dec 1, 2021
1 parent e38c6f5 commit cf5338f
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/com/lmax/simpledsl/api/DslParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ public interface DslParams extends DslValues
*/
RepeatingGroup[] valuesAsGroup(String groupName);

/**
* Create a new {@literal array} of arguments that can be re-parsed into a new instance of {@link DslParams}.
*
* @param args the name of the arguments to copy
* @return the new {@literal array} of parameters
*/
String[] copyArgs(String... args);

/**
* Create new {@link DslParams}.
*
Expand All @@ -64,7 +72,6 @@ static DslParams create(String[] args, DslArg... arguments)
return new DslParamsParser().parse(args, arguments);
}


/**
* A shorthand way to create a {@link DslParams} instance that accepts a single required parameter and return the
* value that was supplied for that parameter.
Expand All @@ -79,5 +86,4 @@ static String getSingleRequiredParamValue(final String[] args, final String requ
.parse(args, new RequiredArg(requiredParamName))
.value(requiredParamName);
}

}
2 changes: 2 additions & 0 deletions src/main/java/com/lmax/simpledsl/internal/DslParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ abstract class DslParam
abstract RepeatingParamGroup asRepeatingParamGroup();

abstract boolean hasValue();

abstract String[] rawArgs();
}
17 changes: 17 additions & 0 deletions src/main/java/com/lmax/simpledsl/internal/DslParamsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@
import com.lmax.simpledsl.api.DslParams;
import com.lmax.simpledsl.api.RepeatingGroup;

import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;

/**
* The internal implementation of {@link DslParams}.
Expand Down Expand Up @@ -58,6 +63,18 @@ public RepeatingGroup[] valuesAsGroup(final String groupName)
return repeatingParamGroup.values();
}

@Override
public String[] copyArgs(final String... argsToCopy)
{
final Set<String> uniqueArgsToCopy = new HashSet<>(asList(argsToCopy));
return stream(args)
.map(DslArg::getName)
.filter(uniqueArgsToCopy::contains)
.map(this::getDslParam)
.flatMap(param -> stream(param.rawArgs()))
.toArray(String[]::new);
}

@Override
public boolean hasValue(final String name)
{
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/lmax/simpledsl/internal/RepeatingParamGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

import java.util.List;

import static java.util.Arrays.stream;

class RepeatingParamGroup extends DslParam
{
private final String name;
Expand Down Expand Up @@ -64,4 +66,12 @@ public boolean hasValue()
{
return !values.isEmpty();
}

@Override
String[] rawArgs()
{
return values.stream()
.flatMap(val -> stream(val.rawArgs()))
.toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import java.util.List;
import java.util.Map;

import static java.util.Arrays.stream;

class RepeatingParamValues implements RepeatingGroup
{
private final DslArg[] dslArgs;
Expand Down Expand Up @@ -47,4 +49,12 @@ private List<String> getValues(final String name)
{
return name != null ? valuesByName.get(name.toLowerCase()) : null;
}

public String[] rawArgs()
{
return stream(dslArgs)
.flatMap(arg -> getValues(arg.getName()).stream()
.map(val -> arg.getName() + ": " + val))
.toArray(String[]::new);
}
}
6 changes: 6 additions & 0 deletions src/main/java/com/lmax/simpledsl/internal/SimpleDslParam.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ boolean hasValue()
return !values.isEmpty();
}

@Override
String[] rawArgs()
{
return values.stream().map(val -> name + ": " + val).toArray(String[]::new);
}

/**
* Get the value for this parameter. If multiple values are allowed, use {@link #getValues()} instead.
*
Expand Down
118 changes: 118 additions & 0 deletions src/test/java/com/lmax/simpledsl/internal/DslParamsImplTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@

import com.lmax.simpledsl.api.DslArg;
import com.lmax.simpledsl.api.DslParams;
import com.lmax.simpledsl.api.OptionalArg;
import com.lmax.simpledsl.api.RepeatingArgGroup;
import com.lmax.simpledsl.api.RequiredArg;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

Expand Down Expand Up @@ -263,4 +267,118 @@ public void shouldReturnOptionalListWhenMultipleParameterValueIsSupplied()

assertEquals(Optional.of(asList("value1", "value2")), params.valuesAsOptional("a"));
}

@Test
public void shouldCopyArguments()
{
final DslArg[] args = {new RequiredArg("a"), new RequiredArg("b"), new RequiredArg("c")};

final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));
final SimpleDslParam bParam = new SimpleDslParam("b", singletonList("value3"));
final SimpleDslParam cParam = new SimpleDslParam("c", singletonList("value4"));

final Map<String, DslParam> rawParams = new HashMap<>();
rawParams.put("a", aParam);
rawParams.put("b", bParam);
rawParams.put("c", cParam);

final DslParams params = new DslParamsImpl(args, rawParams);

assertArrayEquals(new String[]{"a: value1", "a: value2", "b: value3"}, params.copyArgs("a", "b"));
}

@Test
public void shouldCopyArgumentWhenOptionalArgumentWasNotSupplied()
{
final DslArg[] args = {new RequiredArg("a"), new RequiredArg("b"), new OptionalArg("c")};

final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));
final SimpleDslParam bParam = new SimpleDslParam("b", singletonList("value3"));
final SimpleDslParam cParam = new SimpleDslParam("c", emptyList());

final Map<String, DslParam> rawParams = new HashMap<>();
rawParams.put("a", aParam);
rawParams.put("b", bParam);
rawParams.put("c", cParam);

final DslParams params = new DslParamsImpl(args, rawParams);

assertArrayEquals(new String[]{"a: value1", "a: value2"}, params.copyArgs("a", "c"));
}

@Test
public void shouldCopyArgumentIncludingARepeatingGroup()
{
final RequiredArg groupArg = new RequiredArg("c");
final DslArg[] args = {
new RequiredArg("a"),
new RequiredArg("b"),
new RepeatingArgGroup(groupArg)
};

final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));
final SimpleDslParam bParam = new SimpleDslParam("b", singletonList("value3"));
final RepeatingParamGroup gParam = new RepeatingParamGroup("c", singletonList(
new RepeatingParamValues(new DslArg[]{groupArg}, Collections.singletonMap("c", singletonList("value3")))
));

final Map<String, DslParam> rawParams = new HashMap<>();
rawParams.put("a", aParam);
rawParams.put("b", bParam);
rawParams.put("c", gParam);

final DslParams params = new DslParamsImpl(args, rawParams);

assertArrayEquals(new String[]{"a: value1", "a: value2", "c: value3"}, params.copyArgs("a", "c"));
}

@Test
public void shouldCopyArgumentIncludingARepeatingGroupWithMultipleArguments()
{
final RequiredArg groupArg = new RequiredArg("c");
final RequiredArg otherGroupArg = new RequiredArg("d");
final DslArg[] args = {
new RequiredArg("a"),
new RequiredArg("b"),
new RepeatingArgGroup(groupArg, otherGroupArg)
};

final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));
final SimpleDslParam bParam = new SimpleDslParam("b", singletonList("value3"));

final Map<String, List<String>> rawGroupParams = new HashMap<>();
rawGroupParams.put("c", singletonList("value3"));
rawGroupParams.put("d", asList("value4", "value5"));
final RepeatingParamGroup gParam = new RepeatingParamGroup("c", singletonList(
new RepeatingParamValues(new DslArg[]{groupArg, otherGroupArg}, rawGroupParams)
));

final Map<String, DslParam> rawParams = new HashMap<>();
rawParams.put("a", aParam);
rawParams.put("b", bParam);
rawParams.put("c", gParam);

final DslParams params = new DslParamsImpl(args, rawParams);

assertArrayEquals(new String[]{"a: value1", "a: value2", "c: value3", "d: value4", "d: value5"}, params.copyArgs("a", "c"));
}

@Test
public void shouldCopyArgumentsOnlyOnceEvenIfSpecifiedMultipleTimes()
{
final DslArg[] args = {new RequiredArg("a"), new RequiredArg("b"), new RequiredArg("c")};

final SimpleDslParam aParam = new SimpleDslParam("a", asList("value1", "value2"));
final SimpleDslParam bParam = new SimpleDslParam("b", singletonList("value3"));
final SimpleDslParam cParam = new SimpleDslParam("c", emptyList());

final Map<String, DslParam> rawParams = new HashMap<>();
rawParams.put("a", aParam);
rawParams.put("b", bParam);
rawParams.put("c", cParam);

final DslParams params = new DslParamsImpl(args, rawParams);

assertArrayEquals(new String[]{"a: value1", "a: value2"}, params.copyArgs("a", "a", "a"));
}
}

0 comments on commit cf5338f

Please sign in to comment.