refactor MutableMeta set method to accept wider range of receivers

This commit is contained in:
Alexander Nozik 2022-08-01 18:12:57 +03:00
parent 6ca76cff17
commit 81e2ad06cc
No known key found for this signature in database
GPG Key ID: F7FCF2DD25C71357
21 changed files with 77 additions and 69 deletions

View File

@ -1,12 +1,12 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins { plugins {
id("ru.mipt.npm.gradle.project") id("space.kscience.gradle.project")
} }
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.6.0-dev-10" version = "0.6.0-dev-12"
} }
subprojects { subprojects {

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
description = "Context and provider definitions" description = "Context and provider definitions"
@ -31,5 +31,5 @@ kotlin {
} }
readme { readme {
maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT maturity = space.kscience.gradle.Maturity.DEVELOPMENT
} }

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
kscience{ kscience{
@ -19,5 +19,5 @@ kotlin {
} }
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL maturity = space.kscience.gradle.Maturity.EXPERIMENTAL
} }

View File

@ -1,14 +1,14 @@
import ru.mipt.npm.gradle.KScienceVersions import space.kscience.gradle.KScienceVersions
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
description = "IO module" description = "IO module"
kscience { kscience {
useSerialization(sourceSet = ru.mipt.npm.gradle.DependencySourceSet.TEST) { useSerialization(sourceSet = space.kscience.gradle.DependencySourceSet.TEST) {
cbor() cbor()
} }
} }
@ -27,5 +27,5 @@ kotlin {
} }
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE maturity = space.kscience.gradle.Maturity.PROTOTYPE
} }

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
// id("ru.mipt.npm.gradle.native") // id("space.kscience.gradle.native")
} }
description = "YAML meta IO" description = "YAML meta IO"
@ -26,7 +26,7 @@ kotlin {
} }
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE maturity = space.kscience.gradle.Maturity.PROTOTYPE
description =""" description ="""
YAML meta converters and Front Matter envelope format YAML meta converters and Front Matter envelope format
""".trimIndent() """.trimIndent()

View File

