diff --git a/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/schema/Type.kt b/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/schema/Type.kt index b740685..33994c1 100644 --- a/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/schema/Type.kt +++ b/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/schema/Type.kt @@ -284,6 +284,8 @@ fun Type.nullable(): Type = if (this is Type.Nullable) this else Type.Nullable(t internal fun Type.fits(standard: Type): Boolean = when { this == standard -> true + // A provider written before handles existed declares its tokens as plain strings. + this == Type.Primitive.STRING && standard is Type.Handle -> true this is Type.Nullable && standard is Type.Nullable -> inner.fits(standard.inner) this is Type.ListOf && standard is Type.ListOf -> item.fits(standard.item) this is Type.MapOf && standard is Type.MapOf -> value.fits(standard.value) diff --git a/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/standard/StandardApi.kt b/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/standard/StandardApi.kt index 25f552d..285d19c 100644 --- a/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/standard/StandardApi.kt +++ b/contract/src/main/kotlin/com/xfl/msgbot/plugin/api/standard/StandardApi.kt @@ -112,7 +112,7 @@ object StandardApi { doc = "Answer a message. Pass its token and its address; the provider uses what it understands.", ) { param("text", Type.STRING) - optional("token", Type.STRING, doc = "The message's replyToken, meaningful to the provider that issued it") + optional("token", Type.handle("reply"), doc = "The message's replyToken, meaningful to the provider that issued it") optional("room", Type.STRING) optional("channelId", Type.STRING) optional("packageName", Type.STRING) @@ -131,7 +131,7 @@ object StandardApi { optional("packageName", Type.STRING) } function("markRead", returns = Type.BOOL, doc = "Mark a message, or its room up to now, as read.") { - optional("token", Type.STRING, doc = "The message's readToken, meaningful to the provider that issued it") + optional("token", Type.handle("read"), doc = "The message's readToken, meaningful to the provider that issued it") optional("room", Type.STRING) optional("channelId", Type.STRING) optional("packageName", Type.STRING) @@ -141,7 +141,7 @@ object StandardApi { returns = Type.BYTES.nullable(), doc = "The bytes behind an image token, while the provider still has them.", ) { - param("token", Type.STRING) + param("token", Type.handle("image")) } event("message") { @@ -152,7 +152,7 @@ object StandardApi { Type.struct { field("name", Type.STRING) optional("hash", Type.STRING, doc = "A stable ID of the author, where the messenger has one") - optional("avatar", Type.STRING, doc = "An image token") + optional("avatar", Type.handle("image"), doc = "An image token") }, ) optional("channelId", Type.STRING, doc = "IDs are strings so no language rounds them.") @@ -163,9 +163,9 @@ object StandardApi { optional("isMention", Type.BOOL) optional("isMultiChat", Type.BOOL) optional("isDebugRoom", Type.BOOL, doc = "Set by the host for the debug room; providers leave it out") - optional("image", Type.STRING, doc = "An image token") - optional("replyToken", Type.STRING, doc = "Absent when this message cannot be answered by token") - optional("readToken", Type.STRING) + optional("image", Type.handle("image"), doc = "An image token") + optional("replyToken", Type.handle("reply"), doc = "Absent when this message cannot be answered by token") + optional("readToken", Type.handle("read")) optional("extra", Type.map(Type.ANY), doc = "What only this provider knows, such as attachments or the message type.") } } diff --git a/contract/src/test/kotlin/com/xfl/msgbot/plugin/api/schema/TypeTest.kt b/contract/src/test/kotlin/com/xfl/msgbot/plugin/api/schema/TypeTest.kt index 520b130..bb335d8 100644 --- a/contract/src/test/kotlin/com/xfl/msgbot/plugin/api/schema/TypeTest.kt +++ b/contract/src/test/kotlin/com/xfl/msgbot/plugin/api/schema/TypeTest.kt @@ -1,5 +1,6 @@ package com.xfl.msgbot.plugin.api.schema +import com.xfl.msgbot.plugin.api.standard.StandardApi import com.xfl.msgbot.plugin.api.value.Value import com.xfl.msgbot.plugin.api.value.vArray import com.xfl.msgbot.plugin.api.value.vObject @@ -145,4 +146,22 @@ class TypeConstraintTest { val type = Type.parse("list<{a: int}>[..5]") assertEquals(vArray(vObject("a" to 1)), type.conform(vArray(vObject("a" to 1, "b" to 2)))) } + + @Test + fun `a plain string fits a handle in the standard, other types do not`() { + assertTrue(Type.STRING.fits(Type.handle("reply"))) + assertTrue(Type.STRING.nullable().fits(Type.handle("reply").nullable())) + assertTrue(Type.handle("reply").fits(Type.handle("reply"))) + assertTrue(!Type.INT.fits(Type.handle("reply"))) + assertTrue(!Type.handle("read").fits(Type.handle("reply"))) + } + + @Test + fun `the standard bot module marks its tokens as handles`() { + val message = StandardApi.Bot.event("message")!!.fields.associateBy { it.name } + assertEquals(Type.handle("reply").nullable(), message.getValue("replyToken").type) + assertEquals(Type.handle("read").nullable(), message.getValue("readToken").type) + assertEquals(Type.handle("image").nullable(), message.getValue("image").type) + assertEquals(Type.handle("reply").nullable(), StandardApi.Bot.function("reply")!!.params.first { it.name == "token" }.type) + } }