Skip to content

Commit

Permalink
Add flag to exclude generation of async Dialogue interfaces (#2423)
Browse files Browse the repository at this point in the history
Add flag to exclude generation of async Dialogue interfaces
  • Loading branch information
mpritham authored Dec 12, 2024
1 parent a8c1b0d commit 3dec951
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-2423.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: improvement
improvement:
description: Add flag to exclude generation of async Dialogue interfaces
links:
- https://github.com/palantir/conjure-java/pull/2423
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,15 @@ default boolean externalFallbackTypes() {
return false;
}

/**
* Java 24's loom support removes the need for async interfaces. When set to true, only Dialogue interfaces for
* blocking clients will be generated. The Dialogue async interfaces will not be generated.
*/
@Value.Default
default boolean excludeDialogueAsyncInterfaces() {
return false;
}

Optional<String> packagePrefix();

Optional<String> apiVersion();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.palantir.conjure.java.types.TypeMapper;
import com.palantir.conjure.java.util.TypeFunctions;
import com.palantir.conjure.spec.ConjureDefinition;
import com.palantir.conjure.spec.ServiceDefinition;
import com.palantir.conjure.spec.TypeDefinition;
import com.palantir.conjure.spec.TypeName;
import com.palantir.dialogue.BinaryRequestBody;
Expand All @@ -32,6 +33,8 @@
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.SafeArg;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

Expand Down Expand Up @@ -82,13 +85,30 @@ public Stream<JavaFile> generate(ConjureDefinition conjureDefinition) {
StaticFactoryMethodType.BLOCKING);

return conjureDefinition.getServices().stream()
.flatMap(serviceDef -> !serviceDef.getEndpoints().isEmpty()
? Stream.of(
endpoints.endpointsClass(serviceDef),
interfaceGenerator.generateBlocking(serviceDef, blockingGenerator),
interfaceGenerator.generateAsync(serviceDef, asyncGenerator))
: Stream.of(
interfaceGenerator.generateBlocking(serviceDef, blockingGenerator),
interfaceGenerator.generateAsync(serviceDef, asyncGenerator)));
.flatMap(serviceDef -> generateFilesForService(
options.excludeDialogueAsyncInterfaces(),
serviceDef,
endpoints,
interfaceGenerator,
blockingGenerator,
asyncGenerator));
}

private static Stream<JavaFile> generateFilesForService(
boolean excludeDialogueAsyncInterfaces,
ServiceDefinition serviceDef,
DialogueEndpointsGenerator endpointsGenerator,
DialogueInterfaceGenerator interfaceGenerator,
StaticFactoryMethodGenerator blockingGenerator,
StaticFactoryMethodGenerator asyncGenerator) {
List<JavaFile> files = new ArrayList<>(/* initialCapacity= */ 3);
if (!serviceDef.getEndpoints().isEmpty()) {
files.add(endpointsGenerator.endpointsClass(serviceDef));
}
files.add(interfaceGenerator.generateBlocking(serviceDef, blockingGenerator));
if (!excludeDialogueAsyncInterfaces) {
files.add(interfaceGenerator.generateAsync(serviceDef, asyncGenerator));
}
return files.stream();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Locale;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.parallel.Execution;
Expand Down Expand Up @@ -97,11 +98,27 @@ public void generateEteServices() throws IOException {
validateGeneratorOutput(files, Paths.get("src/integrationInput/java/com/palantir/product"));
}

@Test
public void testServiceGeneration_excludeDialogueAsyncInterfaces() {
List<Path> files = getGeneratedFilesForDef(
"example-service",
Options.builder().excludeDialogueAsyncInterfaces(true).build());
List<String> fileNames =
files.stream().map(Path::getFileName).map(Path::toString).toList();
assertThat(fileNames).noneMatch(name -> name.toLowerCase(Locale.ROOT).contains("async"));
}

private void testServiceGeneration(String conjureFile) throws IOException {
validateGeneratorOutput(
getGeneratedFilesForDef(conjureFile, Options.empty()),
Paths.get("src/test/resources/test/api"),
".dialogue");
}

private List<Path> getGeneratedFilesForDef(String conjureFile, Options options) {
ConjureDefinition def = Conjure.parse(ImmutableList.of(new File("src/test/resources/" + conjureFile + ".yml")));
List<Path> files = new GenerationCoordinator(
MoreExecutors.directExecutor(), ImmutableSet.of(new DialogueServiceGenerator(Options.empty())))
return new GenerationCoordinator(
MoreExecutors.directExecutor(), ImmutableSet.of(new DialogueServiceGenerator(options)))
.emit(def, folder);
validateGeneratorOutput(files, Paths.get("src/test/resources/test/api"), ".dialogue");
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ The recommended way to use conjure-java is via a build tool like [gradle-conjure
legacy 'javax' packages.
--externalFallbackTypes
Java external type imports are generated using their fallback type.
--excludeDialogueAsyncInterfaces
Exclude the generation of asynchronous interfaces for Dialogue clients.

### Known Tag Values

Expand Down

0 comments on commit 3dec951

Please sign in to comment.