Skip to content

Commit defde35

Browse files
authored
Merge pull request #896 from simple-robot/optimize-895
优化统一MergedBinder对null结果、失败结果的处理
2 parents dc5cf81 + dd5c1c5 commit defde35

File tree

4 files changed

+228
-7
lines changed

4 files changed

+228
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
* Copyright (c) 2024. ForteScarlet.
3+
*
4+
* Project https://github.com/simple-robot/simpler-robot
5+
6+
*
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Lesser General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* Lesser GNU General Public License for more details.
18+
*
19+
* You should have received a copy of the Lesser GNU General Public License
20+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
package love.forte.simbot.spring.test
25+
26+
import io.mockk.every
27+
import io.mockk.mockk
28+
import kotlinx.coroutines.flow.toList
29+
import kotlinx.coroutines.runBlocking
30+
import love.forte.simbot.application.Application
31+
import love.forte.simbot.event.*
32+
import love.forte.simbot.quantcat.common.annotations.Filter
33+
import love.forte.simbot.quantcat.common.annotations.FilterValue
34+
import love.forte.simbot.quantcat.common.annotations.Listener
35+
import love.forte.simbot.spring.EnableSimbot
36+
import org.springframework.beans.factory.annotation.Autowired
37+
import org.springframework.boot.test.context.SpringBootTest
38+
import org.springframework.stereotype.Component
39+
import kotlin.test.Test
40+
import kotlin.test.assertEquals
41+
import kotlin.test.assertFails
42+
import kotlin.test.assertNull
43+
44+
45+
/**
46+
* 测试来源:https://github.com/simple-robot/simpler-robot/issues/895
47+
*
48+
* @author ForteScarlet
49+
*/
50+
@SpringBootTest(
51+
classes = [
52+
DefaultBinderTests::class,
53+
TestListenerContainer::class,
54+
]
55+
)
56+
@EnableSimbot
57+
open class DefaultBinderTests {
58+
59+
@Test
60+
fun binderTest1(
61+
@Autowired application: Application
62+
) {
63+
fun Event.push(): List<EventResult> {
64+
return runBlocking {
65+
application.eventDispatcher.push(this@push)
66+
.throwIfError()
67+
.filterNotInvalid()
68+
.toList()
69+
}
70+
}
71+
72+
// test1: 应当得到 page=1, 因为匹配内容 page 实际不存在,使用默认值
73+
mockk<MessageEvent>(relaxed = true) {
74+
every { messageContent.plainText } returns "test_1 1"
75+
assertEquals(1, push().first().content)
76+
}
77+
78+
// test2(1): 符合匹配结果,应当得到 1
79+
mockk<MessageEvent>(relaxed = true) {
80+
every { messageContent.plainText } returns "test_2 1"
81+
assertEquals("1", push().first().content)
82+
}
83+
84+
// test2(2): 不符合匹配结果、不是required=false,理应报错
85+
mockk<MessageEvent>(relaxed = true) {
86+
every { messageContent.plainText } returns "test_2"
87+
assertFails { push() }
88+
}
89+
90+
// test3(1): 不符合匹配结果、不是required=false,但参数是可选的,使用默认值,即得到 null
91+
mockk<MessageEvent>(relaxed = true) {
92+
every { messageContent.plainText } returns "test_3"
93+
assertNull(push().first().content)
94+
}
95+
96+
// test3(2): 符合匹配结果、得到 "1"
97+
mockk<MessageEvent>(relaxed = true) {
98+
every { messageContent.plainText } returns "test_3 1"
99+
assertEquals("1", push().first().content)
100+
}
101+
102+
// test4(1): 不符合匹配结果、是required=false,参数是可选的,使用默认值,即得到 null
103+
mockk<MessageEvent>(relaxed = true) {
104+
every { messageContent.plainText } returns "test_4"
105+
assertNull(push().first().content)
106+
}
107+
108+
// test4(2): 符合匹配结果、得到 1
109+
mockk<MessageEvent>(relaxed = true) {
110+
every { messageContent.plainText } returns "test_4 1"
111+
assertEquals(1, push().first().content)
112+
}
113+
114+
// test5(1): 不符合匹配结果、不是required=false,参数是可选的,使用默认值,即得到 1
115+
mockk<MessageEvent>(relaxed = true) {
116+
every { messageContent.plainText } returns "test_5"
117+
assertEquals(1, push().first().content)
118+
}
119+
120+
// test5(2): 符合匹配结果、得到 2
121+
mockk<MessageEvent>(relaxed = true) {
122+
every { messageContent.plainText } returns "test_5 2"
123+
assertEquals(2, push().first().content)
124+
}
125+
126+
// test6(1): 不符合匹配结果、是required=false,参数是可选的,但是参数是可以为null的,因此会填充 null 而不是默认值
127+
mockk<MessageEvent>(relaxed = true) {
128+
every { messageContent.plainText } returns "test_6"
129+
assertNull(push().first().content)
130+
}
131+
132+
// test6(2): 符合匹配结果、得到 2
133+
mockk<MessageEvent>(relaxed = true) {
134+
every { messageContent.plainText } returns "test_6 2"
135+
assertEquals(2, push().first().content)
136+
}
137+
}
138+
139+
}
140+
141+
@Component
142+
class TestListenerContainer {
143+
@Listener
144+
@Filter("^test_1(\\s+(?<page>\\d+))?")
145+
fun MessageEvent.handle1(
146+
@FilterValue("page", false) page: Int = 1
147+
): Int = page
148+
149+
@Listener
150+
@Filter("^test_2(\\s+(?<page>\\d+))?")
151+
fun MessageEvent.handle2(
152+
@FilterValue("page") page: String?
153+
): String? = page
154+
155+
@Listener
156+
@Filter("^test_3(\\s+(?<page>\\d+))?")
157+
fun MessageEvent.handle3(
158+
@FilterValue("page") page: String? = null
159+
): String? = page
160+
161+
@Listener
162+
@Filter("^test_4(\\s+(?<page>\\d+))?")
163+
fun MessageEvent.handle4(
164+
@FilterValue("page", false) page: Int? = null
165+
): Int? = page
166+
167+
@Listener
168+
@Filter("^test_5(\\s+(?<page>\\d+))?")
169+
fun MessageEvent.handle5(
170+
@FilterValue("page") page: Int = 1
171+
): Int = page
172+
173+
@Listener
174+
@Filter("^test_6(\\s+(?<page>\\d+))?")
175+
fun MessageEvent.handle6(
176+
@FilterValue("page", false) page: Int? = 1
177+
): Int? = page
178+
}

