Skip to content

Commit

Permalink
Bump up ZIO to 2.0.0-RC5 and zio-json to 0.3.0-RC7 (#98)
Browse files Browse the repository at this point in the history
* Bump up ZIO to 2.0.0-RC5 and zio-json to 0.3.0-RC7

* Fix 2.12.x compilation error
  • Loading branch information
jrmsamson authored Apr 21, 2022
1 parent de6d54b commit 1c53576
Show file tree
Hide file tree
Showing 22 changed files with 107 additions and 110 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ inThisBuild(
)
)

val zioVersion = "2.0.0-RC3"
val zioJsonVersion = "0.3.0-RC4"
val zioVersion = "2.0.0-RC5"
val zioJsonVersion = "0.3.0-RC7"
val awsLambdaJavaTests = "1.1.1"

lazy val root =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package zio.lambda.event

import com.amazonaws.services.lambda.runtime.events.{KinesisEvent => JavaKinesisEvent}
import com.amazonaws.services.lambda.runtime.events.{ScheduledEvent => JavaScheduledEvent}
import com.amazonaws.services.lambda.runtime.events.models.kinesis.EncryptionType
import com.amazonaws.services.lambda.runtime.events.{KafkaEvent => JavaKafkaEvent}
import com.amazonaws.services.lambda.runtime.events.{KinesisEvent => JavaKinesisEvent}
import com.amazonaws.services.lambda.runtime.events.{SQSEvent => JavaSQSEvent}
import com.amazonaws.services.lambda.runtime.events.models.kinesis.EncryptionType
import com.amazonaws.services.lambda.runtime.events.{ScheduledEvent => JavaScheduledEvent}
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import zio.Random
import zio.test._

import java.nio.ByteBuffer
import java.time.Instant
import java.time.OffsetDateTime
import java.time.temporal.ChronoUnit
import java.util.Date
import java.util.TimeZone
import scala.jdk.CollectionConverters._
import java.time.OffsetDateTime

