-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcef26b
commit 48efb0e
Showing
5 changed files
with
124 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package xs4s.fs2compat | ||
|
||
import _root_.fs2.{Pipe, Stream} | ||
import cats.effect.{Resource, Sync} | ||
import xs4s.XmlElementExtractor | ||
import xs4s.generic.Scanner | ||
import xs4s.syntax.core._ | ||
|
||
import javax.xml.stream.XMLEventReader | ||
import javax.xml.stream.events.XMLEvent | ||
import scala.language.higherKinds | ||
|
||
/** | ||
* Utilities to enhance xs4s interaction with FS2 | ||
*/ | ||
trait Fs2Syntax { | ||
|
||
implicit class RichScanner[In, State, Out](scanner: Scanner[In, State, Out]) { | ||
|
||
/** Create an FS2 Pipe from the scanner */ | ||
def fs2Pipe[F[_]]: Pipe[F, In, Out] = | ||
_.scan(scanner.initial)(scanner.scan).map(scanner.collect).unNone | ||
} | ||
|
||
implicit class RichFs2StreamObj(obj: Stream.type) { | ||
|
||
/** Create an XMLEvent Stream from an XMLEventReader */ | ||
def xmlEventStream[F[_]: Sync]( | ||
xmlEventReader: Resource[F, XMLEventReader], | ||
chunkSize: Int = 1): Stream[F, XMLEvent] = | ||
Stream | ||
.resource(xmlEventReader) | ||
.flatMap( | ||
reader => | ||
Stream | ||
.fromBlockingIterator[F] | ||
.apply[XMLEvent](reader.toIterator, chunkSize)) | ||
} | ||
|
||
implicit class RichXmlElementExtractor[O]( | ||
xmlElementExtractor: XmlElementExtractor[O]) { | ||
def toFs2PipeThrowError[F[_]]: Pipe[F, XMLEvent, O] = | ||
xmlElementExtractor.scannerThrowingOnError.fs2Pipe[F] | ||
def toFs2PipeIncludeError[F[_]]: Pipe[F, XMLEvent, Either[Throwable, O]] = | ||
xmlElementExtractor.scannerEitherOnError.fs2Pipe[F] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package xs4s | ||
|
||
import cats.effect.{Async, Resource, Sync} | ||
import fs2._ | ||
import xs4s.syntax.fs2._ | ||
|
||
import javax.xml.stream.XMLInputFactory | ||
import javax.xml.stream.events.XMLEvent | ||
import scala.language.higherKinds | ||
|
||
package object fs2compat { | ||
|
||
/** | ||
* Turns an FS2 Byte Stream into a stream of XMLEvent. | ||
* It turns the source stream into an input stream, | ||
* then creates a reader, and then creates a further stream | ||
* from the Iterator that was obtained. | ||
* */ | ||
def byteStreamToXmlEventStream[F[_]: Async]( | ||
xmlInputFactory: XMLInputFactory = defaultXmlInputFactory, | ||
chunkSize: Int = 1)(implicit F: Sync[F]): Pipe[F, Byte, XMLEvent] = | ||
byteStream => | ||
Stream | ||
.resource(io.toInputStreamResource(byteStream)) | ||
.flatMap( | ||
inputStream => | ||
Stream.xmlEventStream( | ||
Resource.make( | ||
F.delay(xmlInputFactory.createXMLEventReader(inputStream)))( | ||
xmlEventReader => F.delay(xmlEventReader.close())), | ||
chunkSize)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package xs4s.syntax | ||
|
||
object fs2 extends xs4s.fs2compat.Fs2Syntax {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package xs4s.fs2compat | ||
|
||
import cats.effect.IO | ||
import cats.effect.unsafe.implicits.global | ||
import fs2.Stream | ||
import org.scalatest.freespec.AnyFreeSpec | ||
import xs4s.XmlElementExtractor | ||
import xs4s.syntax.fs2.RichXmlElementExtractor | ||
|
||
import scala.xml.Elem | ||
|
||
final class Fs2CompatSpec extends AnyFreeSpec { | ||
"It works" in { | ||
val input = | ||
s""" | ||
|<items> | ||
|<embedded><item>Embedded</item></embedded> | ||
|<item>General</item> | ||
|<embedded-twice><embedded-once><item>Doubly embedded</item></embedded-once></embedded-twice> | ||
|<item><item>Nested</item></item> | ||
|</items> | ||
| | ||
""".stripMargin | ||
|
||
val anchorElementExtractor: XmlElementExtractor[Elem] = | ||
XmlElementExtractor.filterElementsByName("item") | ||
|
||
val textStream: Stream[IO, String] = fs2.Stream | ||
.apply[IO, String](input) | ||
.flatMap(str => fs2.Stream.emits(str.getBytes().toList)) | ||
.through(byteStreamToXmlEventStream[IO]()) | ||
.through(anchorElementExtractor.toFs2PipeThrowError) | ||
.map(_.text) | ||
|
||
assert( | ||
textStream.compile.toList.unsafeRunSync() == List("Embedded", | ||
"General", | ||
"Doubly embedded", | ||
"Nested")) | ||
} | ||
} |