simbot-quantcat/simbot-quantcat-common/src/commonMain/kotlin/love/forte/simbot/quantcat/common/annotations/FilterValue.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -26,14 +26,17 @@ package love.forte.simbot.quantcat.common.annotations
2626
import love.forte.simbot.quantcat.common.filter.FilterValueProperties
2727

2828
/**
29-
* 指定一个参数,此参数为通过 [love.forte.simbot.quantcat.annotations.Filter]
29+
* 指定一个参数,此参数为通过 [love.forte.simbot.quantcat.common.annotations.Filter]
3030
* 解析而得到的动态参数提取器中的内容。
3131
*
3232
* 参数提取格式基于正则匹配模式,参考 [Filter.value] 中的相关说明。
3333
*
3434
* @param value 所需动态参数的key。
3535
* @param required 对于参数绑定器来讲其是否为必须的。
36-
* 如果不是必须的,则在无法获取参数后传递null作为结果,否则将会抛出异常并交由后续绑定器处理。
36+
* 如果不是必须的,则在无法获取参数后传递 `null` 作为结果
37+
* (此结果被视为正确结果。换言之如果参数为 `nullable`,
38+
* 但是存在一个不是 `null` 的默认值,则最终的参数值依然为 `null`),
39+
* 否则将会抛出异常并交由后续绑定器处理。
3740
*/
3841
@Target(AnnotationTarget.VALUE_PARAMETER)
3942
public annotation class FilterValue(val value: String, val required: Boolean = true)

