Skip to content

Commit

Permalink
优化 参数构建器:使 onlySelect(..)selectExclude(..) 方法支持传入形如 `age,nam…
Browse files Browse the repository at this point in the history
…e` 这样以 `,` 分隔的字符串参数
  • Loading branch information
troyzhxu committed Jun 22, 2022
1 parent 39bb980 commit 94f0458
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
### ✨ Better

* Bean Searcher
* 参数构建器新增 `orderBy(FieldFn<T, ?> fieldFn)``orderBy(String fieldName)``asc()``desc()` 方法
* 优化 `参数构建器`:新增 `orderBy(FieldFn<T, ?> fieldFn)``orderBy(String fieldName)``asc()``desc()` 方法
* 优化 `参数构建器`:使 `onlySelect(..)``selectExclude(..)` 方法支持传入形如 `age,name` 这样以 `,` 分隔的字符串参数
* Bean Searcher Boot Starter
* 优化:添加自定义参数过滤器时,不覆盖内置的参数过滤器

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ public MapBuilder put(String key, Object value) {
*/
@SafeVarargs
public final <T> MapBuilder onlySelect(FieldFn<T, ?>... fieldFns) {
String[] fields = new String[fieldFns.length];
for (int i = 0; i < fields.length; i++) {
fields[i] = toFieldName(fieldFns[i]);
}
return onlySelect(fields);
return onlySelect(toFields(fieldFns));
}

/**
Expand All @@ -56,8 +52,7 @@ public final <T> MapBuilder onlySelect(FieldFn<T, ?>... fieldFns) {
* @return MapBuilder
*/
public MapBuilder onlySelect(String... fields) {
Collections.addAll(obtainList(ONLY_SELECT), fields);
return this;
return appendFields(obtainList(ONLY_SELECT), fields);
}

/**
Expand All @@ -68,11 +63,7 @@ public MapBuilder onlySelect(String... fields) {
*/
@SafeVarargs
public final <T> MapBuilder selectExclude(FieldFn<T, ?>... fieldFns) {
String[] fields = new String[fieldFns.length];
for (int i = 0; i < fields.length; i++) {
fields[i] = toFieldName(fieldFns[i]);
}
return selectExclude(fields);
return selectExclude(toFields(fieldFns));
}

/**
Expand All @@ -81,7 +72,24 @@ public final <T> MapBuilder selectExclude(FieldFn<T, ?>... fieldFns) {
* @return MapBuilder
*/
public MapBuilder selectExclude(String... fields) {
Collections.addAll(obtainList(SELECT_EXCLUDE), fields);
return appendFields(obtainList(SELECT_EXCLUDE), fields);
}

@SafeVarargs
private final <T> String[] toFields(FieldFn<T, ?>... fieldFns) {
String[] fields = new String[fieldFns.length];
for (int i = 0; i < fields.length; i++) {
fields[i] = toFieldName(fieldFns[i]);
}
return fields;
}

private MapBuilder appendFields(List<String> list, String... fields) {
if (fields.length == 1) {
Collections.addAll(list, fields[0].split(","));
} else {
Collections.addAll(list, fields);
}
return this;
}

Expand Down

0 comments on commit 94f0458

Please sign in to comment.