-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add builder for list options (#6148)
#### What type of PR is this? /kind improvement /area core /milestone 2.17.x #### What this PR does / why we need it: 为 ListOptions 增加 Builder 以方便构建 ListOptions 对象,可以通过以下形式创建 ```java var listOptions = ListOptions.builder() .labelSelector()// 构建 LabelSelector .eq("key-1", "value-1") .notEq("key-2", "value-1") .exists("key-3") .end()// 结束构建 LabelSelector .fieldQuery(equal("spec.title", "fake-title")) // 构建 FieldSelector .andQuery(equal("spec.slug", "fake-slug")) .orQuery(equal("spec.slug", "test")) .build(); ``` #### Does this PR introduce a user-facing change? ```release-note 开发者相关:支持通过 Builder 来简化 ListOptions 的构建 ```
- Loading branch information
Showing
5 changed files
with
227 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
api/src/test/java/run/halo/app/extension/ListOptionsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package run.halo.app.extension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static run.halo.app.extension.index.query.QueryFactory.equal; | ||
|
||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Test for {@link ListOptions}. | ||
* | ||
* @author guqing | ||
* @since 2.17.0 | ||
*/ | ||
class ListOptionsTest { | ||
|
||
@Nested | ||
class ListOptionsBuilderTest { | ||
|
||
@Test | ||
void buildTest() { | ||
var listOptions = ListOptions.builder() | ||
.labelSelector() | ||
.eq("key-1", "value-1") | ||
.notEq("key-2", "value-1") | ||
.exists("key-3") | ||
.end() | ||
.andQuery(equal("spec.slug", "fake-slug")) | ||
.orQuery(equal("spec.slug", "test")) | ||
.build(); | ||
System.out.println(listOptions); | ||
assertThat(listOptions.toString()).isEqualTo( | ||
"fieldSelector: (spec.slug = 'fake-slug' OR spec.slug = 'test'), labelSelector: " | ||
+ "(key-1 equal value-1, key-2 not_equal value-1, key-3 EXISTS)"); | ||
} | ||
|
||
@Test | ||
void buildTest2() { | ||
var listOptions = ListOptions.builder() | ||
.labelSelector() | ||
.notEq("key-2", "value-1") | ||
.end() | ||
.fieldQuery(equal("spec.slug", "fake-slug")) | ||
.build(); | ||
assertThat(listOptions.toString()) | ||
.isEqualTo( | ||
"fieldSelector: (spec.slug = 'fake-slug'), labelSelector: (key-2 not_equal " | ||
+ "value-1)"); | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
api/src/test/java/run/halo/app/extension/router/selector/LabelSelectorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package run.halo.app.extension.router.selector; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Tests for {@link LabelSelector}. | ||
* | ||
* @author guqing | ||
* @since 2.17.0 | ||
*/ | ||
class LabelSelectorTest { | ||
|
||
@Test | ||
void builderTest() { | ||
var labelSelector = LabelSelector.builder() | ||
.eq("a", "v1") | ||
.in("b", "v2", "v3") | ||
.build(); | ||
assertThat(labelSelector.toString()) | ||
.isEqualTo("a equal v1, b IN (v2, v3)"); | ||
} | ||
} |