Skip to content

Commit

Permalink
Use streams
Browse files Browse the repository at this point in the history
  • Loading branch information
antvaset committed Feb 2, 2025
1 parent 24a33a8 commit 9d6729f
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ open class CommonCqlCompiler(
return run(CharStreams.fromString(cqlText))
}

fun run(source: Source): Library? {
open fun run(source: Source): Library? {
return run(CharStreams.fromString(source.readString()))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ open class CommonLibraryManager(
val modelManager: CommonModelManager,
val namespaceManager: NamespaceManager,
open val librarySourceLoader: CommonLibrarySourceLoader,
val ucumService: UcumService,
lazyUcumService: Lazy<UcumService>,
val cqlCompilerOptions: CqlCompilerOptions = CqlCompilerOptions.defaultOptions(),
val compiledLibraries: MutableMap<VersionedIdentifier, CompiledLibrary> = HashMap()
) {
Expand All @@ -32,6 +32,8 @@ open class CommonLibraryManager(
READ_WRITE
}

val ucumService by lazyUcumService

/*
* A "well-known" library name is one that is allowed to resolve without a
* namespace in a namespace-aware context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fun cqlToElm(
}
}

val libraryManager = CommonLibraryManager(modelManager, namespaceManager, librarySourceLoader, ucumService)
val libraryManager = CommonLibraryManager(modelManager, namespaceManager, librarySourceLoader, lazy { ucumService })

val translator = CommonCqlTranslator.fromText(cqlText, libraryManager)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.cqframework.cql.cql2elm

import kotlinx.io.Source
import kotlinx.io.asInputStream
import org.antlr.v4.kotlinruntime.CharStreams
import org.hl7.cql.model.NamespaceInfo
import org.hl7.elm.r1.Library
Expand All @@ -20,6 +22,10 @@ class CqlCompiler(
libraryManager: LibraryManager
) : this(namespaceInfo, null, libraryManager)

override fun run(source: Source): Library? {
return run(CharStreams.fromStream(source.asInputStream()))
}

@Throws(IOException::class)
fun run(cqlFile: File): Library? {
return run(CharStreams.fromStream(FileInputStream(cqlFile)))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.cqframework.cql.cql2elm

import org.cqframework.cql.cql2elm.model.CompiledLibrary
import org.cqframework.cql.cql2elm.ucum.getUcumService
import org.cqframework.cql.cql2elm.ucum.lazyUcumService
import org.hl7.elm.r1.VersionedIdentifier
import kotlin.jvm.JvmOverloads

Expand All @@ -15,7 +15,7 @@ constructor(
modelManager,
modelManager.namespaceManager,
PriorityLibrarySourceLoader(),
getUcumService(),
lazyUcumService,
cqlCompilerOptions,
libraryCache
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package org.cqframework.cql.cql2elm.ucum

import java.util.*

fun getUcumService(): UcumService {
return ServiceLoader.load(UcumService::class.java).firstOrNull()
val lazyUcumService = lazy {
ServiceLoader.load(UcumService::class.java).firstOrNull()
?: error(
"""No UCUM service implementation found.
Please ensure a UCUM service implementation is available on the classpath.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
@file:Suppress("detekt:all")

package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.readString
import org.cqframework.cql.elm.serializing.ElmLibraryReader
import org.hl7.elm.r1.Library

class ElmXmlLibraryReader : ElmLibraryReader {
override fun read(string: String): Library {
return xml.decodeFromString(Library.serializer(), string)
}
override fun read(source: Source): Library {
return read(source.readString())
}
expect class ElmXmlLibraryReader() : ElmLibraryReader {
override fun read(string: String): Library
override fun read(source: Source): Library
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Sink
import kotlinx.io.writeString
import org.cqframework.cql.elm.serializing.ElmLibraryWriter
import org.hl7.elm.r1.Library

class ElmXmlLibraryWriter : ElmLibraryWriter {
override fun write(library: Library, sink: Sink) {
sink.writeString(writeAsString(library))
}

override fun writeAsString(library: Library): String {
return xml.encodeToString(Library.serializer(), library)
}
expect class ElmXmlLibraryWriter() : ElmLibraryWriter {
override fun write(library: Library, sink: Sink)
override fun writeAsString(library: Library): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.readString
import org.cqframework.cql.elm.serializing.ElmLibraryReader
import org.hl7.elm.r1.Library

actual class ElmXmlLibraryReader actual constructor() : ElmLibraryReader {
actual override fun read(string: String): Library {
return xml.decodeFromString(Library.serializer(), string)
}
actual override fun read(source: Source): Library {
return read(source.readString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Sink
import kotlinx.io.writeString
import org.cqframework.cql.elm.serializing.ElmLibraryWriter
import org.hl7.elm.r1.Library

actual class ElmXmlLibraryWriter actual constructor() : ElmLibraryWriter {
actual override fun write(library: Library, sink: Sink) {
sink.writeString(writeAsString(library))
}
actual override fun writeAsString(library: Library): String {
return xml.encodeToString(Library.serializer(), library)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.asInputStream
import nl.adaptivity.xmlutil.newGenericReader
import nl.adaptivity.xmlutil.xmlStreaming
import org.cqframework.cql.elm.serializing.ElmLibraryReader
import org.hl7.elm.r1.Library

actual class ElmXmlLibraryReader actual constructor() : ElmLibraryReader {
actual override fun read(string: String): Library {
return xml.decodeFromString(Library.serializer(), string)
}
actual override fun read(source: Source): Library {
return xml.decodeFromReader(
Library.serializer(),
xmlStreaming.newGenericReader(source.asInputStream())
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.cqframework.cql.elm.serializing.xmlutil

import kotlinx.io.Sink
import kotlinx.io.asOutputStream
import nl.adaptivity.xmlutil.core.impl.newWriter
import nl.adaptivity.xmlutil.xmlStreaming
import org.cqframework.cql.elm.serializing.ElmLibraryWriter
import org.hl7.elm.r1.Library

actual class ElmXmlLibraryWriter actual constructor() : ElmLibraryWriter {
actual override fun write(library: Library, sink: Sink) {
xml.encodeToWriter(
xmlStreaming.newWriter(sink.asOutputStream(), "UTF-8"),
Library.serializer(),
library
)
}
actual override fun writeAsString(library: Library): String {
return xml.encodeToString(Library.serializer(), library)
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,13 @@
package org.hl7.elm_modelinfo.r1.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.readString
import nl.adaptivity.xmlutil.serialization.XML
import org.hl7.elm_modelinfo.r1.ModelInfo
import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader

private val xml = XML(org.hl7.elm_modelinfo.r1.serializersModule)
internal val xml = XML(org.hl7.elm_modelinfo.r1.serializersModule)

class XmlModelInfoReader : ModelInfoReader {
override fun read(string: String): ModelInfo {
return xml.decodeFromString(
ModelInfo.serializer(),
string
)
}
override fun read(source: Source): ModelInfo {
return read(source.readString())
}
expect class XmlModelInfoReader() : ModelInfoReader {
override fun read(string: String): ModelInfo
override fun read(source: Source): ModelInfo
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.hl7.elm_modelinfo.r1.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.readString
import org.hl7.elm_modelinfo.r1.ModelInfo
import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader

actual class XmlModelInfoReader actual constructor() : ModelInfoReader {
actual override fun read(string: String): ModelInfo {
return xml.decodeFromString(
ModelInfo.serializer(),
string
)
}
actual override fun read(source: Source): ModelInfo {
return read(source.readString())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.hl7.elm_modelinfo.r1.serializing.xmlutil

import kotlinx.io.Source
import kotlinx.io.asInputStream
import nl.adaptivity.xmlutil.newGenericReader
import nl.adaptivity.xmlutil.xmlStreaming
import org.hl7.elm_modelinfo.r1.ModelInfo
import org.hl7.elm_modelinfo.r1.serializing.ModelInfoReader

actual class XmlModelInfoReader actual constructor() : ModelInfoReader {
actual override fun read(string: String): ModelInfo {
return xml.decodeFromString(
ModelInfo.serializer(),
string
)
}
actual override fun read(source: Source): ModelInfo {
return xml.decodeFromReader(
ModelInfo.serializer(),
xmlStreaming.newGenericReader(source.asInputStream())
)
}
}

0 comments on commit 9d6729f

Please sign in to comment.