-
-
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.
feat: add reset config API for theme and plugin (#2964)
#### What type of PR is this? /kind feature /kind api-change /area core #### What this PR does / why we need it: 为主题和插件提供重置设置项 API 此 PR 会重新读取配置对应的 Setting 资源,从其中读取默认值后更新到现有的 ConfigMap 中替换其 data see #2789 for more details #### Which issue(s) this PR fixes: Fixes #2789 #### Special notes for your reviewer: how to test it? 1. 在主题设置或插件设置配置一些设置项后保存 2. 执行重置配置 3. 配置恢复为了 Setting 中指定的默认值 /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note 为主题和插件提供重置设置项 API ```
- Loading branch information
Showing
12 changed files
with
292 additions
and
47 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
48 changes: 48 additions & 0 deletions
48
src/main/java/run/halo/app/core/extension/theme/SettingUtils.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,48 @@ | ||
package run.halo.app.core.extension.theme; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import lombok.experimental.UtilityClass; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.util.CollectionUtils; | ||
import run.halo.app.core.extension.Setting; | ||
import run.halo.app.infra.utils.JsonUtils; | ||
|
||
@UtilityClass | ||
public class SettingUtils { | ||
private static final String VALUE_FIELD = "value"; | ||
private static final String NAME_FIELD = "name"; | ||
|
||
/** | ||
* Read setting default value from {@link Setting} forms. | ||
* | ||
* @param setting {@link Setting} extension | ||
* @return a map of setting default value | ||
*/ | ||
@NonNull | ||
public static Map<String, String> settingDefinedDefaultValueMap(Setting setting) { | ||
List<Setting.SettingForm> forms = setting.getSpec().getForms(); | ||
if (CollectionUtils.isEmpty(forms)) { | ||
return Map.of(); | ||
} | ||
Map<String, String> data = new LinkedHashMap<>(); | ||
for (Setting.SettingForm form : forms) { | ||
String group = form.getGroup(); | ||
Map<String, JsonNode> groupValue = form.getFormSchema().stream() | ||
.map(o -> JsonUtils.DEFAULT_JSON_MAPPER.convertValue(o, JsonNode.class)) | ||
.filter(jsonNode -> jsonNode.isObject() && jsonNode.has(NAME_FIELD) | ||
&& jsonNode.has(VALUE_FIELD)) | ||
.map(jsonNode -> { | ||
String name = jsonNode.get(NAME_FIELD).asText(); | ||
JsonNode value = jsonNode.get(VALUE_FIELD); | ||
return Map.entry(name, value); | ||
}) | ||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); | ||
data.put(group, JsonUtils.objectToJson(groupValue)); | ||
} | ||
return data; | ||
} | ||
} |
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
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
Oops, something went wrong.