forked from lightbend/kalix-jvm-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(scalasdk): replicated entities examples for Scala (lightbend#613)
- Loading branch information
Showing
30 changed files
with
192 additions
and
84 deletions.
There are no files selected for viewing
34 changes: 0 additions & 34 deletions
34
...d-examples/src/main/scala/com/example/replicated/registermap/domain/SomeRegisterMap.scala
This file was deleted.
Oops, something went wrong.
File renamed without changes.
This file contains 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains 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 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 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 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 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
73 changes: 73 additions & 0 deletions
73
...y-examples/src/main/scala/com/example/replicated/registermap/domain/SomeRegisterMap.scala
This file contains 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,73 @@ | ||
package com.example.replicated.registermap.domain | ||
|
||
import com.akkaserverless.scalasdk.replicatedentity.ReplicatedEntity | ||
import com.akkaserverless.scalasdk.replicatedentity.ReplicatedEntityContext | ||
import com.akkaserverless.scalasdk.replicatedentity.ReplicatedRegisterMap | ||
import com.example.replicated.registermap | ||
import com.google.protobuf.empty.Empty | ||
|
||
// This class was initially generated based on the .proto definition by Akka Serverless tooling. | ||
// | ||
// As long as this file exists it will not be overwritten: you can maintain it yourself, | ||
// or delete it so it is regenerated as needed. | ||
|
||
/** A replicated entity. */ | ||
class SomeRegisterMap(context: ReplicatedEntityContext) extends AbstractSomeRegisterMap { | ||
|
||
/** Command handler for "Set". */ | ||
def set(currentData: ReplicatedRegisterMap[SomeKey, SomeValue], setValue: registermap.SetValue): ReplicatedEntity.Effect[Empty] = { | ||
|
||
val keyValue = | ||
for { | ||
key <- setValue.key.map(_.field) | ||
value <- setValue.value.map(_.field) | ||
} yield (key, value) | ||
|
||
keyValue.map { case (key, value) => | ||
effects | ||
.update(currentData.setValue(SomeKey(key), SomeValue(value))) | ||
.thenReply(Empty.defaultInstance) | ||
} | ||
.getOrElse { | ||
effects | ||
.error(s"Invalid input: received key = '${setValue.key}' and value = '${setValue.value}'") | ||
} | ||
|
||
} | ||
|
||
/** Command handler for "Remove". */ | ||
def remove(currentData: ReplicatedRegisterMap[SomeKey, SomeValue], removeValue: registermap.RemoveValue): ReplicatedEntity.Effect[Empty] = | ||
removeValue.key.map { key => | ||
effects | ||
.update(currentData.remove(SomeKey(key.field))) | ||
.thenReply(Empty.defaultInstance) | ||
} | ||
.getOrElse { | ||
effects.error("Invalid input: key is empty") | ||
} | ||
|
||
|
||
/** Command handler for "Get". */ | ||
def get(currentData: ReplicatedRegisterMap[SomeKey, SomeValue], getValue: registermap.GetValue): ReplicatedEntity.Effect[registermap.CurrentValue] = | ||
getValue.key.map { key => | ||
val value = currentData.get(SomeKey(key.field)).map(v => registermap.Value(v.someField)) | ||
effects | ||
.reply(registermap.CurrentValue(getValue.key, value)) | ||
} | ||
.getOrElse { | ||
effects.error("The command handler for `Get` is not implemented, yet") | ||
} | ||
|
||
/** Command handler for "GetAll". */ | ||
def getAll(currentData: ReplicatedRegisterMap[SomeKey, SomeValue], getAllValues: registermap.GetAllValues): ReplicatedEntity.Effect[registermap.CurrentValues] = { | ||
|
||
val allData = | ||
currentData.keySet.map { key => | ||
val value = currentData.get(key).map(v => registermap.Value(v.someField)) | ||
registermap.CurrentValue(Some(registermap.Key(key.someField)), value) | ||
}.toSeq | ||
|
||
effects.reply(registermap.CurrentValues(allData)) | ||
} | ||
|
||
} |
This file contains 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
File renamed without changes.