From f0acf0373cd63c4f24094bc7913b99d48caeb77e Mon Sep 17 00:00:00 2001 From: Daniel Slapman Date: Sun, 24 Mar 2024 17:23:13 +0100 Subject: [PATCH] Support `persist` for GrpcStub --- backend/build.sbt | 2 +- .../api/request/CreateGrpcStubRequest.scala | 1 + .../mockingbird/grpc/GrpcRequestHandler.scala | 17 +++++++++++++++-- .../tcb/mockingbird/model/GrpcStub.scala | 1 + backend/project/Settings.scala | 2 +- backend/project/build.properties | 2 +- 6 files changed, 20 insertions(+), 5 deletions(-) diff --git a/backend/build.sbt b/backend/build.sbt index 9cd76ec0..8c7b0389 100644 --- a/backend/build.sbt +++ b/backend/build.sbt @@ -34,7 +34,7 @@ val dataAccess = (project in file("dataAccess")) "com.chuusai" %% "shapeless" % "2.3.3", "org.julienrf" %% "enum-labels" % "3.1", "tf.tofu" %% "derevo-core" % Versions.derevo, - "com.softwaremill.magnolia1_2" %% "magnolia" % "1.1.2", + "com.softwaremill.magnolia1_2" %% "magnolia" % "1.1.8", "com.google.code.findbugs" % "jsr305" % "3.0.2" % Optional ) ++ Dependencies.scalatest ++ Dependencies.scalacheck ++ Dependencies.json ++ Dependencies.refined, libraryDependencies ++= Dependencies.reflect(scalaVersion.value), diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/api/request/CreateGrpcStubRequest.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/api/request/CreateGrpcStubRequest.scala index 6815a15d..ac686fd8 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/api/request/CreateGrpcStubRequest.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/api/request/CreateGrpcStubRequest.scala @@ -36,5 +36,6 @@ final case class CreateGrpcStubRequest( requestPredicates: JsonPredicate, state: Option[Map[JsonOptic, Map[Keyword.Json, Json]]], seed: Option[Json], + persist: Option[Map[JsonOptic, Json]], labels: Seq[String] = Seq.empty ) diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/grpc/GrpcRequestHandler.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/grpc/GrpcRequestHandler.scala index 18d9db25..066a5212 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/grpc/GrpcRequestHandler.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/grpc/GrpcRequestHandler.scala @@ -4,15 +4,19 @@ import io.circe.Json import io.circe.syntax.KeyOps import io.grpc.CallOptions import io.grpc.ManagedChannelBuilder +import mouse.option.* import scalapb.zio_grpc.RequestContext import scalapb.zio_grpc.ZManagedChannel import scalapb.zio_grpc.client.ClientCalls import zio.Duration +import zio.interop.catz.core.* import ru.tinkoff.tcb.mockingbird.api.Tracing import ru.tinkoff.tcb.mockingbird.api.WLD +import ru.tinkoff.tcb.mockingbird.dal.PersistentStateDAO import ru.tinkoff.tcb.mockingbird.error.StubSearchError import ru.tinkoff.tcb.mockingbird.grpc.GrpcExractor.FromGrpcProtoDefinition +import ru.tinkoff.tcb.mockingbird.misc.Renderable.ops.* import ru.tinkoff.tcb.mockingbird.model.FillResponse import ru.tinkoff.tcb.mockingbird.model.GProxyResponse import ru.tinkoff.tcb.mockingbird.model.PersistentState @@ -24,7 +28,8 @@ trait GrpcRequestHandler { def exec(bytes: Array[Byte]): RIO[WLD & RequestContext, Array[Byte]] } -class GrpcRequestHandlerImpl(stubResolver: GrpcStubResolver) extends GrpcRequestHandler { +class GrpcRequestHandlerImpl(stateDAO: PersistentStateDAO[Task], stubResolver: GrpcStubResolver) + extends GrpcRequestHandler { override def exec(bytes: Array[Byte]): RIO[WLD & RequestContext, Array[Byte]] = for { context <- ZIO.service[RequestContext] @@ -42,6 +47,13 @@ class GrpcRequestHandlerImpl(stubResolver: GrpcStubResolver) extends GrpcRequest "seed" := seed, "state" := state.data ) + persist = stub.persist + _ <- persist + .cata(spec => stateDAO.upsertBySpec(state.id, spec.fill(data)).map(_.successful), ZIO.succeed(true)) + _ <- persist + .map(_.keys.map(_.path).filter(_.startsWith("_")).toVector) + .filter(_.nonEmpty) + .cata(_.traverse(stateDAO.createIndexForDataField), ZIO.unit) responseSchema = stub.responseSchema response <- stub.response match { case FillResponse(rdata, delay) => @@ -85,7 +97,8 @@ class GrpcRequestHandlerImpl(stubResolver: GrpcStubResolver) extends GrpcRequest } object GrpcRequestHandlerImpl { - val live: URLayer[GrpcStubResolver, GrpcRequestHandler] = ZLayer.fromFunction(new GrpcRequestHandlerImpl(_)) + val live: URLayer[PersistentStateDAO[Task] & GrpcStubResolver, GrpcRequestHandlerImpl] = + ZLayer.fromFunction(new GrpcRequestHandlerImpl(_, _)) } object GrpcRequestHandler { diff --git a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/GrpcStub.scala b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/GrpcStub.scala index 979a3228..42514065 100644 --- a/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/GrpcStub.scala +++ b/backend/mockingbird/src/main/scala/ru/tinkoff/tcb/mockingbird/model/GrpcStub.scala @@ -47,6 +47,7 @@ final case class GrpcStub( seed: Option[Json], state: Option[Map[JsonOptic, Map[Keyword.Json, Json]]], requestPredicates: JsonPredicate, + persist: Option[Map[JsonOptic, Json]], labels: Seq[String] ) diff --git a/backend/project/Settings.scala b/backend/project/Settings.scala index 60c8bf3d..5144532a 100644 --- a/backend/project/Settings.scala +++ b/backend/project/Settings.scala @@ -67,7 +67,7 @@ object Settings { "-Ywarn-macros:after" ), addCompilerPlugin("com.olegpy" %% "better-monadic-for" % "0.3.1"), - addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.2" cross CrossVersion.full), + addCompilerPlugin("org.typelevel" %% "kind-projector" % "0.13.3" cross CrossVersion.full), wartremoverDependencies ~= (_.filterNot(_.name == "wartremover-contrib")), wartremoverDependencies += "org.wartremover" % "wartremover-contrib_2.13" % ContribWart.ContribVersion, wartremoverErrors ++= Seq( diff --git a/backend/project/build.properties b/backend/project/build.properties index 63df6ac8..f2f13471 100644 --- a/backend/project/build.properties +++ b/backend/project/build.properties @@ -1 +1 @@ -sbt.version = 1.9.8 \ No newline at end of file +sbt.version = 1.9.9 \ No newline at end of file