From 55c4be247967edd471aa5a1d4c784e6364a21483 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Wed, 28 Feb 2024 14:30:09 +0800 Subject: [PATCH] =?UTF-8?q?fix(cli):=20=E4=BF=AE=E5=A4=8D=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E8=BD=AC=E4=B9=89=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E4=B8=AD=E7=9A=84$=E7=AC=A6=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cli/runner/ServiceInfoWrapper.java | 4 ++-- .../runner/SpringPropertiesEscapeHelper.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/ServiceInfoWrapper.java b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/ServiceInfoWrapper.java index 49f07ee..26b922e 100644 --- a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/ServiceInfoWrapper.java +++ b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/ServiceInfoWrapper.java @@ -21,8 +21,8 @@ public class ServiceInfoWrapper { public ServiceInfoWrapper(String name, ServiceInfo serviceInfo) { this.name = name; this.serviceInfo = serviceInfo; - this.environments = SpringPropertiesEscapeHelper.escapeMapKey(serviceInfo.getEnvironments()); - this.arguments = SpringPropertiesEscapeHelper.escapeMapKey(serviceInfo.getArguments()); + this.environments = SpringPropertiesEscapeHelper.escapeMap(serviceInfo.getEnvironments()); + this.arguments = SpringPropertiesEscapeHelper.escapeMap(serviceInfo.getArguments()); } public Boolean getEnabled() { diff --git a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/SpringPropertiesEscapeHelper.java b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/SpringPropertiesEscapeHelper.java index 0a25cda..722b255 100644 --- a/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/SpringPropertiesEscapeHelper.java +++ b/service-cli/service-cli-runner/src/main/java/com/lanyuanxiaoyao/service/cli/runner/SpringPropertiesEscapeHelper.java @@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.service.cli.runner; import java.util.HashMap; import java.util.Map; +import java.util.stream.Collectors; /** * spring boot 配置文件转义 @@ -10,6 +11,9 @@ import java.util.Map; * @date 2024-02-25 */ public class SpringPropertiesEscapeHelper { + public static Map escapeMap(Map map) { + return escapeMapValue(escapeMapKey(map)); + } /** * 使用 _ 代替 map key 的 . */ @@ -20,4 +24,19 @@ public class SpringPropertiesEscapeHelper { } return result; } + + public static Map escapeMapValue(Map map) { + // https://github.com/spring-projects/spring-framework/issues/9628 + // spring boot 的配置文件没有办法转义 $,使用 $\{ -> ${ 来绕过一下 + return map.entrySet() + .stream() + .peek(entry -> { + if (entry.getValue() instanceof String) { + String value = (String) entry.getValue(); + //noinspection unchecked + entry.setValue((T) value.replace("$\\{", "${")); + } + }) + .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); + } }