Add Yaml meta format plugin

This commit is contained in:
Alexander Nozik 2021-11-27 13:36:07 +03:00
parent c423dc214e
commit 532e0c253b
4 changed files with 53 additions and 23 deletions

View File

@ -23,11 +23,11 @@ public class FrontMatterEnvelopeFormat(
do { do {
line = input.readUTF8Line() ?: error("Input does not contain front matter separator") line = input.readUTF8Line() ?: error("Input does not contain front matter separator")
offset += line.encodeToByteArray().size.toUInt() offset += line.encodeToByteArray().size.toUInt()
} while (!line.startsWith(space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR)) } while (!line.startsWith(SEPARATOR))
val readMetaFormat = val readMetaFormat =
space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.metaTypeRegex.matchEntire(line)?.groupValues?.first() metaTypeRegex.matchEntire(line)?.groupValues?.first()
?.let { io.resolveMetaFormat(it) } ?: space.kscience.dataforge.io.yaml.YamlMetaFormat ?.let { io.resolveMetaFormat(it) } ?: YamlMetaFormat
//TODO replace by preview //TODO replace by preview
val meta = Binary { val meta = Binary {
@ -35,7 +35,7 @@ public class FrontMatterEnvelopeFormat(
line = input.readSafeUtf8Line() line = input.readSafeUtf8Line()
writeUtf8String(line + "\r\n") writeUtf8String(line + "\r\n")
offset += line.encodeToByteArray().size.toUInt() offset += line.encodeToByteArray().size.toUInt()
} while (!line.startsWith(space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR)) } while (!line.startsWith(SEPARATOR))
}.read { }.read {
readMetaFormat.readMeta(input) readMetaFormat.readMeta(input)
@ -47,16 +47,16 @@ public class FrontMatterEnvelopeFormat(
var line: String var line: String
do { do {
line = input.readSafeUtf8Line() //?: error("Input does not contain front matter separator") line = input.readSafeUtf8Line() //?: error("Input does not contain front matter separator")
} while (!line.startsWith(space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR)) } while (!line.startsWith(SEPARATOR))
val readMetaFormat = val readMetaFormat =
space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.metaTypeRegex.matchEntire(line)?.groupValues?.first() metaTypeRegex.matchEntire(line)?.groupValues?.first()
?.let { io.resolveMetaFormat(it) } ?: space.kscience.dataforge.io.yaml.YamlMetaFormat ?.let { io.resolveMetaFormat(it) } ?: YamlMetaFormat
val meta = Binary { val meta = Binary {
do { do {
writeUtf8String(input.readSafeUtf8Line() + "\r\n") writeUtf8String(input.readSafeUtf8Line() + "\r\n")
} while (!line.startsWith(space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR)) } while (!line.startsWith(SEPARATOR))
}.read { }.read {
readMetaFormat.readMeta(input) readMetaFormat.readMeta(input)
} }
@ -72,9 +72,9 @@ public class FrontMatterEnvelopeFormat(
formatMeta: Meta, formatMeta: Meta,
) { ) {
val metaFormat = metaFormatFactory(formatMeta, this@FrontMatterEnvelopeFormat.io.context) val metaFormat = metaFormatFactory(formatMeta, this@FrontMatterEnvelopeFormat.io.context)
output.writeRawString("${space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR}\r\n") output.writeRawString("$SEPARATOR\r\n")
metaFormat.run { this.writeObject(output, envelope.meta) } metaFormat.run { this.writeObject(output, envelope.meta) }
output.writeRawString("${space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.SEPARATOR}\r\n") output.writeRawString("$SEPARATOR\r\n")
//Printing data //Printing data
envelope.data?.let { data -> envelope.data?.let { data ->
output.writeBinary(data) output.writeBinary(data)
@ -92,32 +92,32 @@ public class FrontMatterEnvelopeFormat(
private val metaTypeRegex = "---(\\w*)\\s*".toRegex() private val metaTypeRegex = "---(\\w*)\\s*".toRegex()
override fun invoke(meta: Meta, context: Context): EnvelopeFormat { override fun invoke(meta: Meta, context: Context): EnvelopeFormat {
return space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat(context.io, meta) return FrontMatterEnvelopeFormat(context.io, meta)
} }
override fun peekFormat(io: IOPlugin, binary: Binary): EnvelopeFormat? = binary.read { override fun peekFormat(io: IOPlugin, binary: Binary): EnvelopeFormat? = binary.read {
val line = readSafeUtf8Line() val line = readSafeUtf8Line()
return@read if (line.startsWith("---")) { return@read if (line.startsWith("---")) {
space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.invoke() invoke()
} else { } else {
null null
} }
} }
private val default by lazy { space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.invoke() } private val default by lazy { invoke() }
override fun readPartial(input: Input): PartialEnvelope = override fun readPartial(input: Input): PartialEnvelope =
space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.default.readPartial(input) default.readPartial(input)
override fun writeEnvelope( override fun writeEnvelope(
output: Output, output: Output,
envelope: Envelope, envelope: Envelope,
metaFormatFactory: MetaFormatFactory, metaFormatFactory: MetaFormatFactory,
formatMeta: Meta, formatMeta: Meta,
): Unit = space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.default.writeEnvelope(output, envelope, metaFormatFactory, formatMeta) ): Unit = FrontMatterEnvelopeFormat.default.writeEnvelope(output, envelope, metaFormatFactory, formatMeta)
override fun readObject(input: Input): Envelope = space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.Companion.default.readObject(input) override fun readObject(input: Input): Envelope = default.readObject(input)
} }
} }

View File

@ -110,7 +110,7 @@ public class YamlMetaFormat(private val meta: Meta) : MetaFormat {
} }
override fun toMeta(): Meta = Meta { override fun toMeta(): Meta = Meta {
NAME_KEY put space.kscience.dataforge.io.yaml.FrontMatterEnvelopeFormat.name.toString() NAME_KEY put FrontMatterEnvelopeFormat.name.toString()
META_KEY put meta META_KEY put meta
} }

View File

@ -0,0 +1,32 @@
package space.kscience.dataforge.io.yaml
import space.kscience.dataforge.context.AbstractPlugin
import space.kscience.dataforge.context.Context
import space.kscience.dataforge.context.PluginFactory
import space.kscience.dataforge.context.PluginTag
import space.kscience.dataforge.io.IOPlugin
import space.kscience.dataforge.io.MetaFormatFactory
import space.kscience.dataforge.meta.Meta
import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.asName
import kotlin.reflect.KClass
@DFExperimental
public class YamlPlugin(meta: Meta) : AbstractPlugin(meta) {
public val io: IOPlugin by require(IOPlugin)
override val tag: PluginTag get() = Companion.tag
override fun content(target: String): Map<Name, Any> = when (target) {
MetaFormatFactory.META_FORMAT_TYPE -> mapOf("yaml".asName() to YamlMetaFormat)
else -> super.content(target)
}
public companion object : PluginFactory<YamlPlugin> {
override val tag: PluginTag = PluginTag("io.yaml", group = PluginTag.DATAFORGE_GROUP)
override val type: KClass<out YamlPlugin> = YamlPlugin::class
override fun invoke(meta: Meta, context: Context): YamlPlugin = YamlPlugin(meta)
}
}

View File

@ -52,12 +52,10 @@ public class IOPlugin(meta: Meta) : AbstractPlugin(meta) {
return resolveEnvelopeFormat(Name.parse(name), meta) return resolveEnvelopeFormat(Name.parse(name), meta)
} }
override fun content(target: String): Map<Name, Any> { override fun content(target: String): Map<Name, Any> = when (target) {
return when (target) { META_FORMAT_TYPE -> defaultMetaFormats.toMap()
META_FORMAT_TYPE -> defaultMetaFormats.toMap() ENVELOPE_FORMAT_TYPE -> defaultEnvelopeFormats.toMap()
ENVELOPE_FORMAT_TYPE -> defaultEnvelopeFormats.toMap() else -> super.content(target)
else -> super.content(target)
}
} }
public companion object : PluginFactory<IOPlugin> { public companion object : PluginFactory<IOPlugin> {