Skip to content

Commit bd9cf64

Browse files
committed
no skew for non simple kind
1 parent 8ecd92b commit bd9cf64

File tree

7 files changed

+84
-12
lines changed

7 files changed

+84
-12
lines changed

core/src/main/java/net/jbock/coerce/CoercionProvider.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import static javax.lang.model.element.Modifier.FINAL;
22+
import static net.jbock.coerce.CoercionKind.SIMPLE;
2223
import static net.jbock.coerce.CoercionKind.findKind;
2324
import static net.jbock.coerce.MapperClassValidator.validateMapperClass;
2425
import static net.jbock.coerce.MapperCoercion.mapperInit;
@@ -132,7 +133,7 @@ private Coercion handleMapper(
132133
TypeName mapperType = TypeName.get(mapperClass.asType());
133134
ParameterSpec mapperParam = ParameterSpec.builder(mapperType, snakeToCamel(paramName) + "Mapper").build();
134135
TriggerKind tk = trigger(sourceMethod.getReturnType());
135-
MapperSkew skew = mapperSkew(tk.trigger);
136+
MapperSkew skew = mapperSkew(tk);
136137
if (skew != null) {
137138
validateMapperClass(mapperClass, skew.mapperReturnType);
138139
return coercions.get(skew.baseType).getCoercion(field, tk.kind)
@@ -143,8 +144,11 @@ private Coercion handleMapper(
143144
}
144145
}
145146

146-
private MapperSkew mapperSkew(TypeMirror mirror) {
147-
TypeName input = TypeName.get(mirror);
147+
private MapperSkew mapperSkew(TriggerKind tk) {
148+
if (tk.kind != SIMPLE) {
149+
return null;
150+
}
151+
TypeName input = TypeName.get(tk.trigger);
148152
if (input.isPrimitive()) {
149153
if (!coercions.containsKey(input)) {
150154
return null;

core/src/main/java/net/jbock/coerce/ObjectByteCoercion.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package net.jbock.coerce;
22

3-
class ObjectByteCoercion extends BasicCharacterCoercion {
3+
class ObjectByteCoercion extends BasicByteCoercion {
44

55
ObjectByteCoercion() {
66
super(Byte.class);

core/src/test/java/net/jbock/compiler/ProcessorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,10 +511,10 @@ void mapperValid() {
511511
List<String> sourceLines = withImports(
512512
"@CommandLineArguments",
513513
"abstract class ValidArguments {",
514-
" @Parameter(mappedBy = Mapper.class) abstract int number();",
515-
" static class Mapper implements Function<String, Integer> {",
516-
" public Integer apply(String s) {",
517-
" return 1;",
514+
" @Parameter(mappedBy = Mapper.class) abstract List<OptionalInt> numbers();",
515+
" static class Mapper implements Function<String, OptionalInt> {",
516+
" public OptionalInt apply(String s) {",
517+
" return OptionalInt.of(1);",
518518
" }",
519519
" }",
520520
"}");

examples/src/main/java/net/jbock/examples/CustomMapperArguments.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract class CustomMapperArguments {
5151
@PositionalParameter(mappedBy = BooleanMapper.class)
5252
abstract List<Boolean> booleanList();
5353

54-
@PositionalParameter(mappedBy = OptionalIntMapper.class)
54+
@Parameter(mappedBy = OptionalIntMapper.class)
5555
abstract List<OptionalInt> optionalInts();
5656

5757
@Parameter(mappedBy = BooleanMapper.class)
@@ -117,11 +117,10 @@ static class OptionalIntMapper implements Function<String, OptionalInt> {
117117

118118
@Override
119119
public OptionalInt apply(String s) {
120-
try {
121-
return OptionalInt.of(Integer.parseInt(s));
122-
} catch (NumberFormatException e) {
120+
if (s.isEmpty()) {
123121
return OptionalInt.empty();
124122
}
123+
return OptionalInt.of(Integer.parseInt(s));
125124
}
126125
}
127126

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package net.jbock.examples;
2+
3+
import net.jbock.CommandLineArguments;
4+
import net.jbock.Parameter;
5+
6+
@CommandLineArguments
7+
abstract class PrimitiveArguments {
8+
9+
@Parameter(shortName = 'B')
10+
abstract byte simpleByte();
11+
12+
@Parameter(shortName = 'S')
13+
abstract short simpleShort();
14+
15+
@Parameter(shortName = 'I')
16+
abstract int simpleInt();
17+
18+
@Parameter(shortName = 'L')
19+
abstract long simpleLong();
20+
21+
@Parameter(shortName = 'F')
22+
abstract float simpleFloat();
23+
24+
@Parameter(shortName = 'D')
25+
abstract double simpleDouble();
26+
27+
@Parameter(shortName = 'C')
28+
abstract char simpleChar();
29+
30+
}

examples/src/test/java/net/jbock/examples/CustomMapperArgumentsTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ void success() {
3131
"--anInt", "50",
3232
"--notFlag", "true",
3333
"--integerList", "1,2,3,4",
34+
"--optionalInts", "1",
35+
"--optionalInts", "",
36+
"--optionalInts", "3",
37+
"--optionalInts", "4",
3438
"--enumSet", "FOO",
3539
"true", "false", "true",
3640
"--stringArray", "A",
@@ -43,6 +47,8 @@ void success() {
4347
assertEquals(Arrays.asList(true, false, true), parsed.booleanList());
4448
assertEquals(OptionalInt.of(51), parsed.anOptionalInt());
4549
assertEquals(Arrays.asList(1, 2, 3, 4), parsed.integerList().orElseThrow(AssertionFailedError::new));
50+
assertEquals(Arrays.asList(OptionalInt.of(1), OptionalInt.empty(), OptionalInt.of(3), OptionalInt.of(4)),
51+
parsed.optionalInts());
4652
assertEquals(singleton(MyEnum.FOO), parsed.enumSet().orElseThrow(AssertionFailedError::new));
4753
assertArrayEquals(new String[]{"A"}, parsed.stringArray().orElseThrow(AssertionFailedError::new));
4854
assertTrue(parsed.notFlag());
@@ -88,6 +94,8 @@ void testPrint() {
8894
"",
8995
" --enumSet <enum_set>",
9096
"",
97+
" --optionalInts <optional_ints...>",
98+
"",
9199
" --notFlag <NOT_FLAG>",
92100
"",
93101
" --help",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package net.jbock.examples;
2+
3+
import net.jbock.examples.fixture.ParserTestFixture;
4+
import org.junit.jupiter.api.Test;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
class PrimitiveArgumentsTest {
9+
10+
private ParserTestFixture<PrimitiveArguments> f =
11+
ParserTestFixture.create(PrimitiveArguments_Parser.create());
12+
13+
@Test
14+
void simpleTest() {
15+
PrimitiveArguments parsed = f.parse(
16+
"-B", "1",
17+
"-S", "1",
18+
"-I", "1",
19+
"-L", "1",
20+
"-F", "1",
21+
"-D", "1",
22+
"-C", "A");
23+
assertEquals(1, parsed.simpleByte());
24+
assertEquals(1, parsed.simpleShort());
25+
assertEquals(1, parsed.simpleInt());
26+
assertEquals(1, parsed.simpleLong());
27+
assertEquals(1, parsed.simpleFloat());
28+
assertEquals(1, parsed.simpleDouble());
29+
assertEquals('A', parsed.simpleChar());
30+
}
31+
}

0 commit comments

Comments
 (0)