Skip to content

Commit

Permalink
Fix transducer handling of empty arrays (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
fsvehla authored Sep 16, 2021
1 parent 89c5af2 commit a8b4470
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ trait JsonDecoderPlatformSpecific[A] { self: JsonDecoder[A] =>
ZTransducer {
for {
// format: off
runtime <- ZIO.runtime[Any].toManaged_
runtime <- ZManaged.runtime[Any]
inQueue <- Queue.unbounded[Take[Nothing, Char]].toManaged_
outQueue <- Queue.unbounded[Take[Throwable, A]].toManaged_
ended <- Ref.makeManaged(false)
reader <- ZManaged.fromAutoCloseable {
ZIO.effectTotal {
UIO {
def readPull: Iterator[Chunk[Char]] =
runtime.unsafeRun(inQueue.take)
.fold(
Expand All @@ -70,13 +70,23 @@ trait JsonDecoderPlatformSpecific[A] { self: JsonDecoder[A] =>
new zio.stream.internal.ZReader(Iterator.empty ++ readPull)
}
}
jsonReader <- ZManaged.fromAutoCloseable(ZIO.effectTotal(new WithRetractReader(reader)))
jsonReader <- ZManaged.fromAutoCloseable(UIO(new WithRetractReader(reader)))
process <- effectBlockingInterrupt {
// Exceptions fall through and are pushed into the queue
@tailrec def loop(atBeginning: Boolean): Unit = {
val nextElem = try {
if (atBeginning && delimiter == JsonStreamDelimiter.Array) {
Lexer.char(Nil, jsonReader, '[')

jsonReader.nextNonWhitespace() match {
case ']' =>
// returning empty here instead of falling through, which would
// attempt to decode a value that we know doesn’t exist.
return ()

case _ =>
jsonReader.retract()
}
} else {
delimiter match {
case JsonStreamDelimiter.Newline =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ object DecoderPlatformSpecificSpec extends DefaultRunnableSpec {
assert(xs)(equalTo(Chunk(1001)))
}
},
testM("empty array") {
ZStream
.fromIterable("[]".toSeq)
.transduce(JsonDecoder[String].decodeJsonTransducer(JsonStreamDelimiter.Array))
.runCollect
.map { xs =>
assert(xs)(isEmpty)
}
},
testM("decodes multiple elements") {
ZStream
.fromIterable("[ 1001, 1002, 1003 ]".toSeq)
Expand Down

0 comments on commit a8b4470

Please sign in to comment.