fix: ineffective single-character queries in labelSelector (#5007)

**What this PR does / why we need it:**
This PR resolves the reported bug in issue #5001,

**Which issue(s) this PR fixes:**

Fixes [#5001](https://github.com/halo-dev/halo/issues/5001)

```release-note
修复 labelSelector 单字符值查询无效的问题
```
pull/5032/head
TL 2023-12-07 16:52:07 +08:00 committed by GitHub
parent 1f5bef71ac
commit 8b405faa57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 2 deletions

View File

@ -12,7 +12,7 @@ public enum Operator implements Converter<String, SelectorCriteria> {
public SelectorCriteria convert(@Nullable String selector) {
if (preFlightCheck(selector, 3)) {
var i = selector.indexOf(getOperator());
if (i > 0 && (i + getOperator().length()) < selector.length() - 1) {
if (i > 0 && (i + getOperator().length()) <= selector.length() - 1) {
String key = selector.substring(0, i);
String value = selector.substring(i + getOperator().length());
return new SelectorCriteria(key, this, Set.of(value));

View File

@ -27,6 +27,8 @@ class OperatorTest {
new TestCase("name=", Equals, null),
new TestCase("name=value", Equals,
new SelectorCriteria("name", Equals, Set.of("value"))),
new TestCase("name=v", Equals,
new SelectorCriteria("name", Equals, Set.of("v"))),
new TestCase("", NotEquals, null),
new TestCase("=", NotEquals, null),
@ -59,4 +61,4 @@ class OperatorTest {
assertEquals(testCase.expected(), testCase.converter().convert(testCase.source()));
});
}
}
}