Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix transducer handling of empty arrays #435

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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