Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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") {
Expand All @@ -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.")
Expand All @@ -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.")
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
}
}
Loading