Skip to content

Commit dc2d3fd

Browse files
committed
feat: implementation of scala fibonacci sample
1 parent f5621bd commit dc2d3fd

4 files changed

Lines changed: 67 additions & 7 deletions

File tree

.circleci/config.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ jobs:
284284
echo "Running mvn with SDK version: '$SDK_VERSION'"
285285
mvn -Dakkaserverless-sdk.version=$SDK_VERSION test-compile
286286
mvn -Dakkaserverless-sdk.version=$SDK_VERSION verify -Pit
287+
- run:
288+
name: Fibonacci Action
289+
command: |
290+
cd samples/scala-fibonacci-action
291+
echo "Running sbt with SDK version: '$SDK_VERSION'"
292+
sbt -Dakkaserverless-sdk.version=$SDK_VERSION test:compile
287293
- save_deps_cache
288294

289295
publish:

codegen/scala-gen/src/main/scala/com/akkaserverless/codegen/scalasdk/impl/SourceGenerator.scala

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ object SourceGenerator {
2626
* Generate the 'managed' code for this model: code that will be regenerated regularly in the 'compile' configuratio
2727
*/
2828
def generateManaged(model: ModelBuilder.Model): Seq[File] = {
29-
Seq(File("foo/bar/AbstractBaz.scala", "package foo.bar\n\nabstract class AbstractBaz")) ++
30-
MainSourceGenerator.generateManaged(model) ++
29+
MainSourceGenerator.generateManaged(model).toSeq ++
3130
model.services.values
3231
.flatMap {
3332
case service: ModelBuilder.EntityService =>
@@ -50,17 +49,14 @@ object SourceGenerator {
5049
/**
5150
* Generate the 'managed' code for this model: code that will be regenerated regularly in the 'compile' configuratio
5251
*/
53-
def generateManagedTest(model: ModelBuilder.Model): Seq[File] =
54-
Seq(File("foo/bar/BazSpec.scala", "package foo.bar\n\nclass BazSpec { new Baz() }"))
55-
.map(_.prepend(managedComment))
52+
def generateManagedTest(model: ModelBuilder.Model): Seq[File] = Seq.empty
5653

5754
/**
5855
* Generate the 'unmanaged' code for this model: code that is generated once on demand and then maintained by the
5956
* user.
6057
*/
6158
def generateUnmanaged(model: ModelBuilder.Model): Seq[File] = {
62-
Seq(File("foo/bar/Baz.scala", "package foo.bar\n\nclass Baz extends AbstractBaz")) ++
63-
MainSourceGenerator.generateUnmanaged(model) ++
59+
MainSourceGenerator.generateUnmanaged(model).toSeq ++
6460
model.services.values
6561
.flatMap {
6662
case service: ModelBuilder.EntityService =>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* This code was generated by Akka Serverless tooling.
2+
* As long as this file exists it will not be re-generated.
3+
* You are free to make changes to this file.
4+
*/
5+
package com.example.fibonacci.fibonacci
6+
7+
import com.akkaserverless.scalasdk.action.Action
8+
import com.akkaserverless.scalasdk.action.ActionCreationContext
9+
10+
/** An action. */
11+
class FibonacciAction(creationContext: ActionCreationContext)
12+
extends AbstractFibonacciAction {
13+
14+
private def isFibonacci(num: Long): Boolean = {
15+
val isPerfectSquare = (n: Long) => {
16+
val square = Math.sqrt(n.toDouble).toLong
17+
square * square == n
18+
}
19+
isPerfectSquare(5 * num * num + 4) || isPerfectSquare(5 * num * num - 4)
20+
}
21+
22+
private def nextFib(num: Long): Long = {
23+
val result = num * (1 + Math.sqrt(5)) / 2.0;
24+
Math.round(result)
25+
}
26+
27+
/** Handler for "NextNumber". */
28+
override def nextNumber(number: Number): Action.Effect[Number] = {
29+
val num = number.value
30+
if (isFibonacci(num))
31+
effects.reply(Number(nextFib(num)))
32+
else
33+
effects.error(s"Input number is not a Fibonacci number, recived '$num'")
34+
}
35+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.fibonacci.fibonacci
2+
3+
import com.akkaserverless.scalasdk.AkkaServerless
4+
import org.slf4j.LoggerFactory
5+
6+
object Main {
7+
8+
private val log = LoggerFactory.getLogger("com.example.fibonacci.fibonacci.Main")
9+
10+
def createAkkaServerless(): AkkaServerless = {
11+
// The AkkaServerlessFactory automatically registers any generated Actions, Views or Entities,
12+
// and is kept up-to-date with any changes in your protobuf definitions.
13+
// If you prefer, you may remove this and manually register these components in a
14+
// `AkkaServerless()` instance.
15+
AkkaServerlessFactory.withComponents(
16+
new FibonacciAction(_))
17+
}
18+
19+
def main(args: Array[String]): Unit = {
20+
log.info("starting the Akka Serverless service")
21+
createAkkaServerless().start()
22+
}
23+
}

0 commit comments

Comments
 (0)