Skip to content

Commit b8ac984

Browse files
authored
[fit] Optimize format mode (#305)
1 parent ad6c259 commit b8ac984

33 files changed

+1100
-241
lines changed

framework/fel/java/fel-core/src/main/java/modelengine/fel/core/template/support/DefaultStringTemplate.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fel.core.template.support;
88

99
import modelengine.fel.core.template.StringTemplate;
1010
import modelengine.fitframework.inspection.Validation;
1111
import modelengine.fitframework.merge.ConflictResolutionPolicy;
12+
import modelengine.fitframework.parameterization.ParameterizationMode;
1213
import modelengine.fitframework.parameterization.ParameterizedString;
1314
import modelengine.fitframework.parameterization.ParameterizedStringResolver;
1415
import modelengine.fitframework.parameterization.ResolvedParameter;
@@ -28,7 +29,7 @@
2829
*/
2930
public class DefaultStringTemplate implements StringTemplate {
3031
private static final ParameterizedStringResolver FORMATTER =
31-
ParameterizedStringResolver.create("{{", "}}", '\\', false);
32+
ParameterizedStringResolver.create("{{", "}}", '\\', ParameterizationMode.LENIENT_STRICT_PARAMETERS);
3233

3334
private final ParameterizedString parameterizedString;
3435

framework/fel/java/fel-core/src/test/java/modelengine/fel/core/template/BulkStringTemplateTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fel.core.template;
88

99
import static org.assertj.core.api.Assertions.assertThat;
10-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
10+
import static org.assertj.core.api.Assertions.catchThrowableOfType;
1111

1212
import modelengine.fel.core.template.support.DefaultBulkStringTemplate;
13+
import modelengine.fitframework.parameterization.StringFormatException;
1314
import modelengine.fitframework.util.MapBuilder;
1415

1516
import org.junit.jupiter.api.DisplayName;
@@ -80,7 +81,8 @@ void giveMissValueThenThrowException() {
8081
List<Map<String, String>> values = new ArrayList<>();
8182
values.add(MapBuilder.<String, String>get().put("adjective", "funny").build());
8283
values.add(MapBuilder.<String, String>get().put("content", "rabbits").build());
83-
assertThatThrownBy(() -> new DefaultBulkStringTemplate(template, "\n").render(values)).isInstanceOf(
84-
IllegalArgumentException.class);
84+
StringFormatException cause = catchThrowableOfType(StringFormatException.class,
85+
() -> new DefaultBulkStringTemplate(template, "\n").render(values));
86+
assertThat(cause).hasMessage("Required parameters are missing.");
8587
}
8688
}

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/exception/ClassAccessException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.exception;
88

99
/**
1010
* 当访问类型发生异常时引发的异常。
1111
*
1212
* @author 梁济时
13-
* @since 1.0
13+
* @since 2020-07-24
1414
*/
1515
public class ClassAccessException extends RuntimeException {
1616
/**

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/exception/DateFormatException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.exception;
88

99
/**
1010
* 当发现日期格式错误时引发的异常。
1111
*
1212
* @author 梁济时
13-
* @since 1.0
13+
* @since 2020-07-24
1414
*/
1515
public class DateFormatException extends IllegalArgumentException {
1616
/**

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/exception/FieldVisitException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.exception;
88

99
/**
1010
* 当访问字段失败时引发的异常。
1111
*
1212
* @author 梁济时
13-
* @since 1.0
13+
* @since 2020-07-24
1414
*/
1515
public class FieldVisitException extends RuntimeException {
1616
/**

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/exception/MethodInvocationException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.exception;
88

99
/**
1010
* 当调用方法失败时引发的异常。
1111
*
1212
* @author 梁济时
13-
* @since 1.0
13+
* @since 2020-07-24
1414
*/
1515
public class MethodInvocationException extends RuntimeException {
1616
/**

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/exception/MethodNotFoundException.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.exception;
88

99
/**
1010
* 当没有指定的方法时引发的异常。
1111
*
1212
* @author 梁济时
13-
* @since 1.0
13+
* @since 2020-07-24
1414
*/
1515
public class MethodNotFoundException extends RuntimeException {
1616
/**
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) 2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*--------------------------------------------------------------------------------------------*/
6+
7+
package modelengine.fitframework.parameterization;
8+
9+
/**
10+
* 参数化字符串的格式化模式。
11+
*
12+
* @author 季聿阶
13+
* @since 2025-09-23
14+
*/
15+
public enum ParameterizationMode {
16+
/**
17+
* 严格模式:
18+
* <ul>
19+
* <li>参数数量必须完全匹配</li>
20+
* <li>语法错误抛异常</li>
21+
* <li>缺失参数抛异常</li>
22+
* </ul>
23+
*/
24+
STRICT(true, true, MissingParameterBehavior.THROW_EXCEPTION),
25+
26+
/**
27+
* 宽松模式 - 使用空字符串:
28+
* <ul>
29+
* <li>允许多余参数</li>
30+
* <li>语法错误当普通字符处理</li>
31+
* <li>缺失参数使用空字符串</li>
32+
* </ul>
33+
*/
34+
LENIENT_EMPTY(false, false, MissingParameterBehavior.USE_EMPTY_STRING),
35+
36+
/**
37+
* 宽松模式 - 使用默认值:
38+
* <ul>
39+
* <li>允许多余参数</li>
40+
* <li>语法错误当普通字符处理</li>
41+
* <li>缺失参数使用默认值</li>
42+
* </ul>
43+
*/
44+
LENIENT_DEFAULT(false, false, MissingParameterBehavior.USE_DEFAULT_VALUE),
45+
46+
/**
47+
* 宽松模式 - 保持占位符:
48+
* <ul>
49+
* <li>允许多余参数</li>
50+
* <li>语法错误当普通字符处理</li>
51+
* <li>缺失参数保持原占位符</li>
52+
* </ul>
53+
*/
54+
LENIENT_KEEP_PLACEHOLDER(false, false, MissingParameterBehavior.KEEP_PLACEHOLDER),
55+
56+
/**
57+
* 混合模式:
58+
* <ul>
59+
* <li>允许多余参数</li>
60+
* <li>语法错误当普通字符处理</li>
61+
* <li>但缺失参数仍抛异常(便于调试)</li>
62+
* </ul>
63+
*/
64+
LENIENT_STRICT_PARAMETERS(false, false, MissingParameterBehavior.THROW_EXCEPTION);
65+
66+
private final boolean requireExactParameterCount;
67+
private final boolean strictSyntax;
68+
private final MissingParameterBehavior missingParameterBehavior;
69+
70+
ParameterizationMode(boolean requireExactParameterCount, boolean strictSyntax,
71+
MissingParameterBehavior missingParameterBehavior) {
72+
this.requireExactParameterCount = requireExactParameterCount;
73+
this.strictSyntax = strictSyntax;
74+
this.missingParameterBehavior = missingParameterBehavior;
75+
}
76+
77+
/**
78+
* 是否要求参数数量必须完全匹配。
79+
*
80+
* @return 如果要求参数数量必须完全匹配,则返回 {@code true},否则,返回 {@code false}。
81+
*/
82+
public boolean isRequireExactParameterCount() {
83+
return this.requireExactParameterCount;
84+
}
85+
86+
/**
87+
* 是否要求语法错误抛异常。
88+
*
89+
* @return 如果要求语法错误抛异常,则返回 {@code true},否则,返回 {@code false}。
90+
*/
91+
public boolean isStrictSyntax() {
92+
return this.strictSyntax;
93+
}
94+
95+
/**
96+
* 获取缺失参数处理行为。
97+
*
98+
* @return 表示缺失参数处理行为的 {@link MissingParameterBehavior}。
99+
*/
100+
public MissingParameterBehavior getMissingParameterBehavior() {
101+
return this.missingParameterBehavior;
102+
}
103+
104+
/**
105+
* 缺失参数处理行为。
106+
*/
107+
public enum MissingParameterBehavior {
108+
/**
109+
* 抛异常。
110+
*/
111+
THROW_EXCEPTION,
112+
113+
/**
114+
* 使用空字符串。
115+
*/
116+
USE_EMPTY_STRING,
117+
118+
/**
119+
* 使用默认值。
120+
*/
121+
USE_DEFAULT_VALUE,
122+
123+
/**
124+
* 保持原占位符。
125+
*/
126+
KEEP_PLACEHOLDER
127+
}
128+
}

framework/fit/java/fit-util/src/main/java/modelengine/fitframework/parameterization/ParameterizedString.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
/*---------------------------------------------------------------------------------------------
2-
* Copyright (c) 2024 Huawei Technologies Co., Ltd. All rights reserved.
3-
* This file is a part of the ModelEngine Project.
4-
* Licensed under the MIT License. See License.txt in the project root for license information.
5-
*--------------------------------------------------------------------------------------------*/
1+
/*
2+
* Copyright (c) 2024-2025 Huawei Technologies Co., Ltd. All rights reserved.
3+
* This file is a part of the ModelEngine Project.
4+
* Licensed under the MIT License. See License.txt in the project root for license information.
5+
*/
66

77
package modelengine.fitframework.parameterization;
88

@@ -13,7 +13,7 @@
1313
* 为参数解析提供结果。
1414
*
1515
* @author 梁济时
16-
* @since 1.0
16+
* @since 2020-07-24
1717
*/
1818
public interface ParameterizedString {
1919
/**
@@ -45,4 +45,14 @@ public interface ParameterizedString {
4545
* @throws StringFormatException 当需要但是未提供指定名称的参数时。
4646
*/
4747
String format(Map<?, ?> args);
48+
49+
/**
50+
* 使用指定的参数映射格式化字符串。
51+
*
52+
* @param args 表示参数映射的 {@link Map}{@code <?, ?>}。
53+
* @param defaultValue 表示缺少对应参数时的默认值的 {@link String}。
54+
* @return 表示格式化后的字符串的 {@link String}。
55+
* @throws StringFormatException 当需要但是未提供指定名称的参数时。
56+
*/
57+
String format(Map<?, ?> args, String defaultValue);
4858
}

0 commit comments

Comments
 (0)