Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -144,17 +144,24 @@ object ModelBuilder {
inputType: FullyQualifiedName,
outputType: FullyQualifiedName,
streamedInput: Boolean,
streamedOutput: Boolean
streamedOutput: Boolean,
inFromTopic: Boolean,
outToTopic: Boolean
)

object Command {
def from(method: Descriptors.MethodDescriptor): Command = Command(
FullyQualifiedName.from(method),
FullyQualifiedName.from(method.getInputType),
FullyQualifiedName.from(method.getOutputType),
streamedInput = method.isClientStreaming,
streamedOutput = method.isServerStreaming
)
def from(method: Descriptors.MethodDescriptor): Command = {
val eventing = method.getOptions.getExtension(com.akkaserverless.Annotations.method).getEventing
Command(
FullyQualifiedName.from(method),
FullyQualifiedName.from(method.getInputType),
FullyQualifiedName.from(method.getOutputType),
streamedInput = method.isClientStreaming,
streamedOutput = method.isServerStreaming,
inFromTopic = eventing.hasIn && eventing.getIn.hasTopic,
outToTopic = eventing.hasOut && eventing.getOut.hasTopic
)
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class ModelBuilderSuite extends munit.FunSuite {
override def error(message: String): Unit = log.error(message)
}

def command(
fqn: FullyQualifiedName,
inputType: FullyQualifiedName,
outputType: FullyQualifiedName,
streamedInput: Boolean = false,
streamedOutput: Boolean = false,
inFromTopic: Boolean = false,
outToTopic: Boolean = false
) = ModelBuilder.Command(fqn, inputType, outputType, streamedInput, streamedOutput, inFromTopic, outToTopic)

test("EventSourcedEntity introspection") {
val testFilesPath = Paths.get(getClass.getClassLoader.getResource("test-files").toURI)
val descriptorFilePath =
Expand Down Expand Up @@ -110,26 +120,20 @@ class ModelBuilderSuite extends munit.FunSuite {
ModelBuilder.EntityService(
FullyQualifiedName("ShoppingCartService", shoppingCartProto),
List(
ModelBuilder.Command(
command(
FullyQualifiedName("AddItem", shoppingCartProto),
FullyQualifiedName("AddLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("RemoveItem", shoppingCartProto),
FullyQualifiedName("RemoveLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("GetCart", shoppingCartProto),
FullyQualifiedName("GetShoppingCart", shoppingCartProto),
FullyQualifiedName("Cart", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Cart", shoppingCartProto)
)
),
entity.fqn.fullQualifiedName
Expand Down Expand Up @@ -205,33 +209,25 @@ class ModelBuilderSuite extends munit.FunSuite {
ModelBuilder.EntityService(
FullyQualifiedName("ShoppingCartService", shoppingCartProto),
List(
ModelBuilder.Command(
command(
FullyQualifiedName("AddItem", shoppingCartProto),
FullyQualifiedName("AddLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("RemoveItem", shoppingCartProto),
FullyQualifiedName("RemoveLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("GetCart", shoppingCartProto),
FullyQualifiedName("GetShoppingCart", shoppingCartProto),
FullyQualifiedName("Cart", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Cart", shoppingCartProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("RemoveCart", shoppingCartProto),
FullyQualifiedName("RemoveShoppingCart", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
)
),
entity.fqn.fullQualifiedName
Expand Down Expand Up @@ -300,34 +296,27 @@ class ModelBuilderSuite extends munit.FunSuite {

val transformedUpdates =
List(
ModelBuilder.Command(
command(
FullyQualifiedName("ProcessAdded", shoppingCartProto),
FullyQualifiedName("ItemAdded", domainProto),
FullyQualifiedName("CartViewState", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("CartViewState", shoppingCartProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("ProcessRemoved", shoppingCartProto),
FullyQualifiedName("ItemRemoved", domainProto),
FullyQualifiedName("CartViewState", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("CartViewState", shoppingCartProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("ProcessCheckedOut", shoppingCartProto),
FullyQualifiedName("CheckedOut", domainProto),
FullyQualifiedName("CartViewState", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("CartViewState", shoppingCartProto)
)
)
val queries = List(
ModelBuilder.Command(
command(
FullyQualifiedName("GetCheckedOutCarts", shoppingCartProto),
FullyQualifiedName("GetCheckedOutCartsRequest", shoppingCartProto),
FullyQualifiedName("CartViewState", shoppingCartProto),
streamedInput = false,
streamedOutput = true
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,21 @@ object ActionServiceSourceGenerator {
val outputType = cmd.outputType.fullName

if (isUnary(cmd)) {
val jsonTopicHint = {
// note: the somewhat funky indenting is on purpose to lf+indent only if comment present
if (cmd.inFromTopic && cmd.inputType.fullQualifiedName == "com.google.protobuf.Any")
"""|// JSON input from a topic can be decoded using JsonSupport.decodeJson(MyClass.class, any)
| """.stripMargin
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

else if (cmd.outToTopic && cmd.outputType.fullQualifiedName == "com.google.protobuf.Any")
"""|// JSON output to emit to a topic can be encoded using JsonSupport.encodeJson(myPojo)
| """.stripMargin
else ""
}

s"""|/** Handler for "$methodName". */
|@Override
|public Effect<$outputType> ${lowerFirst(methodName)}($inputTypeFullName $input) {
| throw new RuntimeException("The command handler for `$methodName` is not implemented, yet");
| ${jsonTopicHint}throw new RuntimeException("The command handler for `$methodName` is not implemented, yet");
|}""".stripMargin
} else if (isStreamOut(cmd)) {
s"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,4 +488,48 @@ class ActionServiceSourceGeneratorSuite extends munit.FunSuite {
|}""".stripMargin
)
}

test("Action with pub/sub source generation") {

val service = TestData.simpleJsonPubSubActionService()

val generatedSrc =
ActionServiceSourceGenerator.actionSource(service)
assertEquals(
generatedSrc,
"""/* This code was generated by Akka Serverless tooling.
| * As long as this file exists it will not be re-generated.
| * You are free to make changes to this file.
| */
|
|package com.example.service;
|
|import akka.NotUsed;
|import akka.stream.javadsl.Source;
|import com.akkaserverless.javasdk.action.ActionCreationContext;
|import com.example.service.persistence.EntityOuterClass;
|import com.google.protobuf.Any;
|import com.google.protobuf.Empty;
|import java.util.concurrent.CompletionStage;
|
|/** An action. */
|public class MyServiceAction extends AbstractMyServiceAction {
|
| public MyServiceAction(ActionCreationContext creationContext) {}
|
| /** Handler for "InFromTopic". */
| @Override
| public Effect<Empty> inFromTopic(Any any) {
| // JSON input from a topic can be decoded using JsonSupport.decodeJson(MyClass.class, any)
| throw new RuntimeException("The command handler for `InFromTopic` is not implemented, yet");
| }
| /** Handler for "OutToTopic". */
| @Override
| public Effect<Any> outToTopic(EntityOuterClass.EntityUpdated entityUpdated) {
| // JSON output to emit to a topic can be encoded using JsonSupport.encodeJson(myPojo)
| throw new RuntimeException("The command handler for `OutToTopic` is not implemented, yet");
| }
|}""".stripMargin
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.lightbend.akkasls.codegen
package java

import com.lightbend.akkasls.codegen.java.TestData.command

class EventSourcedEntityTestKitGeneratorSuite extends munit.FunSuite {

test(
Expand Down Expand Up @@ -181,26 +183,20 @@ class EventSourcedEntityTestKitGeneratorSuite extends munit.FunSuite {
ModelBuilder.EntityService(
FullyQualifiedName("ShoppingCartService", shoppingCartProto),
List(
ModelBuilder.Command(
command(
FullyQualifiedName("AddItem", shoppingCartProto),
FullyQualifiedName("AddLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("RemoveItem", shoppingCartProto),
FullyQualifiedName("RemoveLineItem", shoppingCartProto),
FullyQualifiedName("Empty", googleEmptyProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Empty", googleEmptyProto)
),
ModelBuilder.Command(
command(
FullyQualifiedName("GetCart", shoppingCartProto),
FullyQualifiedName("GetShoppingCart", shoppingCartProto),
FullyQualifiedName("Cart", shoppingCartProto),
streamedInput = false,
streamedOutput = false
FullyQualifiedName("Cart", shoppingCartProto)
)
),
entity.fqn.fullName
Expand Down
Loading