object JavaLambdaEventsGen {

Expand Down Expand Up @@ -65,14 +64,14 @@ object JavaLambdaEventsGen {
record
}

val genKinesisEvent: Gen[Random with Sized, JavaKinesisEvent] =
val genKinesisEvent: Gen[Sized, JavaKinesisEvent] =
Gen.listOf1(genKinesisEventRecord).map { records =>
val kinesisEvent = new JavaKinesisEvent()
kinesisEvent.setRecords(records.asJava)
kinesisEvent
}

val genScheduledEvent: Gen[Random with Sized, JavaScheduledEvent] =
val genScheduledEvent: Gen[Sized, JavaScheduledEvent] =
for {
account <- Gen.string
region <- Gen.string
Expand Down Expand Up @@ -129,7 +128,7 @@ object JavaLambdaEventsGen {
.withValue(value)
.build()

val genKafkaEvent: Gen[Random with Sized, JavaKafkaEvent] =
val genKafkaEvent: Gen[Sized, JavaKafkaEvent] =
for {
eventSource <- Gen.string
eventSourceArn <- Gen.string
Expand Down Expand Up @@ -181,7 +180,7 @@ object JavaLambdaEventsGen {
sqsMessage
}

val genSQSEvent: Gen[Random with Sized, JavaSQSEvent] =
val genSQSEvent: Gen[Sized, JavaSQSEvent] =
Gen.listOf(genSQSEventRecord).map { records =>
val sqsEvent = new JavaSQSEvent()
sqsEvent.setRecords(records.asJava)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import zio.json._
import zio.test.Assertion._
import zio.test._

object KafkaEventSpec extends DefaultRunnableSpec {
object KafkaEventSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Failure] =
override def spec =
suite("KafkaEvent spec")(
test("should decode Kafka JSON") {
check(JavaLambdaEventsGen.genKafkaEvent) { kafkaEvent =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package zio.lambda.event

import zio.test._
import zio.test.Assertion._
import zio.json._
import zio.test.Assertion._
import zio.test._

object KinesisEventSpec extends DefaultRunnableSpec {
object KinesisEventSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Failure] =
override def spec =
suite("KinesisEvent spec")(
test("should decode Kinesis JSON") {
check(JavaLambdaEventsGen.genKinesisEvent) { kinesisEvent =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package zio.lambda.event

import zio.test._
import zio.test.Assertion._
import zio.json._
import zio.test.Assertion._
import zio.test._

object SQSEventSpec extends DefaultRunnableSpec {
object SQSEventSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Failure] =
override def spec =
suite("SQSEvent spec")(
test("should decode SQS JSON") {
check(JavaLambdaEventsGen.genSQSEvent) { sqsEvent =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package zio.lambda.event

import zio.test._
import zio.test.Assertion._
import zio.json._
import zio.test.Assertion._
import zio.test._

object ScheduledEventSpec extends DefaultRunnableSpec {
object ScheduledEventSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Failure] =
override def spec =
suite("ScheduledEvent spec")(
test("should decode Scheduled JSON") {
check(JavaLambdaEventsGen.genScheduledEvent) { scheduledEvent =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import zio.lambda._

object SimpleHandler extends ZLambda[CustomEvent, String] {

override def apply(event: CustomEvent, context: Context): RIO[ZEnv, String] =
override def apply(event: CustomEvent, context: Context): Task[String] =
for {
_ <- printLine(event.message)
} yield "Handler ran successfully"
Expand Down
6 changes: 3 additions & 3 deletions lambda/src/main/scala/zio/lambda/ZLambda.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ abstract class ZLambda[E, A](
implicit val lambdaResponseEncoder: JsonEncoder[A]
) extends ZIOAppDefault { self =>

def apply(event: E, context: Context): RIO[ZEnv, A]
def apply(event: E, context: Context): Task[A]

def applyJson(json: String, context: Context): RIO[ZEnv, String] =
def applyJson(json: String, context: Context): Task[String] =
lambdaEventDecoder.decodeJson(json) match {
case Left(errorMessage) =>
ZIO.fail(new Throwable(s"Error decoding json. Json=$json, Error$errorMessage"))
Expand All @@ -24,7 +24,7 @@ abstract class ZLambda[E, A](
def run =
LoopProcessor
.loop(Right(self))
.provideCustom(
.provide(
LambdaEnvironment.live,
RuntimeApiLive.layer,
LoopProcessor.live
Expand Down
29 changes: 14 additions & 15 deletions lambda/src/main/scala/zio/lambda/internal/CustomClassLoader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ trait CustomClassLoader {
}

object CustomClassLoader {
val live: URLayer[LambdaEnvironment, CustomClassLoader] = (
(environment: LambdaEnvironment) =>
new CustomClassLoader {
override def getClassLoader: Task[ClassLoader] =
ZStream
.fromJavaStream(Files.list(Paths.get(environment.taskRoot)))
.runCollect
.map(stream =>
new URLClassLoader(
stream
.map(_.toUri().toURL())
.toArray
)
val live: URLayer[LambdaEnvironment, CustomClassLoader] = ZLayer.fromFunction { (environment: LambdaEnvironment) =>
new CustomClassLoader {
override def getClassLoader: Task[ClassLoader] =
ZStream
.fromJavaStream(Files.list(Paths.get(environment.taskRoot)))
.runCollect
.map(stream =>
new URLClassLoader(
stream
.map(_.toUri().toURL())
.toArray
)
}
).toLayer
)
}
}

def getClassLoader: ZIO[CustomClassLoader, Throwable, ClassLoader] =
ZIO.serviceWithZIO(_.getClassLoader)
Expand Down
47 changes: 24 additions & 23 deletions lambda/src/main/scala/zio/lambda/internal/LambdaEnvironment.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package zio.lambda.internal

import zio._
import zio.System._

final case class LambdaEnvironment(
runtimeApi: String,
Expand All @@ -15,28 +14,30 @@ final case class LambdaEnvironment(
)

object LambdaEnvironment {
val live: ZLayer[System, Throwable, LambdaEnvironment] =
(for {
runtimeApi <-
env("AWS_LAMBDA_RUNTIME_API")
.someOrFail(new Throwable("AWS_LAMBDA_RUNTIME_API env variable not defined"))
val live: TaskLayer[LambdaEnvironment] =
ZLayer {
for {
runtimeApi <- ZIO.system
.flatMap(_.env("AWS_LAMBDA_RUNTIME_API"))
.someOrFail(new Throwable("AWS_LAMBDA_RUNTIME_API env variable not defined"))
handler <- ZIO.system.flatMap(_.envOrElse("_HANDLER", ""))
taskRoot <- ZIO.system.flatMap(_.envOrElse("LAMBDA_TASK_ROOT", ""))
memoryLimitInMB <- ZIO.system.flatMap(_.envOrElse("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128"))
logGroupName <- ZIO.system.flatMap(_.envOrElse("AWS_LAMBDA_LOG_GROUP_NAME", ""))
logStreamName <- ZIO.system.flatMap(_.envOrElse("AWS_LAMBDA_LOG_STREAM_NAME", ""))
functionName <- ZIO.system.flatMap(_.envOrElse("AWS_LAMBDA_FUNCTION_NAME", ""))
functionVersion <- ZIO.system.flatMap(_.envOrElse("AWS_LAMBDA_FUNCTION_VERSION", ""))

handler <- envOrElse("_HANDLER", "")
taskRoot <- envOrElse("LAMBDA_TASK_ROOT", "")
memoryLimitInMB <- envOrElse("AWS_LAMBDA_FUNCTION_MEMORY_SIZE", "128")
logGroupName <- envOrElse("AWS_LAMBDA_LOG_GROUP_NAME", "")
logStreamName <- envOrElse("AWS_LAMBDA_LOG_STREAM_NAME", "")
functionName <- envOrElse("AWS_LAMBDA_FUNCTION_NAME", "")
functionVersion <- envOrElse("AWS_LAMBDA_FUNCTION_VERSION", "")
} yield LambdaEnvironment(
runtimeApi,
handler,
taskRoot,
memoryLimitInMB.toInt,
logGroupName,
logStreamName,
functionName,
functionVersion
)).toLayer
} yield LambdaEnvironment(
runtimeApi,
handler,
taskRoot,
memoryLimitInMB.toInt,
logGroupName,
logStreamName,
functionName,
functionVersion
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@ final case class LambdaLoaderLive(

object LambdaLoaderLive {
val layer: ZLayer[CustomClassLoader with LambdaEnvironment, Throwable, LambdaLoader] =
(LambdaLoaderLive(_, _)).toLayer
ZLayer.fromFunction(LambdaLoaderLive.apply _)
}
18 changes: 10 additions & 8 deletions lambda/src/main/scala/zio/lambda/internal/LoopProcessor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import zio.lambda.Context
import zio.lambda.ZLambda

trait LoopProcessor {
def loop(eitherZLambda: Either[Throwable, ZLambda[_, _]]): RIO[ZEnv, Unit]
def loop(eitherZLambda: Either[Throwable, ZLambda[_, _]]): Task[Unit]
}

object LoopProcessor {

final case class Live(runtimeApi: RuntimeApi, environment: LambdaEnvironment) extends LoopProcessor {
def loop(eitherZLambda: Either[Throwable, ZLambda[_, _]]): RIO[ZEnv, Unit] =
def loop(eitherZLambda: Either[Throwable, ZLambda[_, _]]): Task[Unit] =
eitherZLambda match {
case Right(zLambda) =>
runtimeApi.getNextInvocation
.flatMap(request =>
zLambda
.applyJson(request.payload, Context.from(request, environment))
.foldZIO(
.foldZIO[Any, Throwable, Unit](
throwable =>
runtimeApi.sendInvocationError(
InvocationError(request.id, InvocationErrorResponse.fromThrowable(throwable))
Expand Down Expand Up @@ -47,13 +47,15 @@ object LoopProcessor {

def loop(
eitherZLambda: Either[Throwable, ZLambda[_, _]]
): RIO[LoopProcessor with ZEnv, Unit] =
): RIO[LoopProcessor, Unit] =
ZIO.serviceWithZIO[LoopProcessor](_.loop(eitherZLambda))

val live: ZLayer[RuntimeApi with LambdaEnvironment, Throwable, LoopProcessor] =
(for {
runtimeApi <- ZIO.service[RuntimeApi]
environment <- ZIO.service[LambdaEnvironment]
} yield Live(runtimeApi, environment)).toLayer
ZLayer {
for {
runtimeApi <- ZIO.service[RuntimeApi]
environment <- ZIO.service[LambdaEnvironment]
} yield Live(runtimeApi, environment)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ final case class RuntimeApiLive(environment: LambdaEnvironment) extends RuntimeA

object RuntimeApiLive {
val layer: ZLayer[LambdaEnvironment, Throwable, RuntimeApi] =
ZIO
.service[LambdaEnvironment]
.map(RuntimeApiLive(_))
.toLayer
ZLayer.fromFunction(RuntimeApiLive.apply _)

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ object ZLambdaReflectiveApp extends ZIOAppDefault {
InvocationErrorResponse.fromThrowable(throwable)
)
)
.provideCustom(
.provide(
LambdaEnvironment.live,
CustomClassLoader.live,
LambdaLoaderLive.layer,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package zio.lambda.internal

import zio.Random
import zio.test._

object InvocationErrorGen {
Expand All @@ -16,7 +15,7 @@ object InvocationErrorGen {
stackTrace
)

val gen: Gen[Random with Sized, InvocationError] =
val gen: Gen[Sized, InvocationError] =
for {
requestId <- Gen.string
invocationErrorResponse <- genInvocationErrorResponse
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package zio.lambda.internal

import zio.Random
import zio.test._
import zio.lambda.ClientContext
import zio.lambda.CognitoIdentity
Expand Down Expand Up @@ -43,7 +42,7 @@ object InvocationRequestGen {
cognitoIdentityPoolId = cognitoIdentityPoolId
)

val gen: Gen[Random with Sized, InvocationRequest] =
val gen: Gen[Sized, InvocationRequest] =
for {
id <- Gen.string
remainingTimeInMillis <- Gen.long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import zio.test._

import InvocationRequestImplicits._

object InvocationRequestSpec extends DefaultRunnableSpec {
object InvocationRequestSpec extends ZIOSpecDefault {

override def spec: ZSpec[Environment, Failure] =
override def spec =
suite("InvocationRequest spec")(
suite("fromHttpResponse")(
test("should return InvocationRequest") {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package zio.lambda.internal

import zio.Random
import zio.test._

object LambdaEnvironmentGen {

val gen: Gen[Random with Sized, LambdaEnvironment] =
val gen: Gen[Sized, LambdaEnvironment] =
for {
runtimeApi <- Gen.string(Gen.char)
handler <- Gen.string
Expand Down
Loading

0 comments on commit 1c53576

Please sign in to comment.