MS-Jing commented on issue #988: URL: https://github.com/apache/fesod/issues/988#issuecomment-5261830229
@nkuprins Thank you for your reply,I carefully read your proposal in [#198 ](https://github.com/apache/fesod/issues/198) I think the `fesod` framework should not create the `Dict` interface. Because the framework should not be concerned with the specific values of the business. This `Dict` interface should be created by the specific developers. Only in this way can the `fesod` framework be more pure. ``` java // 1. scalar field, mapping used once @DictFormat({"1=Success", "0=Failure"}) private Integer status; // 2. scalar field, mapping already exists as an enum @DictFormat(mapping = Status.class) // Status implements Dict: code() + label() private Integer status; // 3. the field IS the enum @DictFormat private UserStatus status; // UserStatus implements Labeled: label() // 4. the field IS the enum, no annotation at all private UserStatus status; ``` + In `1`, This `DictFormat` annotation will have some of the validation issues you mentioned in [#198 ](https://github.com/apache/fesod/issues/198). + In `2`, This `@DictFormat(mapping = Status.class)` annotation is equivalent to the `@ExcelProperty(converter = IEnumConverter.class)`. annotation. And there is no need to pay attention to the specific `Status.class`. + In `3`, This DictFormat annotation is merely a label. + In `4`, No other unnecessary annotations. It is more acceptable to business developers and reduces their psychological burden.They only need to implement the `Labeled` interface for their business enumerations, and then they can handle the values of the enumeration types just like basic data types. So, I think the @DictFormat annotation is not mandatory. As I mentioned in [#1006 ](https://github.com/apache/fesod/issues/1006). Only two files need to be modified and two more files need to be added. ```java public interface Labeled { String getLabel(); } ``` ```java public class IEnumConverter implements Converter<Labeled> { private static final Map<Class<?>, List<Labeled>> enumCache = new HashMap<>(); @Override public Class<?> supportJavaTypeKey() { return Labeled.class; } @Override public CellDataTypeEnum supportExcelTypeKey() { return CellDataTypeEnum.STRING; } @Override public WriteCellData<?> convertToExcelData(Labeled value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { if (value != null) { return new WriteCellData<>(value.getLabel()); } return new WriteCellData<>(); } @Override public Labeled convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception { Class<?> type = contentProperty.getField().getType(); if (!enumCache.containsKey(type)) { Object[] enumConstants = type.getEnumConstants(); List<Labeled> labeledList = Arrays.stream(enumConstants).map(enumConstant -> (Labeled) enumConstant).collect(Collectors.toList()); enumCache.put(type, labeledList); } List<Labeled> labeledList = enumCache.getOrDefault(type, Collections.emptyList()); for (Labeled labeled : labeledList) { if (labeled.getLabel().equals(cellData.getStringValue())) { return labeled; } } return null; } } ``` modification `ConverterKeyBuild#buildKey`: ```java public static ConverterKey buildKey(Class<?> clazz, CellDataTypeEnum cellDataTypeEnum) { Class<?> boxingClass = BOXING_MAP.get(clazz); if (boxingClass != null) { return new ConverterKey(boxingClass, cellDataTypeEnum); } if (clazz == Labeled.class || (clazz.isEnum() && Labeled.class.isAssignableFrom(clazz))) { // clazz equals IEnum.class or clazz is enum and IEnum is assignable from clazz return new ConverterKey(Labeled.class, cellDataTypeEnum); } return new ConverterKey(clazz, cellDataTypeEnum); } ``` modification `DefaultConverterLoader#initDefaultWriteConverter` and `DefaultConverterLoader#initAllConverter`: ```java private static void initAllConverter() { ...... putAllConverter(new IEnumConverter()); } private static void initDefaultWriteConverter() { ...... putWriteConverter(new IEnumConverter()); ...... putWriteStringConverter(new IEnumConverter()); } ``` __Business developers only need to handle the business enumerations properly and can use it normally without performing any other operations.__ ```java @Test public void testLabeledConverter() { String fileName = System.getProperty("user.dir") + "/iEnumConverter.xlsx"; // 写出枚举类型字段 FastExcel.write(fileName) .head(UserInfo.class) .sheet() .doWrite(testDataList()); // 读取枚举类型字段 List<UserInfo> list = FastExcel.read(fileName) .head(UserInfo.class) .sheet() .doReadSync(); System.out.println(list); } ``` __I am more than willing to submit the PR. Looking forward to your reply__ My English is not very good. It's all translated by software. If there are any mistakes, please correct them. thanks again -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
