This commit is contained in:
Looly
2024-11-06 16:50:01 +08:00
parent 2d068f93fe
commit 525866e9e7
2 changed files with 46 additions and 7 deletions

View File

@@ -38,7 +38,6 @@ import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.function.UnaryOperator;
/** /**
* Setting文件加载器 * Setting文件加载器
@@ -72,7 +71,7 @@ public class SettingLoader {
/** /**
* 值编辑器 * 值编辑器
*/ */
private UnaryOperator<String> valueEditor; private ValueEditor valueEditor;
/** /**
* 构造 * 构造
@@ -120,7 +119,7 @@ public class SettingLoader {
* @return this * @return this
* @since 6.0.0 * @since 6.0.0
*/ */
public SettingLoader setValueEditor(final UnaryOperator<String> valueEditor) { public SettingLoader setValueEditor(final ValueEditor valueEditor) {
this.valueEditor = valueEditor; this.valueEditor = valueEditor;
return this; return this;
} }
@@ -192,16 +191,17 @@ public class SettingLoader {
continue; continue;
} }
final String key = StrUtil.trim(keyValue[0]);
String value = keyValue[1]; String value = keyValue[1];
if (null != this.valueEditor) { if (null != this.valueEditor) {
value = this.valueEditor.apply(value); value = this.valueEditor.edit(group, key, value);
} }
// 替换值中的所有变量变量(变量必须是此行之前定义的变量,否则无法找到) // 替换值中的所有变量变量(变量必须是此行之前定义的变量,否则无法找到)
if (this.isUseVariable) { if (this.isUseVariable) {
value = replaceVar(groupedMap, group, value); value = replaceVar(groupedMap, group, value);
} }
groupedMap.put(group, StrUtil.trim(keyValue[0]), value); groupedMap.put(group, key, value);
} }
} finally { } finally {
IoUtil.closeQuietly(reader); IoUtil.closeQuietly(reader);
@@ -298,4 +298,22 @@ public class SettingLoader {
return value; return value;
} }
// ----------------------------------------------------------------------------------- Private method end // ----------------------------------------------------------------------------------- Private method end
/**
* 值编辑器,用于在加载配置文件时对值进行编辑,例如解密等<br>
* 此接口用于在加载配置文件时,编辑值,例如解密等,从而加载出明文的配置值
*
*/
@FunctionalInterface
public interface ValueEditor {
/**
* 编辑值
*
* @param group 分组
* @param key 键
* @param value 值
* @return 编辑后的值
*/
String edit(String group, String key, String value);
}
} }

View File

@@ -0,0 +1,21 @@
package org.dromara.hutool.setting;
import org.dromara.hutool.core.io.resource.ResourceUtil;
import org.dromara.hutool.core.util.CharsetUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class IssueIB1I8PTest {
@Test
void loadTest() {
final SettingLoader loader = new SettingLoader(CharsetUtil.UTF_8, true);
loader.setValueEditor((group, key, value)->{
if("pass".equals(key)){
return "pass" + value;
}
return value;
});
final Setting setting = new Setting(ResourceUtil.getResource("test.setting"), loader);
Assertions.assertEquals("pass123456", setting.getStrByGroup("pass", "demo"));
}
}