From a8db2e5e4bb16f94ff4f162859fcb166595f9f12 Mon Sep 17 00:00:00 2001 From: guqing <38999863+guqing@users.noreply.github.com> Date: Thu, 14 Jul 2022 16:19:09 +0800 Subject: [PATCH] refactor: the value structure of ConfigMap for Setting custom extension (#2243) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit #### What type of PR is this? /kind improvement /area core /milestone 2.0 #### What this PR does / why we need it: 由于 Setting 自定义模型关联表单值的存储结构改变,对应修改 SettingFetcher 的取值方式 https://github.com/halo-dev/rfcs/pull/18 #### Which issue(s) this PR fixes: Fixes # #### Special notes for your reviewer: /cc @halo-dev/sig-halo #### Does this PR introduce a user-facing change? ```release-note None ``` --- .../core/extension/endpoint/UserEndpoint.java | 2 +- .../run/halo/app/plugin/SettingFetcher.java | 34 +++++++++++++------ .../halo/app/plugin/SettingFetcherTest.java | 15 ++++---- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java b/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java index be60871c8..5ae6895c9 100644 --- a/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java +++ b/src/main/java/run/halo/app/core/extension/endpoint/UserEndpoint.java @@ -135,7 +135,7 @@ public class UserEndpoint implements CustomEndpoint { .parameter(parameterBuilder().in(ParameterIn.PATH).name("name") .description("User name") .required(true)) - .response(responseBuilder().implementation(Set.class))) + .response(responseBuilder().implementation(UserPermission.class))) .build(); } diff --git a/src/main/java/run/halo/app/plugin/SettingFetcher.java b/src/main/java/run/halo/app/plugin/SettingFetcher.java index 219db6b79..fbfbf663c 100644 --- a/src/main/java/run/halo/app/plugin/SettingFetcher.java +++ b/src/main/java/run/halo/app/plugin/SettingFetcher.java @@ -3,8 +3,11 @@ package run.halo.app.plugin; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.JsonNodeFactory; +import java.util.Collection; +import java.util.Map; import java.util.Optional; import java.util.concurrent.atomic.AtomicReference; +import java.util.stream.Collectors; import org.apache.commons.lang3.StringUtils; import org.springframework.lang.NonNull; import org.springframework.lang.Nullable; @@ -25,9 +28,7 @@ import run.halo.app.infra.utils.JsonUtils; */ public class SettingFetcher { - private static final String PLUGIN_SETTING_VALUE = "setting"; - - private final AtomicReference valueRef = new AtomicReference<>(null); + private final AtomicReference> valueRef = new AtomicReference<>(null); private final ExtensionClient extensionClient; @@ -49,9 +50,16 @@ public class SettingFetcher { return getInternal(group); } + /** + * Get values from {@link ConfigMap}. + * + * @return a unmodifiable map of values(non-null). + */ @NonNull - public JsonNode getValues() { - return valueRef.updateAndGet(m -> m != null ? m : getValuesInternal()); + public Map getValues() { + Map values = + valueRef.updateAndGet(m -> m != null ? m : getValuesInternal()); + return Map.copyOf(values); } private JsonNode getInternal(String group) { @@ -59,13 +67,14 @@ public class SettingFetcher { .orElse(JsonNodeFactory.instance.missingNode()); } - private JsonNode getValuesInternal() { + private Map getValuesInternal() { return configMap(pluginName) - .filter(configMap -> configMap.getData() != null - && configMap.getData().containsKey(PLUGIN_SETTING_VALUE)) - .map(configMap -> configMap.getData().get(PLUGIN_SETTING_VALUE)) - .map(this::readTree) - .orElse(JsonNodeFactory.instance.missingNode()); + .filter(configMap -> configMap.getData() != null) + .map(ConfigMap::getData) + .map(Map::entrySet) + .stream() + .flatMap(Collection::stream) + .collect(Collectors.toMap(Map.Entry::getKey, entry -> readTree(entry.getValue()))); } private Optional configMap(String pluginName) { @@ -80,6 +89,9 @@ public class SettingFetcher { } private JsonNode readTree(String json) { + if (StringUtils.isBlank(json)) { + return JsonNodeFactory.instance.missingNode(); + } try { return JsonUtils.DEFAULT_JSON_MAPPER.readTree(json); } catch (JsonProcessingException e) { diff --git a/src/test/java/run/halo/app/plugin/SettingFetcherTest.java b/src/test/java/run/halo/app/plugin/SettingFetcherTest.java index 3b904e623..f6287f1a8 100644 --- a/src/test/java/run/halo/app/plugin/SettingFetcherTest.java +++ b/src/test/java/run/halo/app/plugin/SettingFetcherTest.java @@ -54,7 +54,7 @@ class SettingFetcherTest { @Test void getValues() throws JSONException { - JsonNode values = settingFetcher.getValues(); + Map values = settingFetcher.getValues(); verify(extensionClient, times(1)).fetch(eq(ConfigMap.class), any()); @@ -62,7 +62,7 @@ class SettingFetcherTest { JSONAssert.assertEquals(getSns(), JsonUtils.objectToJson(values.get("sns")), true); // The extensionClient will only be called once - JsonNode callAgain = settingFetcher.getValues(); + Map callAgain = settingFetcher.getValues(); assertThat(callAgain).isNotNull(); verify(extensionClient, times(1)).fetch(eq(ConfigMap.class), any()); } @@ -98,15 +98,14 @@ class SettingFetcherTest { configMap.setMetadata(metadata); configMap.setKind("ConfigMap"); configMap.setApiVersion("v1alpha1"); - configMap.setData(Map.of("setting", String.format(""" - { - "sns": %s, - "basic": { + configMap.setData(Map.of("sns", getSns(), + "basic", """ + { "color": "red", "width": "100" } - } - """, getSns()))); + """) + ); return configMap; }