Skip to content
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

polymorphism support POC #100

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 @@ -96,7 +96,7 @@ private[impl] class ReflectiveEventSourcedEntityRouter[S, E, ES <: EventSourcedE
// the state: S received can either be of the entity "state" type (if coming from emptyState/memory)
// or PB Any type (if coming from the runtime)
state match {
case s if s == null || state.getClass == entityStateType =>
case s if s == null || entityStateType.isAssignableFrom(state.getClass) =>
// note that we set the state even if null, this is needed in order to
// be able to call currentState() later
entity._internalSetCurrentState(s)
Expand Down
2 changes: 1 addition & 1 deletion samples/shopping-cart-quickstart/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>io.akka</groupId>
<artifactId>akka-javasdk-parent</artifactId>
<version>3.0.2</version>
<version>3.0.2-5-898ec6f9-dev-SNAPSHOT</version>
</parent>

<groupId>com.example</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// For actual services meant for production this must be carefully considered, and often set more limited
// tag::endpoint-component-interaction[]
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.INTERNET))
@HttpEndpoint("/carts") // <1>
@HttpEndpoint("/carts-old") // <1>
public class ShoppingCartEndpoint {

private final ComponentClient componentClient;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
// tag::top[]
package shoppingcart.polymorphism.api;

import akka.http.javadsl.model.HttpResponse;
import akka.javasdk.annotations.Acl;
import akka.javasdk.annotations.http.Delete;
import akka.javasdk.annotations.http.Get;
import akka.javasdk.annotations.http.HttpEndpoint;
import akka.javasdk.annotations.http.Post;
import akka.javasdk.annotations.http.Put;
import akka.javasdk.client.ComponentClient;
import akka.javasdk.http.HttpResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shoppingcart.domain.ShoppingCart.LineItem;
import shoppingcart.polymorphism.application.ShoppingCartEntity;
import shoppingcart.polymorphism.domain.ShoppingCart;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.CheckedOutShoppingCartCommand.Cancel;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.AddItem;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.Checkout;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.RemoveItem;

import java.util.concurrent.CompletionStage;

// end::top[]

// tag::class[]

// Opened up for access from the public internet to make the sample service easy to try out.
// For actual services meant for production this must be carefully considered, and often set more limited
// tag::endpoint-component-interaction[]
@Acl(allow = @Acl.Matcher(principal = Acl.Principal.INTERNET))
@HttpEndpoint("/carts") // <1>
public class ShoppingCartEndpoint {

private final ComponentClient componentClient;

private static final Logger logger = LoggerFactory.getLogger(ShoppingCartEndpoint.class);

public ShoppingCartEndpoint(ComponentClient componentClient) { // <2>
this.componentClient = componentClient;
}

// end::class[]

// tag::get[]
@Get("/{cartId}") // <3>
public CompletionStage<ShoppingCart> get(String cartId) {
logger.info("Get cart id={}", cartId);
return componentClient.forEventSourcedEntity(cartId) // <4>
.method(ShoppingCartEntity::getCart)
.invokeAsync(); // <5>
}

// end::get[]

// tag::addItem[]
@Put("/{cartId}/item") // <6>
public CompletionStage<HttpResponse> addItem(String cartId, LineItem item) {
logger.info("Adding item to cart id={} item={}", cartId, item);
return componentClient.forEventSourcedEntity(cartId)
.method(ShoppingCartEntity::handleCommand)
.invokeAsync(new AddItem(item))
.thenApply(__ -> HttpResponses.ok()); // <7>
}
// end::endpoint-component-interaction[]

// end::addItem[]

@Delete("/{cartId}/item/{productId}")
public CompletionStage<HttpResponse> removeItem(String cartId, String productId) {
logger.info("Removing item from cart id={} item={}", cartId, productId);
return componentClient.forEventSourcedEntity(cartId)
.method(ShoppingCartEntity::handleCommand)
.invokeAsync(new RemoveItem(productId))
.thenApply(__ -> HttpResponses.ok());
}

@Post("/{cartId}/checkout")
public CompletionStage<HttpResponse> checkout(String cartId) {
logger.info("Checkout cart id={}", cartId);
return componentClient.forEventSourcedEntity(cartId)
.method(ShoppingCartEntity::handleCommand)
.invokeAsync(new Checkout())
.thenApply(__ -> HttpResponses.ok());
}

@Post("/{cartId}/cancel")
public CompletionStage<HttpResponse> cancel(String cartId) {
logger.info("Checkout cart id={}", cartId);
return componentClient.forEventSourcedEntity(cartId)
.method(ShoppingCartEntity::handleCommand)
.invokeAsync(new Cancel())
.thenApply(__ -> HttpResponses.ok());
}

// tag::class[]
}
// end::class[]
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// tag::top[]
package shoppingcart.polymorphism.application;

import akka.Done;
import akka.javasdk.annotations.ComponentId;
import akka.javasdk.eventsourcedentity.EventSourcedEntity;
import akka.javasdk.eventsourcedentity.EventSourcedEntityContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import shoppingcart.polymorphism.domain.OpenShoppingCart;
import shoppingcart.polymorphism.domain.ShoppingCart;
import shoppingcart.polymorphism.domain.ShoppingCartCommand;
import shoppingcart.polymorphism.domain.ShoppingCartEvent;

import java.util.Collections;


@ComponentId("shopping-cart-poly")
public class ShoppingCartEntity extends EventSourcedEntity<ShoppingCart, ShoppingCartEvent> { // <1>

private final String entityId;

private static final Logger logger = LoggerFactory.getLogger(ShoppingCartEntity.class);

public ShoppingCartEntity(EventSourcedEntityContext context) {
this.entityId = context.entityId(); // <1>
}

@Override
public ShoppingCart emptyState() { // <2>
return new OpenShoppingCart(entityId, Collections.emptyList());
}

public Effect<Done> handleCommand(ShoppingCartCommand command) {
//todo some basic validation
var event = currentState().handleCommand(command);

return effects().persist(event).thenReply(__ -> Done.done());
}

public ReadOnlyEffect<ShoppingCart> getCart() {
return effects().reply(currentState()); // <3>
}


@Override
public ShoppingCart applyEvent(ShoppingCartEvent event) {
return currentState().applyEvent(event);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package shoppingcart.polymorphism.domain;

import shoppingcart.domain.ShoppingCart.LineItem;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.CheckedOutShoppingCartCommand;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.CheckedOutShoppingCartCommand.Cancel;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.CheckedOutShoppingCartEvent;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.CheckedOutShoppingCartEvent.Cancelled;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.OpenShoppingCartEvent;

import java.util.List;

public record CheckedOutShoppingCart(String cartId, List<LineItem> items, Boolean cancelled) implements ShoppingCart {

@Override
public ShoppingCartEvent handleCommand(ShoppingCartCommand command) {
return switch (command) {
case OpenShoppingCartCommand __ -> throw new IllegalStateException("Cannot handle command on a checked out cart");
case CheckedOutShoppingCartCommand checkedOutCommand -> switch (checkedOutCommand) {
case Cancel cancel -> new Cancelled();
};
};
}

@Override
public ShoppingCart applyEvent(ShoppingCartEvent event) {
return switch (event) {
case OpenShoppingCartEvent __ -> {
throw new IllegalStateException("Cannot apply event on a checkout out cart");
}
case CheckedOutShoppingCartEvent checkedOutEvent -> switch (checkedOutEvent) {
case Cancelled cancelled -> new CheckedOutShoppingCart(cartId, items, true);
};
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package shoppingcart.polymorphism.domain;

import shoppingcart.domain.ShoppingCart.LineItem;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.CheckedOutShoppingCartCommand;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.AddItem;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.Checkout;
import shoppingcart.polymorphism.domain.ShoppingCartCommand.OpenShoppingCartCommand.RemoveItem;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.CheckedOutShoppingCartEvent;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.OpenShoppingCartEvent;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.OpenShoppingCartEvent.CheckedOut;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.OpenShoppingCartEvent.ItemAdded;
import shoppingcart.polymorphism.domain.ShoppingCartEvent.OpenShoppingCartEvent.ItemRemoved;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public record OpenShoppingCart(String cartId, List<LineItem> items) implements ShoppingCart {

@Override
public ShoppingCartEvent handleCommand(ShoppingCartCommand command) {
return switch (command) {
case CheckedOutShoppingCartCommand __ -> throw new IllegalStateException("Cannot handle command on an open cart");
case OpenShoppingCartCommand openCommand -> switch (openCommand) {
case AddItem addItem -> new ItemAdded(addItem.item());
case RemoveItem removeItem -> new ItemRemoved(removeItem.productId());
case Checkout checkout -> new CheckedOut();
};
};
}

@Override
public ShoppingCart applyEvent(ShoppingCartEvent event) {
return switch (event) {
case CheckedOutShoppingCartEvent __ -> {
throw new IllegalStateException("Cannot apply event on an open cart");
}
case OpenShoppingCartEvent openEvent -> switch (openEvent) {
case CheckedOut checkedOut -> onCheckedOut();
case ItemAdded itemAdded -> onItemAdded(itemAdded);
case ItemRemoved itemRemoved -> onItemRemoved(itemRemoved);
};
};
}


public OpenShoppingCart onItemAdded(ItemAdded itemAdded) {
var item = itemAdded.item();
var lineItem = updateItem(item); // <1>
List<LineItem> lineItems = removeItemByProductId(item.productId()); // <2>
lineItems.add(lineItem); // <3>
lineItems.sort(Comparator.comparing(LineItem::productId));
return new OpenShoppingCart(cartId, lineItems); // <4>
}

private LineItem updateItem(LineItem item) {
return findItemByProductId(item.productId())
.map(li -> li.withQuantity(li.quantity() + item.quantity()))
.orElse(item);
}

private List<LineItem> removeItemByProductId(String productId) {
return items().stream()
.filter(lineItem -> !lineItem.productId().equals(productId))
.collect(Collectors.toList());
}

public Optional<LineItem> findItemByProductId(String productId) {
Predicate<LineItem> lineItemExists =
lineItem -> lineItem.productId().equals(productId);
return items.stream().filter(lineItemExists).findFirst();
}

public OpenShoppingCart onItemRemoved(ItemRemoved itemRemoved) {
List<LineItem> updatedItems =
removeItemByProductId(itemRemoved.productId());
updatedItems.sort(Comparator.comparing(LineItem::productId));
return new OpenShoppingCart(cartId, updatedItems);
}

public ShoppingCart onCheckedOut() {
return new CheckedOutShoppingCart(cartId, items, false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package shoppingcart.polymorphism.domain;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = OpenShoppingCart.class, name = "open"),
@JsonSubTypes.Type(value = CheckedOutShoppingCart.class, name = "checked-out")})
public sealed interface ShoppingCart permits OpenShoppingCart, CheckedOutShoppingCart {

ShoppingCartEvent handleCommand(ShoppingCartCommand command);

ShoppingCart applyEvent(ShoppingCartEvent event);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package shoppingcart.polymorphism.domain;

import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import shoppingcart.domain.ShoppingCart;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ShoppingCartCommand.OpenShoppingCartCommand.class, name = "open"),
@JsonSubTypes.Type(value = ShoppingCartCommand.CheckedOutShoppingCartCommand.class, name = "checked-out")})
public sealed interface ShoppingCartCommand {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = OpenShoppingCartCommand.AddItem.class, name = "a"),
@JsonSubTypes.Type(value = OpenShoppingCartCommand.RemoveItem.class, name = "r"),
@JsonSubTypes.Type(value = OpenShoppingCartCommand.Checkout.class, name = "c")})
sealed interface OpenShoppingCartCommand extends ShoppingCartCommand {
record AddItem(ShoppingCart.LineItem item) implements OpenShoppingCartCommand {
}

record RemoveItem(String productId) implements OpenShoppingCartCommand {
}

record Checkout() implements OpenShoppingCartCommand {
}
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = CheckedOutShoppingCartCommand.Cancel.class, name = "cc")})
sealed interface CheckedOutShoppingCartCommand extends ShoppingCartCommand {

record Cancel() implements CheckedOutShoppingCartCommand {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package shoppingcart.polymorphism.domain;

import akka.javasdk.annotations.TypeName;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import shoppingcart.domain.ShoppingCart;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type")
@JsonSubTypes({
@JsonSubTypes.Type(value = ShoppingCartEvent.OpenShoppingCartEvent.class, name = "open"),
@JsonSubTypes.Type(value = ShoppingCartEvent.CheckedOutShoppingCartEvent.class, name = "checked-out")})
public sealed interface ShoppingCartEvent {

sealed interface OpenShoppingCartEvent extends ShoppingCartEvent {
@TypeName("item-added") // <2>
record ItemAdded(ShoppingCart.LineItem item) implements OpenShoppingCartEvent {
}

@TypeName("item-removed")
record ItemRemoved(String productId) implements OpenShoppingCartEvent {
}

@TypeName("checked-out")
record CheckedOut() implements OpenShoppingCartEvent {
}
}

sealed interface CheckedOutShoppingCartEvent extends ShoppingCartEvent {
@TypeName("cancelled")
record Cancelled() implements CheckedOutShoppingCartEvent {
}
}
}
Loading