-
Notifications
You must be signed in to change notification settings - Fork 37
Update closure types example #1311
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,55 +1,26 @@ | ||
| /** | ||
| * Configuration shared by every subproject. The Smithy plugin itself is applied per | ||
| * subproject, since each one is configured by its own `smithy-build.json`. | ||
| */ | ||
| subprojects { | ||
| apply(plugin = "java-library") | ||
|
|
||
| plugins { | ||
| `java-library` | ||
| id("software.amazon.smithy.gradle.smithy-base") | ||
| } | ||
|
|
||
| dependencies { | ||
| val smithyJavaVersion: String by project | ||
|
|
||
| smithyBuild("software.amazon.smithy.java:codegen-plugin:$smithyJavaVersion") | ||
| // Combined mode generates a server, so server-api must be on the codegen classpath (the plugin | ||
| // validates it) and on the runtime classpath for the generated server. | ||
| smithyBuild("software.amazon.smithy.java:server-api:$smithyJavaVersion") | ||
| api("software.amazon.smithy.java:server-api:$smithyJavaVersion") | ||
| // The RPC v2 CBOR server protocol the generated service is served with at runtime. | ||
| implementation("software.amazon.smithy.java:server-rpcv2-cbor:$smithyJavaVersion") | ||
|
|
||
| testImplementation("org.hamcrest:hamcrest:3.0") | ||
| testImplementation("org.junit.jupiter:junit-jupiter:6.1.2") | ||
| testRuntimeOnly("org.junit.platform:junit-platform-launcher") | ||
| testImplementation("org.assertj:assertj-core:3.27.7") | ||
| } | ||
|
|
||
| // Add the generated Java sources and resources to the main source set so they compile. | ||
| afterEvaluate { | ||
| val generatedPath = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-codegen").get() | ||
| sourceSets { | ||
| main { | ||
| java { | ||
| srcDir("$generatedPath/java") | ||
| } | ||
| resources { | ||
| srcDir("$generatedPath/resources") | ||
| } | ||
| } | ||
| repositories { | ||
| mavenLocal() | ||
| mavenCentral() | ||
| } | ||
| } | ||
|
|
||
| tasks { | ||
| val smithyBuild by getting | ||
| compileJava { | ||
| dependsOn(smithyBuild) | ||
| the<JavaPluginExtension>().toolchain { | ||
| languageVersion = JavaLanguageVersion.of(21) | ||
| } | ||
| processResources { | ||
| dependsOn(smithyBuild) | ||
|
|
||
| dependencies { | ||
| "testImplementation"("org.junit.jupiter:junit-jupiter:6.1.2") | ||
| "testRuntimeOnly"("org.junit.platform:junit-platform-launcher") | ||
| "testImplementation"("org.assertj:assertj-core:3.27.7") | ||
| } | ||
| withType<Test> { | ||
|
|
||
| tasks.withType<Test> { | ||
| useJUnitPlatform() | ||
| } | ||
| } | ||
|
|
||
| repositories { | ||
| mavenLocal() | ||
| mavenCentral() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /** | ||
| * Generates a BirdWatcher client and nothing else. There is no `closure` setting, so | ||
| * generation is driven by the service in the usual way. A client calls the service and | ||
| * has no reason to know about the events it publishes. | ||
| */ | ||
|
|
||
| plugins { | ||
| id("software.amazon.smithy.gradle.smithy-base") | ||
| } | ||
|
|
||
| dependencies { | ||
| val smithyJavaVersion: String by project | ||
| val smithyVersion: String by project | ||
|
|
||
| smithyBuild("software.amazon.smithy.java:codegen-plugin:$smithyJavaVersion") | ||
| implementation("software.amazon.smithy:smithy-protocol-traits:$smithyVersion") | ||
|
|
||
| // Client mode needs client-core on the codegen classpath and at runtime. | ||
| smithyBuild("software.amazon.smithy.java:client-core:$smithyJavaVersion") | ||
| api("software.amazon.smithy.java:client-core:$smithyJavaVersion") | ||
|
|
||
| implementation("software.amazon.smithy.java:client-rpcv2-cbor:$smithyJavaVersion") | ||
| } | ||
|
|
||
| // Compile the generated sources and package the generated resources, which hold the | ||
| // service file used to discover the schemas at runtime. | ||
| afterEvaluate { | ||
| val generated = smithy.getPluginProjectionPath(smithy.sourceProjection.get(), "java-codegen").get() | ||
| sourceSets["main"].java.srcDir("$generated/java") | ||
| sourceSets["main"].resources.srcDir("$generated/resources") | ||
| } | ||
|
|
||
| tasks.compileJava { dependsOn(tasks.named("smithyBuild")) } | ||
| tasks.processResources { dependsOn(tasks.named("smithyBuild")) } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "version": "1.0", | ||
| "sources": ["../model"], | ||
| "plugins": { | ||
| "java-codegen": { | ||
| "service": "com.example.audubon#BirdWatcher", | ||
| "namespace": "com.example.audubon.client", | ||
| "headerFile": "../license.txt", | ||
| "modes": ["client"] | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
...s/closure-types/client/src/test/java/com/example/audubon/client/ClientGenerationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Example license header. | ||
| * File header line two | ||
| */ | ||
|
|
||
| package com.example.audubon.client; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
|
||
| import com.example.audubon.client.client.BirdWatcherClient; | ||
| import java.lang.reflect.Method; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| /** | ||
| * A client generated from the service alone, with no {@code closure} setting and no | ||
| * {@code types} mode, so it gets the operation shapes and nothing else. | ||
| */ | ||
| class ClientGenerationTest { | ||
|
|
||
| @Test | ||
| void generatesAMethodPerOperation() { | ||
| assertThat(BirdWatcherClient.class.getMethods()) | ||
| .extracting(Method::getName) | ||
| .contains("reportSighting", "getSighting", "listSightings", "withdrawSighting"); | ||
| } | ||
|
|
||
| @Test | ||
| void generatesNoEventTypes() { | ||
| // No operation reaches the events and no closure is set, so they are absent. | ||
| assertThatThrownBy(() -> Class.forName("com.example.audubon.client.model.SightingReported")) | ||
| .isInstanceOf(ClassNotFoundException.class); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * A subscriber, which runs no code generator. It depends on the `types` project for the | ||
| * generated event classes, which is the point of publishing them separately. | ||
| * | ||
| * The project is referenced relative to the parent so this works both standalone and | ||
| * when the example is built inside the smithy-java repository. | ||
| */ | ||
| dependencies { | ||
| val smithyJavaVersion: String by project | ||
|
|
||
| implementation(project(parent!!.path + ":types")) | ||
| implementation("software.amazon.smithy.java:cbor-codec:$smithyJavaVersion") | ||
| } |
74 changes: 74 additions & 0 deletions
74
...closure-types/consumer/src/main/java/com/example/audubon/consumer/BandedBirdNotifier.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* | ||
| * Example license header. | ||
| * File header line two | ||
| */ | ||
|
|
||
| package com.example.audubon.consumer; | ||
|
|
||
| import com.example.audubon.events.model.SightingReported; | ||
| import com.example.audubon.events.model.SightingWithdrawn; | ||
| import java.util.Base64; | ||
| import java.util.function.Consumer; | ||
| import java.util.function.Predicate; | ||
| import software.amazon.smithy.java.cbor.Rpcv2CborCodec; | ||
| import software.amazon.smithy.java.core.serde.Codec; | ||
|
|
||
| /** | ||
| * Notifies an ornithologist when a banded bird is sighted and nobody has read its band | ||
| * yet. | ||
| * | ||
| * <p>Decodes events with the same generated types the service encoded them with, so | ||
| * nothing here parses a map or casts an untyped value. | ||
| */ | ||
| public final class BandedBirdNotifier { | ||
|
|
||
| // Matches the publisher. | ||
| private static final Codec CODEC = Rpcv2CborCodec.builder().build(); | ||
|
|
||
| private final Predicate<String> hasBand; | ||
| private final Consumer<String> sendSms; | ||
|
|
||
| /** | ||
| * @param hasBand decides whether a photo shows a banded bird; a photo can show that | ||
| * a band is present but not what it says, since the code wraps around the leg. | ||
| */ | ||
| public BandedBirdNotifier(Predicate<String> hasBand, Consumer<String> sendSms) { | ||
| this.hasBand = hasBand; | ||
| this.sendSms = sendSms; | ||
| } | ||
|
|
||
| /** | ||
| * Handles one message from the subscription. An SNS filter policy could reject | ||
| * unwanted events before they arrive. | ||
| * | ||
| * @param subject the event's shape name | ||
| * @param message a Base64-encoded CBOR payload | ||
| */ | ||
| public void onMessage(String subject, String message) { | ||
| byte[] payload = Base64.getDecoder().decode(message); | ||
|
|
||
| switch (subject) { | ||
| case "SightingReported" -> onSightingReported( | ||
| CODEC.deserializeShape(payload, SightingReported.builder())); | ||
| case "SightingWithdrawn" -> onSightingWithdrawn( | ||
| CODEC.deserializeShape(payload, SightingWithdrawn.builder())); | ||
| // Ignoring unknown events keeps working when the service adds new ones. | ||
| } | ||
| } | ||
|
|
||
| private void onSightingReported(SightingReported event) { | ||
| // Worth a trip only if a band is visible and nobody has read it yet. | ||
| if (event.getBandCode() != null || event.getPhotoUrl() == null | ||
| || !hasBand.test(event.getPhotoUrl())) { | ||
| return; | ||
| } | ||
|
|
||
| sendSms.accept("Bird " + event.getBirdId() + " sighted at " | ||
| + event.getLocation().getLatitude() + ", " + event.getLocation().getLongitude() | ||
| + " wearing an unread band."); | ||
| } | ||
|
|
||
| private void onSightingWithdrawn(SightingWithdrawn event) { | ||
| sendSms.accept("Sighting " + event.getSightingId() + " was withdrawn."); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.