diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt index c5ad3831..243d40bf 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/MetaBuilder.kt @@ -141,8 +141,15 @@ fun Meta.edit(builder: MetaBuilder.() -> Unit): MetaBuilder = builder().apply(bu /** * Build a [MetaBuilder] using given transformation */ +@Deprecated("To be replaced with fake constructor", ReplaceWith("Meta")) fun buildMeta(builder: MetaBuilder.() -> Unit): MetaBuilder = MetaBuilder().apply(builder) +/** + * Build a [MetaBuilder] using given transformation + */ +@Suppress("FunctionName") +fun Meta(builder: MetaBuilder.() -> Unit): MetaBuilder = MetaBuilder().apply(builder) + /** * Build meta using given source meta as a base */ diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt new file mode 100644 index 00000000..21785980 --- /dev/null +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/meta/transformations/MetaCaster.kt @@ -0,0 +1,75 @@ +package hep.dataforge.meta.transformations + +import hep.dataforge.meta.Meta +import hep.dataforge.meta.MetaItem +import hep.dataforge.meta.get +import hep.dataforge.meta.value +import hep.dataforge.values.* + +/** + * A converter of generic object to and from [MetaItem] + */ +interface MetaCaster { + fun itemToObject(item: MetaItem<*>): T + fun objectToMetaItem(obj: T): MetaItem<*> + + companion object { + + val meta = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): Meta = when (item) { + is MetaItem.NodeItem -> item.node + is MetaItem.ValueItem -> item.value.toMeta() + } + + override fun objectToMetaItem(obj: Meta): MetaItem<*> = MetaItem.NodeItem(obj) + } + + val value = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): Value = when (item) { + is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") + is MetaItem.ValueItem -> item.value + } + + override fun objectToMetaItem(obj: Value): MetaItem<*> = MetaItem.ValueItem(obj) + } + + val string = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): String = when (item) { + is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") + is MetaItem.ValueItem -> item.value + }.string + + override fun objectToMetaItem(obj: String): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) + } + + val boolean = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): Boolean = when (item) { + is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") + is MetaItem.ValueItem -> item.value + }.boolean + + override fun objectToMetaItem(obj: Boolean): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) + } + + val double = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): Double = when (item) { + is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") + is MetaItem.ValueItem -> item.value + }.double + + override fun objectToMetaItem(obj: Double): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) + } + + val int = object : MetaCaster { + override fun itemToObject(item: MetaItem<*>): Int = when (item) { + is MetaItem.NodeItem -> item.node[Meta.VALUE_KEY].value ?: error("Can't convert node to a value") + is MetaItem.ValueItem -> item.value + }.int + + override fun objectToMetaItem(obj: Int): MetaItem<*> = MetaItem.ValueItem(obj.asValue()) + } + } +} + +fun MetaCaster.metaToObject(meta: Meta): T = itemToObject(MetaItem.NodeItem(meta)) +fun MetaCaster.valueToObject(value: Value): T = itemToObject(MetaItem.ValueItem(value)) diff --git a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueExtensions.kt b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueExtensions.kt index f9137dca..3767e2fb 100644 --- a/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueExtensions.kt +++ b/dataforge-meta/src/commonMain/kotlin/hep/dataforge/values/valueExtensions.kt @@ -1,7 +1,6 @@ package hep.dataforge.values import hep.dataforge.meta.Meta -import hep.dataforge.meta.buildMeta /** * Check if value is null @@ -35,4 +34,4 @@ val Value.doubleArray: DoubleArray } -fun Value.toMeta() = buildMeta { Meta.VALUE_KEY put this } \ No newline at end of file +fun Value.toMeta() = Meta { Meta.VALUE_KEY put this } \ No newline at end of file diff --git a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt index 71469984..bba90d38 100644 --- a/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt +++ b/dataforge-tables/src/commonMain/kotlin/hep/dataforge/tables/Table.kt @@ -45,7 +45,7 @@ operator fun Collection>.get(name: String): Column<*>? = find { it.nam inline operator fun Table.get(row: Int, column: String): T? = getValue(row, column, T::class) -operator fun Table.get(row: Int, column: Column): T? = getValue(row, column.name, column.type) +operator fun Table.get(row: Int, column: ColumnHeader): T? = getValue(row, column.name, column.type) interface Column : ColumnHeader { val size: Int