瀏覽代碼

Add inline `BotFactory.newBot` for Kotlin DSL. fix #1233 (#1234)

Him188 4 年之前
父節點
當前提交
57eb716e31

+ 2 - 0
binary-compatibility-validator/android/api/binary-compatibility-validator-android.api

@@ -64,7 +64,9 @@ public abstract interface class net/mamoe/mirai/BotFactory$BotConfigurationLambd
 }
 
 public final class net/mamoe/mirai/BotFactory$INSTANCE : net/mamoe/mirai/BotFactory {
+	public final synthetic fun newBot (JLjava/lang/String;Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot;
 	public fun newBot (JLjava/lang/String;Lnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot;
+	public final synthetic fun newBot (J[BLkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot;
 	public fun newBot (J[BLnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot;
 }
 

+ 2 - 0
binary-compatibility-validator/api/binary-compatibility-validator.api

@@ -64,7 +64,9 @@ public abstract interface class net/mamoe/mirai/BotFactory$BotConfigurationLambd
 }
 
 public final class net/mamoe/mirai/BotFactory$INSTANCE : net/mamoe/mirai/BotFactory {
+	public final synthetic fun newBot (JLjava/lang/String;Lkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot;
 	public fun newBot (JLjava/lang/String;Lnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot;
+	public final synthetic fun newBot (J[BLkotlin/jvm/functions/Function1;)Lnet/mamoe/mirai/Bot;
 	public fun newBot (J[BLnet/mamoe/mirai/utils/BotConfiguration;)Lnet/mamoe/mirai/Bot;
 }
 

+ 41 - 0
mirai-core-api/src/commonMain/kotlin/BotFactory.kt

@@ -118,5 +118,46 @@ public interface BotFactory {
         override fun newBot(qq: Long, passwordMd5: ByteArray, configuration: BotConfiguration): Bot {
             return Mirai.BotFactory.newBot(qq, passwordMd5, configuration)
         }
+
+        /**
+         * 使用指定的 [配置][configuration] 构造 [Bot] 实例
+         *
+         * ```
+         * newBot(123, "") {
+         *     // this: BotConfiguration
+         *     fileBasedDeviceInfo()
+         * }
+         * ```
+         *
+         * @since 2.7
+         */
+        @JvmSynthetic
+        public inline fun newBot(
+            qq: Long,
+            password: String,
+            configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
+        ): Bot = newBot(qq, password, configuration.run { BotConfiguration().apply(configuration) })
+
+        // implementation notes: this is inline for `inheritCoroutineContext()`
+        // see https://github.com/mamoe/mirai/commit/0dbb448cad1ed4773d48ccb8c0b497841bc9fa4c#r50249446
+
+        /**
+         * 使用指定的 [配置][configuration] 构造 [Bot] 实例
+         *
+         * ```
+         * newBot(123, password) {
+         *     // this: BotConfiguration
+         *     fileBasedDeviceInfo()
+         * }
+         * ```
+         *
+         * @since 2.7
+         */
+        @JvmSynthetic
+        public inline fun newBot(
+            qq: Long,
+            passwordMd5: ByteArray,
+            configuration: BotConfiguration.() -> Unit /* = BotConfiguration.() -> Unit */
+        ): Bot = newBot(qq, passwordMd5, configuration.run { BotConfiguration().apply(configuration) })
     }
 }

+ 26 - 0
mirai-core/src/jvmTest/kotlin/BotFactoryTest.kt

@@ -0,0 +1,26 @@
+/*
+ * Copyright 2019-2021 Mamoe Technologies and contributors.
+ *
+ *  此源代码的使用受 GNU AFFERO GENERAL PUBLIC LICENSE version 3 许可证的约束, 可以在以下链接找到该许可证.
+ *  Use of this source code is governed by the GNU AGPLv3 license that can be found through the following link.
+ *
+ *  https://github.com/mamoe/mirai/blob/master/LICENSE
+ */
+
+package net.mamoe.mirai.internal
+
+import kotlinx.coroutines.runBlocking
+import net.mamoe.mirai.BotFactory
+import kotlin.test.Test
+
+internal class BotFactoryTest {
+
+    @Test
+    fun `inlined newBot in Kotlin`() {
+        runBlocking {
+            BotFactory.newBot(123, "") {
+                inheritCoroutineContext()
+            }.close()
+        }
+    }
+}