forked from kimhyungsik/ax_hub_mcp_tool
feat(common, other): support List parsing with target in GlowTrgmField
This commit is contained in:
@@ -22,4 +22,8 @@ public @interface GlowTrgmField {
|
||||
*/
|
||||
String description() default "";
|
||||
|
||||
/**
|
||||
* List 매핑 시 대상 클래스 (제네릭 타입 소거 우회용)
|
||||
*/
|
||||
Class<?> target() default void.class;
|
||||
}
|
||||
|
||||
@@ -57,10 +57,10 @@ public class GlowTrgmParser {
|
||||
}
|
||||
|
||||
int endIndex = Math.min(currentIndex + length, trgmString.length());
|
||||
String value = trgmString.substring(currentIndex, endIndex).trim();
|
||||
String value = trgmString.substring(currentIndex, endIndex);
|
||||
|
||||
field.setAccessible(true);
|
||||
setFieldValue(instance, field, value);
|
||||
setFieldValue(instance, field, value, annotation);
|
||||
|
||||
currentIndex += length;
|
||||
}
|
||||
@@ -72,21 +72,47 @@ public class GlowTrgmParser {
|
||||
}
|
||||
}
|
||||
|
||||
private static void setFieldValue(Object instance, Field field, String value) throws IllegalAccessException {
|
||||
private static void setFieldValue(Object instance, Field field, String value, GlowTrgmField annotation) throws IllegalAccessException {
|
||||
Class<?> fieldType = field.getType();
|
||||
String trimmedValue = value.trim();
|
||||
|
||||
if (fieldType == String.class) {
|
||||
field.set(instance, value);
|
||||
field.set(instance, trimmedValue);
|
||||
} else if (fieldType == int.class || fieldType == Integer.class) {
|
||||
field.set(instance, value.isEmpty() ? 0 : Integer.parseInt(value));
|
||||
field.set(instance, trimmedValue.isEmpty() ? 0 : Integer.parseInt(trimmedValue));
|
||||
} else if (fieldType == long.class || fieldType == Long.class) {
|
||||
field.set(instance, value.isEmpty() ? 0L : Long.parseLong(value));
|
||||
field.set(instance, trimmedValue.isEmpty() ? 0L : Long.parseLong(trimmedValue));
|
||||
} else if (fieldType == boolean.class || fieldType == Boolean.class) {
|
||||
field.set(instance, Boolean.parseBoolean(value));
|
||||
field.set(instance, Boolean.parseBoolean(trimmedValue));
|
||||
} else if (fieldType == double.class || fieldType == Double.class) {
|
||||
field.set(instance, value.isEmpty() ? 0.0 : Double.parseDouble(value));
|
||||
field.set(instance, trimmedValue.isEmpty() ? 0.0 : Double.parseDouble(trimmedValue));
|
||||
} else if (List.class.isAssignableFrom(fieldType)) {
|
||||
Class<?> targetClass = annotation.target();
|
||||
if (targetClass != void.class) {
|
||||
List<Object> list = new ArrayList<>();
|
||||
int itemLength = calculateTotalLength(targetClass);
|
||||
if (itemLength > 0) {
|
||||
for (int i = 0; i < value.length(); i += itemLength) {
|
||||
int end = Math.min(i + itemLength, value.length());
|
||||
String itemStr = value.substring(i, end);
|
||||
if (itemStr.trim().isEmpty()) continue;
|
||||
list.add(parse(itemStr, targetClass));
|
||||
}
|
||||
}
|
||||
field.set(instance, list);
|
||||
}
|
||||
} else {
|
||||
field.set(instance, value);
|
||||
field.set(instance, trimmedValue);
|
||||
}
|
||||
}
|
||||
|
||||
private static int calculateTotalLength(Class<?> clazz) {
|
||||
int total = 0;
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(GlowTrgmField.class)) {
|
||||
total += field.getAnnotation(GlowTrgmField.class).length();
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,6 +33,6 @@ public class SampleStringRes {
|
||||
@GlowTrgmField(order = 4, length = 2, description = "상태코드")
|
||||
private String statusCode;
|
||||
|
||||
@GlowTrgmField(order = 5, length = 10, description = "타겟")
|
||||
private String target;
|
||||
@GlowTrgmField(order = 5, length = 30, description = "타겟 리스트", target = SampleTargetDto.class)
|
||||
private java.util.List<SampleTargetDto> target;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package io.shinhanlife.axhub.biz.mcp.tool.dto;
|
||||
|
||||
import io.shinhanlife.glow.GlowTrgmField;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @package io.shinhanlife.axhub.biz.mcp.tool.dto
|
||||
* @className SampleTargetDto
|
||||
* @description AX HUB 시스템 처리 클래스
|
||||
* @author 김형식
|
||||
* @create 2026.09.01
|
||||
* <pre>
|
||||
* ---------- 개정이력 ----------
|
||||
* 수정일 수정자 수정내용
|
||||
* ---------- -------- ---------------------------
|
||||
* 2026.09.01 김형식 최초생성
|
||||
*
|
||||
* </pre>
|
||||
*/
|
||||
@Data
|
||||
public class SampleTargetDto {
|
||||
@GlowTrgmField(order = 1, length = 5, description = "항목 코드")
|
||||
private String itemCode;
|
||||
|
||||
@GlowTrgmField(order = 2, length = 5, description = "항목 값")
|
||||
private String itemValue;
|
||||
}
|
||||
Reference in New Issue
Block a user