MetaProvider spec is replaced by readable. listOfSpec replaced with listOfReadable

This commit is contained in:
Alexander Nozik 2024-06-05 10:53:35 +03:00
parent c62dc2c698
commit bb9afb329f
4 changed files with 84 additions and 18 deletions

View File

@ -7,6 +7,7 @@
### Changed ### Changed
### Deprecated ### Deprecated
- MetaProvider `spec` is replaced by `readable`. `listOfSpec` replaced with `listOfReadable`
### Removed ### Removed

View File

@ -9,7 +9,7 @@ plugins {
allprojects { allprojects {
group = "space.kscience" group = "space.kscience"
version = "0.9.0" version = "0.9.1-dev-1"
} }
subprojects { subprojects {

View File

@ -24,20 +24,45 @@ public fun MetaProvider.node(
} }
} }
/** /**
* Use [metaReader] to read the Meta node * Use [reader] to read the Meta node
*/ */
public fun <T> MetaProvider.spec( public fun <T> MetaProvider.readable(
metaReader: MetaReader<T>, reader: MetaReader<T>,
key: Name? = null, key: Name? = null,
): MetaDelegate<T?> = object : MetaDelegate<T?> { ): MetaDelegate<T?> = object : MetaDelegate<T?> {
override val descriptor: MetaDescriptor? get() = metaReader.descriptor override val descriptor: MetaDescriptor? get() = reader.descriptor
override fun getValue(thisRef: Any?, property: KProperty<*>): T? { override fun getValue(thisRef: Any?, property: KProperty<*>): T? {
return get(key ?: property.name.asName())?.let { metaReader.read(it) } return get(key ?: property.name.asName())?.let { reader.read(it) }
} }
} }
/**
* Use [reader] to read the Meta node or return [default] if node does not exist
*/
public fun <T> MetaProvider.readable(
reader: MetaReader<T>,
default: T,
key: Name? = null,
): MetaDelegate<T> = object : MetaDelegate<T> {
override val descriptor: MetaDescriptor? get() = reader.descriptor
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
return get(key ?: property.name.asName())?.let { reader.read(it) } ?: default
}
}
/**
* Use [reader] to read the Meta node
*/
@Deprecated("Replace with reading", ReplaceWith("reading(metaReader, key)"))
public fun <T> MetaProvider.spec(
reader: MetaReader<T>,
key: Name? = null,
): MetaDelegate<T?> = readable(reader, key)
/** /**
* Use object serializer to transform it to Meta and back * Use object serializer to transform it to Meta and back
*/ */
@ -45,34 +70,51 @@ public fun <T> MetaProvider.spec(
public inline fun <reified T> MetaProvider.serializable( public inline fun <reified T> MetaProvider.serializable(
key: Name? = null, key: Name? = null,
descriptor: MetaDescriptor? = null, descriptor: MetaDescriptor? = null,
): MetaDelegate<T?> = spec(MetaConverter.serializable(descriptor), key) ): MetaDelegate<T?> = readable(MetaConverter.serializable(descriptor), key)
@DFExperimental
public inline fun <reified T> MetaProvider.serializable(
key: Name? = null,
default: T,
descriptor: MetaDescriptor? = null,
): MetaDelegate<T> = readable(MetaConverter.serializable(descriptor), default, key)
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)")) @Deprecated("Use convertable", ReplaceWith("convertable(converter, key)"))
public fun <T> MetaProvider.node( public fun <T> MetaProvider.node(
key: Name? = null, key: Name? = null,
converter: MetaReader<T>, converter: MetaReader<T>,
): ReadOnlyProperty<Any?, T?> = spec(converter, key) ): ReadOnlyProperty<Any?, T?> = readable(converter, key)
/** /**
* Use [converter] to convert a list of same name siblings meta to object * Use [reader] to convert a list of same name siblings meta to object
*/ */
public fun <T> Meta.listOfSpec( public fun <T> Meta.listOfReadable(
converter: MetaReader<T>, reader: MetaReader<T>,
key: Name? = null, key: Name? = null,
): MetaDelegate<List<T>> = object : MetaDelegate<List<T>> { ): MetaDelegate<List<T>> = object : MetaDelegate<List<T>> {
override fun getValue(thisRef: Any?, property: KProperty<*>): List<T> { override fun getValue(thisRef: Any?, property: KProperty<*>): List<T> {
val name = key ?: property.name.asName() val name = key ?: property.name.asName()
return getIndexed(name).values.map { converter.read(it) } return getIndexed(name).values.map { reader.read(it) }
} }
override val descriptor: MetaDescriptor? = converter.descriptor?.copy(multiple = true) override val descriptor: MetaDescriptor? = reader.descriptor?.copy(multiple = true)
} }
/**
* Use [converter] to convert a list of same name siblings meta to object
*/
@Deprecated("Replace with readingList", ReplaceWith("readingList(converter, key)"))
public fun <T> Meta.listOfSpec(
converter: MetaReader<T>,
key: Name? = null,
): MetaDelegate<List<T>> = listOfReadable(converter, key)
@DFExperimental @DFExperimental
public inline fun <reified T> Meta.listOfSerializable( public inline fun <reified T> Meta.listOfSerializable(
key: Name? = null, key: Name? = null,
descriptor: MetaDescriptor? = null, descriptor: MetaDescriptor? = null,
): MetaDelegate<List<T>> = listOfSpec(MetaConverter.serializable(descriptor), key) ): MetaDelegate<List<T>> = listOfReadable(MetaConverter.serializable(descriptor), key)
/** /**
* A property delegate that uses custom key * A property delegate that uses custom key

View File

@ -54,9 +54,25 @@ public fun <T> MutableMetaProvider.convertable(
} }
} }
@Deprecated("Use convertable", ReplaceWith("convertable(converter, key)")) public fun <T> MutableMetaProvider.convertable(
public fun <T> MutableMetaProvider.node(key: Name? = null, converter: MetaConverter<T>): MutableMetaDelegate<T?> = converter: MetaConverter<T>,
convertable(converter, key) default: T,
key: Name? = null,
): MutableMetaDelegate<T> = object : MutableMetaDelegate<T> {
override val descriptor: MetaDescriptor? get() = converter.descriptor
override fun getValue(thisRef: Any?, property: KProperty<*>): T {
val name = key ?: property.name.asName()
return get(name)?.let { converter.read(it) } ?: default
}
override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) {
val name = key ?: property.name.asName()
set(name, value?.let { converter.convert(it) })
}
}
/** /**
* Use object serializer to transform it to Meta and back. * Use object serializer to transform it to Meta and back.
@ -66,7 +82,14 @@ public fun <T> MutableMetaProvider.node(key: Name? = null, converter: MetaConver
public inline fun <reified T> MutableMetaProvider.serializable( public inline fun <reified T> MutableMetaProvider.serializable(
descriptor: MetaDescriptor? = null, descriptor: MetaDescriptor? = null,
key: Name? = null, key: Name? = null,
): MutableMetaDelegate<T?> = convertable(MetaConverter.serializable(descriptor), key) ): MutableMetaDelegate<T?> = convertable<T>(MetaConverter.serializable(descriptor), key)
@DFExperimental
public inline fun <reified T> MutableMetaProvider.serializable(
descriptor: MetaDescriptor? = null,
default: T,
key: Name? = null,
): MutableMetaDelegate<T> = convertable(MetaConverter.serializable(descriptor), default, key)
/** /**
* Use [converter] to convert a list of same name siblings meta to object and back. * Use [converter] to convert a list of same name siblings meta to object and back.