Skip to content

Commit

Permalink
use either 1.2, stop relocating either into the compiler jar
Browse files Browse the repository at this point in the history
  • Loading branch information
h908714124 committed Jul 9, 2021
1 parent 6733135 commit 0564983
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 18 deletions.
6 changes: 2 additions & 4 deletions compiler/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ tasks.withType(GenerateModuleMetadata) {
shadowJar {
minimize()
dependencies {
exclude('io.github.jbock-java:jbock:.*')
exclude('javax.annotation:jsr250-api:.*')
}
archiveClassifier.set('')
relocate('io.jbock.util', 'net.jbock.io.jbock.util')
relocate('dagger', 'net.jbock.dagger')
relocate('com.squareup.javapoet', 'net.jbock.javapoet')
relocate('com.google', 'net.jbock.google')
Expand All @@ -50,11 +48,11 @@ shadowJar {
}

dependencies {
def core = 'io.github.jbock-java:jbock:5.3'
implementation('io.github.jbock-java:either:1.1')
def core = project(':core')
implementation('com.squareup:javapoet:1.13.0')
implementation('com.google.auto:auto-common:1.0')
implementation('com.google.guava:guava:30.1.1-jre')
shadow('io.github.jbock-java:either:1.2')
shadow(core)
implementation('com.google.dagger:dagger:2.36')
annotationProcessor('com.google.dagger:dagger-compiler:2.36')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.DeclaredType;
import javax.lang.model.type.TypeMirror;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static io.jbock.util.Either.left;
import static net.jbock.common.Constants.STRING;
Expand Down Expand Up @@ -66,6 +69,14 @@ private <P extends AbstractItem> Either<String, Mapped<P>> findConverter(Match m
}

private Either<String, MapExpr> enumConverter(TypeMirror baseType) {
Either<String, BigInteger> e = Stream.generate(() -> ThreadLocalRandom.current().nextInt(1000))
.map(BigInteger::valueOf)
.limit(10)
.filter(n -> n.isProbablePrime(10))
.findAny()
.<Either<String, BigInteger>>map(Either::right)
.orElseGet(() -> left("my Left value"));

return asEnumType(baseType)
.map(TypeElement::asType)
.<Either<String, TypeMirror>>map(Either::right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ public Set<? extends Element> process(ImmutableSetMultimap<String, Element> elem
elementsByAnnotation.forEach((annotationName, element) ->
ElementFilter.typesIn(List.of(element)).stream()
.map(this::validateSourceElement)
.forEach(either -> either.accept(this::printFailures, this::processSourceElement)));
.forEach(either -> either.ifPresentOrElse(
this::printFailures,
this::processSourceElement)));
return Set.of();
}

Expand All @@ -88,8 +90,9 @@ private void processSourceElement(SourceElement sourceElement) {
.module(new ValidateModule(types, elements))
.create();
component.processor().generate()
.accept(this::printFailures, type ->
sourceFileGenerator.write(sourceElement, type));
.ifPresentOrElse(
this::printFailures,
type -> sourceFileGenerator.write(sourceElement, type));
}

private Either<List<ValidationFailure>, SourceElement> validateSourceElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private Either<List<ValidationFailure>, Items> createNamedOptions(
.build()
.namedOptionFactory()
.createNamedOption()
.accept(failures::add, namedOptions::add);
.ifPresentOrElse(failures::add, namedOptions::add);
}
if (!failures.isEmpty()) {
return left(failures);
Expand All @@ -90,7 +90,7 @@ private Either<List<ValidationFailure>, IntermediateResult> createPositionalPara
.build()
.positionalParameterFactory()
.createPositionalParam(sourceMethod.index().orElse(methods.positionalParameters().size() - 1))
.accept(failures::add, positionalParams::add);
.ifPresentOrElse(failures::add, positionalParams::add);
}
if (!failures.isEmpty()) {
return left(failures);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ void testTypecheckSuccess() {
TypeElement typeElement = elements.getTypeElement("test.Foo");
ReferenceTool referenceTool = new ReferenceTool(tool);
Either<String, StringConverterType> result = referenceTool.getReferencedType(typeElement);
result.accept(l -> Assertions.fail(), functionType -> Assertions.assertTrue(functionType.isSupplier()));
result.ifPresentOrElse(l -> Assertions.fail(), functionType -> Assertions.assertTrue(functionType.isSupplier()));
});
}
}
2 changes: 1 addition & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ tasks.withType(GenerateModuleMetadata) {
}

dependencies {
api('io.github.jbock-java:either:1.1')
api('io.github.jbock-java:either:1.2')
testImplementation('org.junit.jupiter:junit-jupiter:5.7.1')
}

Expand Down
2 changes: 1 addition & 1 deletion examples/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ compileJava {
dependencies {
def core = project(':core')
implementation(core)
implementation('io.github.jbock-java:either:1.1')
implementation('io.github.jbock-java:either:1.2')
implementation 'io.vavr:vavr:1.0.0-alpha-3'
annotationProcessor project(':compiler')
annotationProcessor(core)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ void testAbsent() {
ParseRequest request = ParseRequest.simple(List.of()).withHelpRequested(true).build();
Either<NotSuccess, ListIntegerArguments> result = parser.parse(request);
assertTrue(result.isLeft());
result.acceptLeft(l -> assertTrue(l instanceof HelpRequested));
result.getLeft().ifPresent(l -> assertTrue(l instanceof HelpRequested));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ void testAbsent() {
ParseRequest request = ParseRequest.simple(List.of()).withHelpRequested(true).build();
Either<NotSuccess, OptionalIntArguments> result = parser.parse(request);
assertTrue(result.isLeft());
result.acceptLeft(l -> assertTrue(l instanceof HelpRequested));
result.getLeft().ifPresent(l -> assertTrue(l instanceof HelpRequested));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void errorDirMissing() {
ParseRequest request = ParseRequest.simple(List.of()).withHelpRequested(true).build();
Either<NotSuccess, RequiredArguments> result = new RequiredArgumentsParser().parse(request);
Assertions.assertTrue(result.isLeft());
result.acceptLeft(l -> assertTrue(l instanceof HelpRequested));
result.getLeft().ifPresent(l -> assertTrue(l instanceof HelpRequested));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void assertPrintsHelp(Map<String, String> messages, String... expected) {
.build();
Either<NotSuccess, E> result = parser.apply(request);
assertTrue(result.isLeft());
result.acceptLeft(l -> {
result.getLeft().ifPresent(l -> {
String[] actual = getUsageDocumentation(l, messages);
assertArraysEquals(expected, actual);
});
Expand Down Expand Up @@ -132,7 +132,7 @@ Either<NotSuccess, E> getParsed() {
public <V> AssertionBuilder<E> has(Function<E, V> getter, V expectation) {
Either<NotSuccess, E> parsed = getParsed();
assertTrue(parsed.isRight(), "Parsing was not successful");
parsed.acceptRight(r -> {
parsed.getRight().ifPresent(r -> {
V result = getter.apply(r);
Assertions.assertEquals(expectation, result);
});
Expand All @@ -147,7 +147,8 @@ private void fails(Predicate<String> messageTest) {
Either<NotSuccess, E> result = getParsed();
assertTrue(result.isLeft());
result.mapLeft(HasMessage.class::cast)
.acceptLeft(hasMessage -> {
.getLeft()
.ifPresent(hasMessage -> {
boolean success = messageTest.test(hasMessage.message());
if (!success) {
Assertions.fail("Assertion failed, message: " + hasMessage.message());
Expand Down

0 comments on commit 0564983

Please sign in to comment.