Skip to content

Commit

Permalink
Jsonrpclib 0.0.7 (#197)
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol authored Apr 11, 2024
1 parent 71db9be commit bb269c9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ val V = new {
val scribe = "3.13.1"
val upickle = "2.0.0"
val cats = "2.10.0"
val jsonrpclib = "0.0.6"
val jsonrpclib = "0.0.7"
val fs2 = "3.10.0"
val http4s = "0.23.26"
val laminar = "0.14.5"
Expand Down
4 changes: 2 additions & 2 deletions modules/e2e-tests/src/test/scala/EndToEndTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ transparent inline def asNotification[T <: LSPNotification](
p: Payload
): Option[t.In] =
for
js <- scala.util.Try(ujson.read(p.array)).toOption
js <- scala.util.Try(ujson.read(writeToArray(p))).toOption
o <- js.objOpt
p <- o.get("params")
res <- scala.util.Try(upickle.default.read[t.In](p)).toOption
Expand All @@ -163,7 +163,7 @@ transparent inline def asResponse[T <: LSPRequest](
p: Payload
): Option[t.Out] =
for
js <- scala.util.Try(ujson.read(p.array)).toOption
js <- scala.util.Try(ujson.read(writeToArray(p))).toOption
o <- js.objOpt
p <- o.get("result")
res <- scala.util.Try(upickle.default.read[t.Out](p)).toOption
Expand Down
7 changes: 3 additions & 4 deletions modules/lsp/src/main/scala/JSONRPC.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,17 @@ import upickle.default.*
import util.chaining.*

private[langoustine] object jsonrpcIntegration:
val nullArray = "null".getBytes()
given codec[T: Reader: Writer]: Codec[T] =
new Codec[T]:
override def decode(
payload: Option[Payload]
): Either[ProtocolError, T] =
payload
.map(_.array)
.flatMap { arr =>
.map(_.stripNull.map(_.array).getOrElse(nullArray))
.flatMap: arr =>
Try(read[T](arr, trace = true)).toOption
}
.toRight(ProtocolError.InvalidParams("oopsie daisy"))
end decode

override def encode(a: T): Payload =
Payload(write(a).getBytes)
Expand Down
3 changes: 2 additions & 1 deletion modules/tests/src/test/scala/testkit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import scala.annotation.tailrec
import java.util.concurrent.atomic.AtomicReference
import jsonrpclib.InputMessage.RequestMessage
import jsonrpclib.InputMessage.NotificationMessage
import com.github.plokhotnyuk.jsoniter_scala.core.writeToArray

given [F[_]](using MonadThrow[F]): Monadic[F] with
def doFlatMap[A, B](fa: F[A])(f: A => F[B]): F[B] =
Expand Down Expand Up @@ -91,7 +92,7 @@ def request[F[_]: RefConstructor: MonadThrow, T <: requests.LSPRequest](
case Left(err) => F.raiseError(erc.encode(err))
case Right(res) =>
F.catchNonFatal {
upickle.default.read[req.Out](outc.encode(res).array)
upickle.default.read[req.Out](writeToArray(outc.encode(res)))
}
}
yield res -> communicate
Expand Down
27 changes: 23 additions & 4 deletions modules/tracer/backend/src/main/scala/command.trace.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ import jsonrpclib.Message
extension (s: fs2.Stream[IO, Payload])
def debugAs(name: String) =
s.evalTap(el =>
Logging.debug(s"[$name (payload)]: ${new String(el.array)}")
Logging.debug(
s"[$name (payload)]: ${el.stripNull.map(a => new String(a.array)).getOrElse("null")}"
)
)

extension [A](s: fs2.Stream[IO, A])
inline def debugAs(name: String)(using NotGiven[A =:= Payload]) =
s.evalTap(el => Logging.debug(s"[$name]: $el"))
inline def debugAs[B](name: String, f: A => B)(using
NotGiven[A =:= Payload]
) =
s.evalTap(el => Logging.debug(s"[$name]: ${f(el)}"))

def Trace(
in: fs2.Stream[IO, Byte],
Expand Down Expand Up @@ -82,7 +88,12 @@ def Trace(
}

val captureStdin =
in
in.chunks
.debugAs(
"chunks coming from stdin",
chunk => new String(chunk.toArray)
)
.unchunks
.through(lsp.decodeMessages)
.evalMap {
case Left(err) =>
Expand All @@ -107,7 +118,12 @@ def Trace(
.through(child.stdin)

val captureStdout =
child.stdout
child.stdout.chunks
.debugAs(
"chunks coming from stdout",
chunk => new String(chunk.toArray)
)
.unchunks
.through(lsp.decodeMessages)
.evalMap {
case Left(err) =>
Expand Down Expand Up @@ -136,7 +152,10 @@ def Trace(

val redirectStderr =
errStream
.debugAs("writing to real stderr")
.debugAs(
"writing to real stderr",
chunk => new String(chunk.toArray)
)
.unchunks
.through(err)

Expand Down
26 changes: 22 additions & 4 deletions modules/tracer/backend/src/main/scala/logging.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
package langoustine.tracer

import scribe.Level

object Logging:
private lazy val io =
scribe.Logger.root
.clearHandlers()
.withHandler(writer = scribe.writer.SystemErrWriter)
.replace()
if sys.env.contains("LANGOUSTINE_TRACER_DEBUG") then
scribe.Logger.root
.clearHandlers()
.withHandler(
writer = scribe.writer.SystemErrWriter,
outputFormat = scribe.output.format.ASCIIOutputFormat
)
.withMinimumLevel(Level.Debug)
.replace()
else
scribe.Logger.root
.clearHandlers()
.withHandler(
writer = scribe.writer.SystemErrWriter,
outputFormat = scribe.output.format.ASCIIOutputFormat
)
.replace()
end if

scribe.cats.io
end io

export io.{debug, info, warn, error}

end Logging

0 comments on commit bb269c9

Please sign in to comment.