optional descriptor for descriptor builder

This commit is contained in:
Alexander Nozik 2019-11-01 21:15:55 +03:00
parent 33b1de2865
commit aefee64581
3 changed files with 24 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package hep.dataforge.io package hep.dataforge.io.serialization
import hep.dataforge.io.serialization.descriptor import hep.dataforge.io.toJson
import hep.dataforge.io.toMeta
import hep.dataforge.meta.* import hep.dataforge.meta.*
import hep.dataforge.names.NameToken import hep.dataforge.names.NameToken
import hep.dataforge.values.* import hep.dataforge.values.*
@ -16,10 +17,10 @@ object ValueSerializer : KSerializer<Value> {
private val valueTypeSerializer = EnumSerializer(ValueType::class) private val valueTypeSerializer = EnumSerializer(ValueType::class)
private val listSerializer by lazy { ArrayListSerializer(ValueSerializer) } private val listSerializer by lazy { ArrayListSerializer(ValueSerializer) }
override val descriptor: SerialDescriptor = descriptor("Value") { override val descriptor: SerialDescriptor = descriptor("hep.dataforge.values.Value") {
boolean("isList") boolean("isList")
enum<ValueType>("valueType") enum<ValueType>("valueType")
element("value", PolymorphicClassDescriptor) element("value", null)
} }
private fun Decoder.decodeValue(): Value { private fun Decoder.decodeValue(): Value {
@ -69,7 +70,7 @@ object ValueSerializer : KSerializer<Value> {
object MetaItemSerializer : KSerializer<MetaItem<*>> { object MetaItemSerializer : KSerializer<MetaItem<*>> {
override val descriptor: SerialDescriptor = descriptor("MetaItem") { override val descriptor: SerialDescriptor = descriptor("MetaItem") {
boolean("isNode") boolean("isNode")
element("value", PolymorphicClassDescriptor) element("value", null)
} }
@ -93,17 +94,21 @@ object MetaItemSerializer : KSerializer<MetaItem<*>> {
private class DeserializedMeta(override val items: Map<NameToken, MetaItem<*>>) : MetaBase() private class DeserializedMeta(override val items: Map<NameToken, MetaItem<*>>) : MetaBase()
/** /**
* Serialized for meta * Serialized for meta
*/ */
@Serializer(Meta::class) @Serializer(Meta::class)
object MetaSerializer : KSerializer<Meta> { object MetaSerializer : KSerializer<Meta> {
private val mapSerializer = private val mapSerializer = HashMapSerializer(
HashMapSerializer(StringSerializer, MetaItemSerializer) StringSerializer,
MetaItemSerializer
)
override val descriptor: SerialDescriptor = override val descriptor: SerialDescriptor = NamedMapClassDescriptor(
NamedMapClassDescriptor("Meta", StringSerializer.descriptor, MetaItemSerializer.descriptor) "hep.dataforge.meta.Meta",
StringSerializer.descriptor,
MetaItemSerializer.descriptor
)
override fun deserialize(decoder: Decoder): Meta { override fun deserialize(decoder: Decoder): Meta {
return if (decoder is JsonInput) { return if (decoder is JsonInput) {

View File

@ -12,12 +12,12 @@ import kotlinx.serialization.internal.*
inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) { inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun element( fun element(
name: String, name: String,
descriptor: SerialDescriptor, descriptor: SerialDescriptor?,
isOptional: Boolean = false, isOptional: Boolean = false,
vararg annotations: Annotation vararg annotations: Annotation
) { ) {
impl.addElement(name, isOptional) impl.addElement(name, isOptional)
impl.pushDescriptor(descriptor) descriptor?.let { impl.pushDescriptor(descriptor) }
annotations.forEach { annotations.forEach {
impl.pushAnnotation(it) impl.pushAnnotation(it)
} }
@ -57,7 +57,7 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun doubleArray(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = fun doubleArray(name: String, isOptional: Boolean = false, vararg annotations: Annotation) =
element(name, DoubleArraySerializer.descriptor, isOptional, *annotations) element(name, DoubleArraySerializer.descriptor, isOptional, *annotations)
inline fun <reified E: Enum<E>> enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) = inline fun <reified E : Enum<E>> enum(name: String, isOptional: Boolean = false, vararg annotations: Annotation) =
element(name, EnumSerializer(E::class).descriptor, isOptional, *annotations) element(name, EnumSerializer(E::class).descriptor, isOptional, *annotations)
fun classAnnotation(a: Annotation) = impl.pushClassAnnotation(a) fun classAnnotation(a: Annotation) = impl.pushClassAnnotation(a)
@ -65,7 +65,10 @@ inline class SerialDescriptorBuilder(private val impl: SerialClassDescImpl) {
fun build(): SerialDescriptor = impl fun build(): SerialDescriptor = impl
} }
inline fun KSerializer<*>.descriptor(name: String, block: SerialDescriptorBuilder.() -> Unit): SerialDescriptor = inline fun <reified T : Any> KSerializer<T>.descriptor(
name: String,
block: SerialDescriptorBuilder.() -> Unit
): SerialDescriptor =
SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build() SerialDescriptorBuilder(SerialClassDescImpl(name)).apply(block).build()
fun Decoder.decodeStructure( fun Decoder.decodeStructure(

View File

@ -1,5 +1,7 @@
package hep.dataforge.io package hep.dataforge.io
import hep.dataforge.io.serialization.MetaItemSerializer
import hep.dataforge.io.serialization.MetaSerializer
import hep.dataforge.io.serialization.NameSerializer import hep.dataforge.io.serialization.NameSerializer
import hep.dataforge.meta.buildMeta import hep.dataforge.meta.buildMeta
import hep.dataforge.names.toName import hep.dataforge.names.toName