Envelope IO

This commit is contained in:
Alexander Nozik 2019-05-26 22:05:20 +03:00
parent 7c38cadddd
commit 13c5a579ed
2 changed files with 25 additions and 6 deletions

View File

@ -70,11 +70,7 @@ class TaggedEnvelopeFormat(
val tag = Tag(metaFormat.key, metaBytes.size.toUInt(), obj.data?.size ?: 0.toULong()) val tag = Tag(metaFormat.key, metaBytes.size.toUInt(), obj.data?.size ?: 0.toULong())
out.writePacket(tag.toBytes()) out.writePacket(tag.toBytes())
out.writeFully(metaBytes) out.writeFully(metaBytes)
obj.data?.read { obj.data?.read { copyTo(out) }
while (!endOfInput){
out.writeByte(readByte())
}
}
} }
} }
} }

View File

@ -26,6 +26,9 @@ interface TransformationRule {
fun <M : MutableMetaNode<M>> transformItem(name: Name, item: MetaItem<*>?, target: M): Unit fun <M : MutableMetaNode<M>> transformItem(name: Name, item: MetaItem<*>?, target: M): Unit
} }
/**
* A transformation which transforms an element with given [name] to itself.
*/
data class SelfTransformationRule(val name: Name) : TransformationRule { data class SelfTransformationRule(val name: Name) : TransformationRule {
override fun matches(name: Name, item: MetaItem<*>?): Boolean { override fun matches(name: Name, item: MetaItem<*>?): Boolean {
return name == name return name == name
@ -38,6 +41,9 @@ data class SelfTransformationRule(val name: Name) : TransformationRule {
} }
} }
/**
* A transformation which transforms element with specific name
*/
data class SingleItemTransformationRule( data class SingleItemTransformationRule(
val from: Name, val from: Name,
val to: Name, val to: Name,
@ -59,14 +65,31 @@ data class SingleItemTransformationRule(
class MetaTransformation { class MetaTransformation {
private val transformations = HashSet<TransformationRule>() private val transformations = HashSet<TransformationRule>()
/** /**
* Produce new meta using only those items that match transformation rules * Produce new meta using only those items that match transformation rules
*/ */
fun produce(source: Meta): Meta = buildMeta { fun transform(source: Meta): Meta = buildMeta {
transformations.forEach { rule -> transformations.forEach { rule ->
rule.selectItems(source).forEach { name -> rule.selectItems(source).forEach { name ->
rule.transformItem(name, source[name], this) rule.transformItem(name, source[name], this)
} }
} }
} }
/**
* Transform a meta, replacing all elements found in rules with transformed entries
*/
fun apply(source: Meta): Meta = buildMeta(source) {
transformations.forEach { rule ->
rule.selectItems(source).forEach { name ->
remove(name)
rule.transformItem(name, source[name], this)
}
}
}
companion object{
}
} }