Skip to content

Make Converter accept TypeDescriptor #1820

Closed as not planned
Closed as not planned
@DreamStar92

Description

@DreamStar92

@schauder
The current converters are unable to handle universal types, such as: IntegerToEnumReadConverter, JsonReadConverter.

expected
a universal converter

    public interface Converter<S, T> {

        @Nullable
        T convert(S source, TypeDescriptor sourceTypeDescriptor);

    }

    public class JsonReadConverter implements Converter<String, JsonMapping> {

        private JsonMapper jsonMapper;

        @Override
        public JsonMapping convert(String source, TypeDescriptor sourceTypeDescriptor) {
            try {
//                current converter not support container class, so not use TypeReference
//                return jsonMapper.readValue(source, new TypeReference<JsonMapping>(){
//                    @Override
//                    public Type getType() {
//                        return sourceTypeDescriptor.getResolvableType().getType();
//                    }
//                });
                return (JsonMapping) jsonMapper.readValue(source, sourceTypeDescriptor.getResolvableType().getRawClass());
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
        }

    }

    public class EnumReadConverter implements Converter<Integer, EnumMapping> {

        @Override
        @SuppressWarnings("unchecked")
        public EnumMapping convert(Integer source, TypeDescriptor sourceTypeDescriptor) {
            try {
                return EnumMapping.indexOf((Class<? extends EnumMapping>) sourceTypeDescriptor.getResolvableType().getRawClass(), source);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }

    }

now
each type needs to declare a converter

@Component("inventoryConverters")
public class Converters implements ConverterRegistrar {
    @Override
    public List<?> converters(JsonMapper jsonMapper) {
        return new ArrayList<Object>() {{
            add(new EnumReadConverter<Category>(Category.class) {
            });
            add(new JsonReadConverter<AvailableWarehouseTypes>(AvailableWarehouseTypes.class, jsonMapper) {
            });
            add(new EnumReadConverter<SkuUnit>(SkuUnit.class) {
            });
            add(new JsonReadConverter<SkuUnitItems>(SkuUnitItems.class, jsonMapper) {
            });
            add(new EnumReadConverter<WarehouseType>(WarehouseType.class) {
            });
        }};
    }
}

Anticipated modifications
Interface adjustment and related calls in data-jdbc

@FunctionalInterface
public interface Converter<S, T> {

	@Nullable
        @Deprecated
	default T convert(S source){
        };

        @Nullable
        default T convert(S source, TypeDescriptor sourceTypeDescriptor) {
            convert(source);
        }

	default <U> Converter<S, U> andThen(Converter<? super T, ? extends U> after) {
		Assert.notNull(after, "After Converter must not be null");
		return (S s) -> {
			T initialResult = convert(s);
			return (initialResult != null ? after.convert(initialResult) : null);
		};
	}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: declinedA suggestion or change that we don't feel we should currently apply

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions