Indexed access test

This commit is contained in:
Alexander Nozik 2020-03-25 18:52:58 +03:00
parent 417c292507
commit e0aab57953
3 changed files with 17 additions and 5 deletions

View File

@ -6,7 +6,6 @@ import hep.dataforge.values.Value
/** /**
* Convert meta to map of maps * Convert meta to map of maps
*/ */
@DFExperimental
fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> { fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> {
return items.entries.associate { (token, item) -> return items.entries.associate { (token, item) ->
token.toString() to when (item) { token.toString() to when (item) {
@ -19,7 +18,6 @@ fun Meta.toMap(descriptor: NodeDescriptor? = null): Map<String, Any?> {
/** /**
* Convert map of maps to meta * Convert map of maps to meta
*/ */
@DFExperimental
fun Map<String, Any?>.toMeta(descriptor: NodeDescriptor? = null): Meta = Meta { fun Map<String, Any?>.toMeta(descriptor: NodeDescriptor? = null): Meta = Meta {
entries.forEach { (key, value) -> entries.forEach { (key, value) ->
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")

View File

@ -4,7 +4,8 @@ import hep.dataforge.names.Name
import hep.dataforge.names.toName import hep.dataforge.names.toName
/** /**
* Get all items matching given name. * Get all items matching given name. The index of the last element, if present is used as a [Regex],
* against which indexes of elements are matched.
*/ */
fun Meta.getIndexed(name: Name): Map<String, MetaItem<*>> { fun Meta.getIndexed(name: Name): Map<String, MetaItem<*>> {
val root = when (name.length) { val root = when (name.length) {

View File

@ -49,4 +49,17 @@ class MetaTest {
assertEquals(meta, reconstructed) assertEquals(meta, reconstructed)
} }
@Test
fun indexed() {
val meta = Meta {
(0..20).forEach {
set("a[$it]", it)
}
}
val indexed = meta.getIndexed("a[1.]")
assertEquals(10, indexed.size)
assertEquals(null, indexed["8"])
assertEquals(12, indexed["12"].int)
}
} }