simbot-quantcat/simbot-quantcat-common/src/commonMain/kotlin/love/forte/simbot/quantcat/common/binder/ParameterBinder.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* Project https://github.com/simple-robot/simpler-robot
55
66
*
7-
* This file is part of the Simple Robot Library.
7+
* This file is part of the Simple Robot Library (Alias: simple-robot, simbot, etc.).
88
*
99
* This program is free software: you can redistribute it and/or modify
1010
* it under the terms of the GNU Lesser General Public License as published by
@@ -30,6 +30,10 @@ import love.forte.simbot.event.EventListenerContext
3030
*
3131
* 对于一个可执行函数的参数 `KParameter` 所需的结果获取器。
3232
*
33+
* 没有任何绑定器时,
34+
* 通常会使用 [EmptyBinder][love.forte.simbot.quantcat.common.binder.impl.EmptyBinder],
35+
* 当存在多个绑定器时,通常会使用 [MergedBinder][love.forte.simbot.quantcat.common.binder.impl.MergedBinder]。
36+
*
3337
*/
3438
public interface ParameterBinder {
3539
/**

simbot-quantcat/simbot-quantcat-common/src/jvmMain/kotlin/love/forte/simbot/quantcat/common/binder/impl/DefaultBinders.kt

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ import kotlin.reflect.KParameter
3232

3333

3434
/**
35-
* 将会直接抛出错误的binder。
35+
* 默认的空内容绑定器。
36+
*
37+
* - 当参数可选时,使用 [ParameterBinder.Ignore] 标记其直接使用默认值。
38+
* - 当参数标记为 nullable 时,直接提供 `null`。
39+
* - 否则,[arg] 将会返回一个 [BindException] 的 [Result.failure] 表示失败。
3640
*/
3741
public class EmptyBinder(
3842
private val parameter: KParameter,
@@ -63,6 +67,11 @@ public class EmptyBinder(
6367

6468
/**
6569
* 组合多个binder的 [ParameterBinder].
70+
*
71+
* 在聚合绑定器中,会依次对所有的 [binders] 和 [spare] 使用 [ParameterBinder.arg] 来评估本次应绑定的参数,
72+
* 知道遇到第一个返回为 [Result.isSuccess] 的结果后终止评估并使用此结果。
73+
*
74+
* 评估过程的详细描述参考 [arg] 文档说明。
6675
*/
6776
public class MergedBinder(
6877
private val binders: List<ParameterBinder>, // not be empty
@@ -81,7 +90,24 @@ public class MergedBinder(
8190
require(binders.isNotEmpty()) { "'binders' must not be empty" }
8291
}
8392

84-
93+
/**
94+
*
95+
* 使用内部所有的聚合 binder 对 [context] 进行评估并选出一个最先出现的可用值。
96+
*
97+
* 评估过程中:
98+
*
99+
* - 如果参数不可为 `null`、评估结果为成功但是内容为 `null`、同时参数是可选的,
100+
* 则会忽略此结果,视为无结果。
101+
* - 如果评估结果为失败,则暂记此异常,并视为无结果。
102+
*
103+
* 期间,遇到任何成功的、不符合上述会造成“无结果”条件的,
104+
* 直接返回此评估结果,不再继续评估。
105+
*
106+
* 当所有binder评估完成,但没有遇到任何结果:
107+
*
108+
* - 如果参数为可选,输出debug日志并使用 [ParameterBinder.Ignore] 标记直接使用默认值。
109+
* - 否则,返回 [Result.failure] 错误结果,并追加之前暂记的所有异常堆栈。
110+
*/
85111
@Suppress("ReturnCount")
86112
override fun arg(context: EventListenerContext): Result<Any?> {
87113
var err: Throwable? = null
@@ -91,7 +117,17 @@ public class MergedBinder(
91117
val result = arg(context)
92118
if (result.isSuccess) {
93119
// if success, return.
94-
return result
120+
// 如果参数不可为 null、结果成功但是为 null、同时参数是可选的,
121+
// 则返回 `null` 以忽略此参数。
122+
return if (
123+
result.getOrNull() == null &&
124+
!parameter.type.isMarkedNullable &&
125+
parameter.isOptional
126+
) {
127+
null
128+
} else {
129+
result
130+
}
95131
}
96132
// failure
97133
val resultErr = result.exceptionOrNull()!!

0 commit comments

Comments
 (0)