Skip to content

Commit

Permalink
Change serialize signature
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte committed Oct 8, 2024
1 parent f452e5c commit 937c39f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public fun Route.sse(handler: suspend SSESession.() -> Unit): Unit = processSSE(
*/
public fun Route.sse(
path: String,
serialize: (TypeInfo) -> (Any) -> String = { { it.toString() } },
serialize: (TypeInfo, Any) -> String = { _, it -> it.toString() },
handler: suspend SSESessionWithSerialization.() -> Unit
) {
route(path, HttpMethod.Get) {
Expand All @@ -70,12 +70,12 @@ public fun Route.sse(
* @see SSESessionWithSerialization
*/
public fun Route.sse(
serialize: (TypeInfo) -> (Any) -> String = { { it.toString() } },
serialize: (TypeInfo, Any) -> String = { _, it -> it.toString() },
handler: suspend SSESessionWithSerialization.() -> Unit
): Unit = processSSE(serialize, handler)

private fun Route.processSSE(
serialize: ((TypeInfo) -> (Any) -> String)?,
serialize: ((TypeInfo, Any) -> String)?,
handler: suspend SSESessionWithSerialization.() -> Unit
) {
plugin(SSE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import kotlinx.coroutines.*
*/
public class SSEServerContent<T : SSESession>(
public val call: ApplicationCall,
public val serialize: ((TypeInfo) -> (Any) -> String)?,
public val serialize: ((TypeInfo, Any) -> String)?,
public val handle: suspend T.() -> Unit
) : OutgoingContent.WriteChannelContent() {
override val contentType: ContentType = ContentType.Text.EventStream
Expand All @@ -40,7 +40,7 @@ public class SSEServerContent<T : SSESession>(
session = DefaultServerSSESession(channel, call, coroutineContext) as T
if (serialize != null) {
session = object : SSESessionWithSerialization, SSESession by session as DefaultServerSSESession {
override val serializer: (TypeInfo) -> (Any) -> String = serialize
override val serializer: (TypeInfo, Any) -> String = serialize
} as T
}
session?.handle()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public interface SSESessionWithSerialization : SSESession {
/**
* Serializer for transforming data object into field `data` of `ServerSentEvent`.
*/
public val serializer: (TypeInfo) -> (Any) -> String
public val serializer: (TypeInfo, Any) -> String
}

public suspend inline fun <reified T : Any> SSESessionWithSerialization.sendSerialized(
Expand All @@ -77,7 +77,7 @@ public suspend inline fun <reified T : Any> SSESessionWithSerialization.sendSeri
send(
ServerSentEvent(
event.data?.let {
serializer(typeInfo<T>()).invoke(it)
serializer(typeInfo<T>(), it)
},
event.event,
event.id,
Expand All @@ -98,5 +98,5 @@ public suspend inline fun <reified T : Any> SSESessionWithSerialization.sendSeri
}

public suspend inline fun <reified T : Any> SSESessionWithSerialization.sendSerialized(data: T) {
send(ServerSentEvent(serializer(typeInfo<T>()).invoke(data)))
send(ServerSentEvent(serializer(typeInfo<T>(), data)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,16 +201,14 @@ class ServerSentEventsTest {
fun testSerializerInRoute() = testApplication {
install(SSE)
routing {
sse("/person", serialize = { typeInfo ->
{ data ->
when (typeInfo.type) {
Person1::class -> {
"Age ${(data as Person1).age}"
}

else -> {
data.toString()
}
sse("/person", serialize = { typeInfo, data ->
when (typeInfo.type) {
Person1::class -> {
"Age ${(data as Person1).age}"
}

else -> {
data.toString()
}
}
}) {
Expand All @@ -233,20 +231,18 @@ class ServerSentEventsTest {
fun testDifferentSerializers() = testApplication {
install(SSE)
routing {
sse(serialize = { typeInfo ->
{ data ->
when (typeInfo.type) {
Person1::class -> {
"Age ${(data as Person1).age}"
}

Person2::class -> {
"Number ${(data as Person2).number}"
}

else -> {
data.toString()
}
sse(serialize = { typeInfo, data ->
when (typeInfo.type) {
Person1::class -> {
"Age ${(data as Person1).age}"
}

Person2::class -> {
"Number ${(data as Person2).number}"
}

else -> {
data.toString()
}
}
}) {
Expand Down Expand Up @@ -279,11 +275,9 @@ class ServerSentEventsTest {
fun testJsonDeserializer() = testApplication {
install(SSE)
routing {
sse("/json", serialize = { typeInfo ->
{
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.encodeToString(serializer, it)
}
sse("/json", serialize = { typeInfo, it ->
val serializer = Json.serializersModule.serializer(typeInfo.kotlinType!!)
Json.encodeToString(serializer, it)
}) {
sendSerialized(Customer(0, "Jet", "Brains"))
sendSerialized(Product(0, listOf(100, 200)))
Expand Down

0 comments on commit 937c39f

Please sign in to comment.