Skip to content

Commit

Permalink
Support persist for GrpcStub
Browse files Browse the repository at this point in the history
  • Loading branch information
danslapman committed Mar 24, 2024
1 parent 595cdab commit f0acf03
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
Expand All @@ -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) =>
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
)

Expand Down
2 changes: 1 addition & 1 deletion backend/project/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion backend/project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.9.8
sbt.version = 1.9.9

0 comments on commit f0acf03

Please sign in to comment.