@ -2,8 +2,8 @@ package space.kscience.dataforge.io
import space.kscience.dataforge.context.ContextBuilder import space.kscience.dataforge.context.ContextBuilder
import space.kscience.dataforge.meta.get import space.kscience.dataforge.meta.get
import space.kscience.dataforge.meta.set
import space.kscience.dataforge.meta.string import space.kscience.dataforge.meta.string
import space.kscience.dataforge.values.set
import java.nio.file.Path import java.nio.file.Path
import kotlin.io.path.Path import kotlin.io.path.Path

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
kscience { kscience {
@ -12,5 +12,5 @@ kscience {
description = "Meta definition and basic operations on meta" description = "Meta definition and basic operations on meta"
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.DEVELOPMENT maturity = space.kscience.gradle.Maturity.DEVELOPMENT
} }

View File

@ -109,7 +109,7 @@ public operator fun Meta.get(name: Name): Meta? = this.getMeta(name)
/** /**
* Parse [Name] from [key] using full name notation and pass it to [Meta.get] * Parse [Name] from [key] using full name notation and pass it to [Meta.get]
*/ */
public operator fun Meta.get(key: String): Meta? = this.get(Name.parse(key)) public operator fun Meta.get(key: String): Meta? = this[Name.parse(key)]
/** /**
* Get all items matching given name. The index of the last element, if present is used as a [Regex], * Get all items matching given name. The index of the last element, if present is used as a [Regex],

View File

@ -23,9 +23,7 @@ public annotation class MetaBuilder
public interface MutableMetaProvider : MetaProvider, MutableValueProvider { public interface MutableMetaProvider : MetaProvider, MutableValueProvider {
override fun getMeta(name: Name): MutableMeta? override fun getMeta(name: Name): MutableMeta?
public fun setMeta(name: Name, node: Meta?) public fun setMeta(name: Name, node: Meta?)
override fun setValue(name: Name, value: Value?) { override fun setValue(name: Name, value: Value?)
getMeta(name)?.value = value
}
} }
/** /**
@ -144,12 +142,12 @@ public interface MutableMeta : Meta, MutableMetaProvider {
/** /**
* Set or replace node at given [name] * Set or replace node at given [name]
*/ */
public operator fun MutableMeta.set(name: Name, meta: Meta): Unit = setMeta(name, meta) public operator fun MutableMetaProvider.set(name: Name, meta: Meta): Unit = setMeta(name, meta)
/** /**
* Set or replace value at given [name] * Set or replace value at given [name]
*/ */
public operator fun MutableMeta.set(name: Name, value: Value?): Unit = setValue(name, value) public operator fun MutableValueProvider.set(name: Name, value: Value?): Unit = setValue(name, value)
public fun MutableMeta.getOrCreate(key: String): MutableMeta = getOrCreate(Name.parse(key)) public fun MutableMeta.getOrCreate(key: String): MutableMeta = getOrCreate(Name.parse(key))
@ -179,26 +177,6 @@ public fun MutableMetaProvider.remove(key: String) {
public operator fun MutableMetaProvider.set(Key: NameToken, value: Meta): Unit = setMeta(Key.asName(), value) public operator fun MutableMetaProvider.set(Key: NameToken, value: Meta): Unit = setMeta(Key.asName(), value)
public operator fun MutableMetaProvider.set(key: String, value: Meta): Unit = setMeta(Name.parse(key), value) public operator fun MutableMetaProvider.set(key: String, value: Meta): Unit = setMeta(Name.parse(key), value)
//value setters
public operator fun MutableMeta.set(name: NameToken, value: Value?): Unit = set(name.asName(), value)
public operator fun MutableMeta.set(key: String, value: Value?): Unit = set(Name.parse(key), value)
public operator fun MutableMeta.set(name: Name, value: String): Unit = set(name, value.asValue())
public operator fun MutableMeta.set(name: NameToken, value: String): Unit = set(name.asName(), value.asValue())
public operator fun MutableMeta.set(key: String, value: String): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableMeta.set(name: Name, value: Boolean): Unit = set(name, value.asValue())
public operator fun MutableMeta.set(name: NameToken, value: Boolean): Unit = set(name.asName(), value.asValue())
public operator fun MutableMeta.set(key: String, value: Boolean): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableMeta.set(name: Name, value: Number): Unit = set(name, value.asValue())
public operator fun MutableMeta.set(name: NameToken, value: Number): Unit = set(name.asName(), value.asValue())
public operator fun MutableMeta.set(key: String, value: Number): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableMeta.set(name: Name, value: List<Value>): Unit = set(name, value.asValue())
public operator fun MutableMeta.set(name: NameToken, value: List<Value>): Unit = set(name.asName(), value.asValue())
public operator fun MutableMeta.set(key: String, value: List<Value>): Unit = set(Name.parse(key), value.asValue())
//public fun MutableMeta.set(key: String, index: String, value: Value?): Unit = //public fun MutableMeta.set(key: String, index: String, value: Value?): Unit =
// set(key.toName().withIndex(index), value) // set(key.toName().withIndex(index), value)
@ -207,7 +185,7 @@ public operator fun MutableMeta.set(key: String, value: List<Value>): Unit = set
/* Same name siblings generation */ /* Same name siblings generation */
public fun MutableMeta.setIndexed( public fun MutableMetaProvider.setIndexed(
name: Name, name: Name,
metas: Iterable<Meta>, metas: Iterable<Meta>,
indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() }, indexFactory: (Meta, index: Int) -> String = { _, index -> index.toString() },
@ -221,10 +199,10 @@ public fun MutableMeta.setIndexed(
} }
} }
public operator fun MutableMeta.set(name: Name, metas: Iterable<Meta>): Unit = public operator fun MutableMetaProvider.set(name: Name, metas: Iterable<Meta>): Unit =
setIndexed(name, metas) setIndexed(name, metas)
public operator fun MutableMeta.set(key: String, metas: Iterable<Meta>): Unit = public operator fun MutableMetaProvider.set(key: String, metas: Iterable<Meta>): Unit =
setIndexed(Name.parse(key), metas) setIndexed(Name.parse(key), metas)
@ -234,7 +212,7 @@ public operator fun MutableMeta.set(key: String, metas: Iterable<Meta>): Unit =
* * node updates node and replaces anything but node * * node updates node and replaces anything but node
* * node list updates node list if number of nodes in the list is the same and replaces anything otherwise * * node list updates node list if number of nodes in the list is the same and replaces anything otherwise
*/ */
public fun MutableMeta.update(meta: Meta) { public fun MutableMetaProvider.update(meta: Meta) {
meta.valueSequence().forEach { (name, value) -> meta.valueSequence().forEach { (name, value) ->
set(name, value) set(name, value)
} }

View File

@ -8,6 +8,7 @@ import space.kscience.dataforge.names.length
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.ValueType import space.kscience.dataforge.values.ValueType
import space.kscience.dataforge.values.asValue import space.kscience.dataforge.values.asValue
import space.kscience.dataforge.values.set
import kotlin.collections.set import kotlin.collections.set
public class MetaDescriptorBuilder @PublishedApi internal constructor() { public class MetaDescriptorBuilder @PublishedApi internal constructor() {

View File

@ -6,6 +6,7 @@ import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.values.ListValue import space.kscience.dataforge.values.ListValue
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.setValue
/** /**
* Convert meta to map of maps * Convert meta to map of maps
@ -43,7 +44,7 @@ public fun Map<String, Any?>.toMeta(@Suppress("UNUSED_PARAMETER") descriptor: Me
if (value is List<*>) { if (value is List<*>) {
val items = value.map { toMeta(it) } val items = value.map { toMeta(it) }
if (items.all { it.isLeaf }) { if (items.all { it.isLeaf }) {
set(key, ListValue(items.map { it.value!! })) setValue(key, ListValue(items.map { it.value!! }))
} else { } else {
setIndexed(Name.parse(key), value.map { toMeta(it) }) setIndexed(Name.parse(key), value.map { toMeta(it) })
} }

View File

@ -1,6 +1,9 @@
package space.kscience.dataforge.values package space.kscience.dataforge.values
import space.kscience.dataforge.meta.set
import space.kscience.dataforge.names.Name import space.kscience.dataforge.names.Name
import space.kscience.dataforge.names.NameToken
import space.kscience.dataforge.names.asName
/** /**
* An object that could provide values * An object that could provide values
@ -21,3 +24,24 @@ public interface MutableValueProvider : ValueProvider {
public fun MutableValueProvider.setValue(key: String, value: Value?) { public fun MutableValueProvider.setValue(key: String, value: Value?) {
setValue(Name.parse(key), value) setValue(Name.parse(key), value)
} }
//value setters
public operator fun MutableValueProvider.set(name: NameToken, value: Value?): Unit = set(name.asName(), value)
public operator fun MutableValueProvider.set(key: String, value: Value?): Unit = set(Name.parse(key), value)
public operator fun MutableValueProvider.set(name: Name, value: String): Unit = set(name, value.asValue())
public operator fun MutableValueProvider.set(name: NameToken, value: String): Unit = set(name.asName(), value.asValue())
public operator fun MutableValueProvider.set(key: String, value: String): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableValueProvider.set(name: Name, value: Boolean): Unit = set(name, value.asValue())
public operator fun MutableValueProvider.set(name: NameToken, value: Boolean): Unit = set(name.asName(), value.asValue())
public operator fun MutableValueProvider.set(key: String, value: Boolean): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableValueProvider.set(name: Name, value: Number): Unit = set(name, value.asValue())
public operator fun MutableValueProvider.set(name: NameToken, value: Number): Unit = set(name.asName(), value.asValue())
public operator fun MutableValueProvider.set(key: String, value: Number): Unit = set(Name.parse(key), value.asValue())
public operator fun MutableValueProvider.set(name: Name, value: List<Value>): Unit = set(name, value.asValue())
public operator fun MutableValueProvider.set(name: NameToken, value: List<Value>): Unit = set(name.asName(), value.asValue())
public operator fun MutableValueProvider.set(key: String, value: List<Value>): Unit = set(Name.parse(key), value.asValue())

View File

@ -1,5 +1,6 @@
package space.kscience.dataforge.meta package space.kscience.dataforge.meta
import space.kscience.dataforge.values.set
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals

View File

@ -2,6 +2,7 @@ package space.kscience.dataforge.meta
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.asValue import space.kscience.dataforge.values.asValue
import space.kscience.dataforge.values.set
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals

View File

@ -1,5 +1,6 @@
package space.kscience.dataforge.meta package space.kscience.dataforge.meta
import space.kscience.dataforge.values.set
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals

View File

@ -4,6 +4,7 @@ import space.kscience.dataforge.misc.DFExperimental
import space.kscience.dataforge.values.NumberValue import space.kscience.dataforge.values.NumberValue
import space.kscience.dataforge.values.True import space.kscience.dataforge.values.True
import space.kscience.dataforge.values.Value import space.kscience.dataforge.values.Value
import space.kscience.dataforge.values.set
import kotlin.test.Test import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
kotlin { kotlin {

View File

@ -1,5 +1,5 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
} }
kotlin { kotlin {
@ -25,5 +25,5 @@ kotlin {
} }
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.PROTOTYPE maturity = space.kscience.gradle.Maturity.PROTOTYPE
} }

View File

@ -1,6 +1,6 @@
plugins { plugins {
id("ru.mipt.npm.gradle.mpp") id("space.kscience.gradle.mpp")
id("ru.mipt.npm.gradle.native") id("space.kscience.gradle.native")
} }
kscience{ kscience{
@ -20,5 +20,5 @@ kotlin {
} }
readme{ readme{
maturity = ru.mipt.npm.gradle.Maturity.EXPERIMENTAL maturity = space.kscience.gradle.Maturity.EXPERIMENTAL
} }

View File

@ -3,6 +3,6 @@ org.gradle.jvmargs=-Xmx4096m
kotlin.code.style=official kotlin.code.style=official
kotlin.mpp.stability.nowarn=true kotlin.mpp.stability.nowarn=true
#kotlin.incremental.js.ir=true kotlin.incremental.js.ir=true
toolsVersion=0.11.7-kotlin-1.7.0 toolsVersion=0.12.0-kotlin-1.7.20-Beta

View File

@ -15,10 +15,10 @@ pluginManagement {
} }
plugins { plugins {
id("ru.mipt.npm.gradle.project") version toolsVersion id("space.kscience.gradle.project") version toolsVersion
id("ru.mipt.npm.gradle.mpp") version toolsVersion id("space.kscience.gradle.mpp") version toolsVersion
id("ru.mipt.npm.gradle.jvm") version toolsVersion id("space.kscience.gradle.jvm") version toolsVersion
id("ru.mipt.npm.gradle.js") version toolsVersion id("space.kscience.gradle.js") version toolsVersion
} }
} }
@ -34,7 +34,7 @@ dependencyResolutionManagement {
versionCatalogs { versionCatalogs {
create("npmlibs") { create("npmlibs") {
from("ru.mipt.npm:version-catalog:$toolsVersion") from("space.kscience:version-catalog:$toolsVersion")
} }
} }
} }