All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.Id.kt Maven / Gradle / Ivy

The newest version!
package opensavvy.spine

import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder

@Serializable(with = Id.Serializer::class)
data class Id(
	val service: Route.Segment,
	val resource: Route,
) {

	constructor(service: String, resource: Route) : this(Route.Segment(service), resource)

	override fun toString() = "$service/$resource"

	//region Serializer
	internal object Serializer : KSerializer {
		override val descriptor = PrimitiveSerialDescriptor("opensavvy.spine.Id", PrimitiveKind.STRING)

		override fun serialize(encoder: Encoder, value: Id) {
			encoder.encodeString("${value.service}/${value.resource}")
		}

		override fun deserialize(decoder: Decoder): Id {
			val segments = decoder.decodeString().split('/')
				.map { Route.Segment(it) }

			return Id(segments.first(), Route(segments.drop(1)))
		}
	}
	//endregion
}

@Serializable
data class Identified(
	val id: Id,
	val value: T,
)




© 2015 - 2024 Weber Informatics LLC | Privacy Policy