|
| 1 | +// Copyright (c) 2023 - Restate Software, Inc., Restate GmbH |
| 2 | +// |
| 3 | +// This file is part of the Restate Java SDK, |
| 4 | +// which is released under the MIT license. |
| 5 | +// |
| 6 | +// You can find a copy of the license in file LICENSE in the root |
| 7 | +// directory of this repository or package, or at |
| 8 | +// https://github.com/restatedev/sdk-java/blob/main/LICENSE |
| 9 | +package dev.restate.sdk.kotlin |
| 10 | + |
| 11 | +import dev.restate.common.Slice |
| 12 | +import dev.restate.sdk.common.DurablePromiseKey |
| 13 | +import dev.restate.sdk.common.StateKey |
| 14 | +import dev.restate.serde.Serde |
| 15 | +import dev.restate.serde.Serde.Schema |
| 16 | +import java.nio.charset.StandardCharsets |
| 17 | +import kotlin.reflect.typeOf |
| 18 | +import kotlinx.serialization.ExperimentalSerializationApi |
| 19 | +import kotlinx.serialization.KSerializer |
| 20 | +import kotlinx.serialization.Serializable |
| 21 | +import kotlinx.serialization.builtins.ListSerializer |
| 22 | +import kotlinx.serialization.builtins.serializer |
| 23 | +import kotlinx.serialization.descriptors.PrimitiveKind |
| 24 | +import kotlinx.serialization.descriptors.SerialDescriptor |
| 25 | +import kotlinx.serialization.descriptors.StructureKind |
| 26 | +import kotlinx.serialization.encodeToString |
| 27 | +import kotlinx.serialization.json.Json |
| 28 | +import kotlinx.serialization.json.JsonArray |
| 29 | +import kotlinx.serialization.json.JsonElement |
| 30 | +import kotlinx.serialization.json.JsonNull |
| 31 | +import kotlinx.serialization.json.JsonTransformingSerializer |
| 32 | +import kotlinx.serialization.serializer |
| 33 | + |
| 34 | +@Deprecated("Use stateKey() instead") |
| 35 | +object KtStateKey { |
| 36 | + |
| 37 | + /** Creates a json [StateKey]. */ |
| 38 | + @Deprecated("Use stateKey() instead", replaceWith = ReplaceWith(expression = "stateKey()")) |
| 39 | + inline fun <reified T> json(name: String): StateKey<T> { |
| 40 | + return StateKey.of(name, KtSerdes.json()) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +@Deprecated("Use durablePromiseKey() instead") |
| 45 | +object KtDurablePromiseKey { |
| 46 | + |
| 47 | + /** Creates a json [StateKey]. */ |
| 48 | + @Deprecated( |
| 49 | + "Use durablePromiseKey() instead", |
| 50 | + replaceWith = ReplaceWith(expression = "durablePromiseKey()")) |
| 51 | + inline fun <reified T : Any?> json(name: String): DurablePromiseKey<T> { |
| 52 | + return DurablePromiseKey.of(name, KtSerdes.json()) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +@Deprecated("Moved to dev.restate.serde.kotlinx") |
| 57 | +object KtSerdes { |
| 58 | + |
| 59 | + @Deprecated("Moved to dev.restate.serde.kotlinx") |
| 60 | + inline fun <reified T : Any?> json(): Serde<T> { |
| 61 | + @Suppress("UNCHECKED_CAST") |
| 62 | + return when (typeOf<T>()) { |
| 63 | + typeOf<Unit>() -> UNIT as Serde<T> |
| 64 | + else -> json(serializer()) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Deprecated("Moved to dev.restate.serde.kotlinx") |
| 69 | + val UNIT: Serde<Unit> = |
| 70 | + object : Serde<Unit> { |
| 71 | + override fun serialize(value: Unit?): Slice { |
| 72 | + return Slice.EMPTY |
| 73 | + } |
| 74 | + |
| 75 | + override fun deserialize(value: Slice) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + override fun contentType(): String? { |
| 80 | + return null |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Deprecated("Moved to dev.restate.serde.kotlinx") |
| 85 | + inline fun <reified T : Any?> json(serializer: KSerializer<T>): Serde<T> { |
| 86 | + return object : Serde<T> { |
| 87 | + override fun serialize(value: T?): Slice { |
| 88 | + if (value == null) { |
| 89 | + return Slice.wrap( |
| 90 | + Json.encodeToString(JsonNull.serializer(), JsonNull).encodeToByteArray()) |
| 91 | + } |
| 92 | + |
| 93 | + return Slice.wrap(Json.encodeToString(serializer, value).encodeToByteArray()) |
| 94 | + } |
| 95 | + |
| 96 | + override fun deserialize(value: Slice): T { |
| 97 | + return Json.decodeFromString( |
| 98 | + serializer, String(value.toByteArray(), StandardCharsets.UTF_8)) |
| 99 | + } |
| 100 | + |
| 101 | + override fun contentType(): String { |
| 102 | + return "application/json" |
| 103 | + } |
| 104 | + |
| 105 | + override fun jsonSchema(): Schema { |
| 106 | + val schema: JsonSchema = serializer.descriptor.jsonSchema() |
| 107 | + return Serde.StringifiedJsonSchema(Json.encodeToString(schema)) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + @Deprecated("Moved to dev.restate.serde.kotlinx") |
| 113 | + @Serializable |
| 114 | + @PublishedApi |
| 115 | + internal data class JsonSchema( |
| 116 | + @Serializable(with = StringListSerializer::class) val type: List<String>? = null, |
| 117 | + val format: String? = null, |
| 118 | + ) { |
| 119 | + companion object { |
| 120 | + val INT = JsonSchema(type = listOf("number"), format = "int32") |
| 121 | + |
| 122 | + val LONG = JsonSchema(type = listOf("number"), format = "int64") |
| 123 | + |
| 124 | + val DOUBLE = JsonSchema(type = listOf("number"), format = "double") |
| 125 | + |
| 126 | + val FLOAT = JsonSchema(type = listOf("number"), format = "float") |
| 127 | + |
| 128 | + val STRING = JsonSchema(type = listOf("string")) |
| 129 | + |
| 130 | + val BOOLEAN = JsonSchema(type = listOf("boolean")) |
| 131 | + |
| 132 | + val OBJECT = JsonSchema(type = listOf("object")) |
| 133 | + |
| 134 | + val LIST = JsonSchema(type = listOf("array")) |
| 135 | + |
| 136 | + val ANY = JsonSchema() |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + object StringListSerializer : |
| 141 | + JsonTransformingSerializer<List<String>>(ListSerializer(String.Companion.serializer())) { |
| 142 | + override fun transformSerialize(element: JsonElement): JsonElement { |
| 143 | + require(element is JsonArray) |
| 144 | + return element.singleOrNull() ?: element |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + @Deprecated("Moved to dev.restate.serde.kotlinx") |
| 149 | + @OptIn(ExperimentalSerializationApi::class) |
| 150 | + @PublishedApi |
| 151 | + internal fun SerialDescriptor.jsonSchema(): JsonSchema { |
| 152 | + var schema = |
| 153 | + when (this.kind) { |
| 154 | + PrimitiveKind.BOOLEAN -> JsonSchema.BOOLEAN |
| 155 | + PrimitiveKind.BYTE -> JsonSchema.INT |
| 156 | + PrimitiveKind.CHAR -> JsonSchema.STRING |
| 157 | + PrimitiveKind.DOUBLE -> JsonSchema.DOUBLE |
| 158 | + PrimitiveKind.FLOAT -> JsonSchema.FLOAT |
| 159 | + PrimitiveKind.INT -> JsonSchema.INT |
| 160 | + PrimitiveKind.LONG -> JsonSchema.LONG |
| 161 | + PrimitiveKind.SHORT -> JsonSchema.INT |
| 162 | + PrimitiveKind.STRING -> JsonSchema.STRING |
| 163 | + StructureKind.LIST -> JsonSchema.LIST |
| 164 | + StructureKind.MAP -> JsonSchema.OBJECT |
| 165 | + else -> JsonSchema.ANY |
| 166 | + } |
| 167 | + |
| 168 | + // Add nullability constraint |
| 169 | + if (this.isNullable && schema.type != null) { |
| 170 | + schema = schema.copy(type = schema.type.plus("null")) |
| 171 | + } |
| 172 | + |
| 173 | + return schema |
| 174 | + } |
| 175 | +} |
0 commit comments