arnaudroger / SimpleFlatMapper

Fast and Easy mapping from database and csv to POJO. A java micro ORM, lightweight alternative to iBatis and Hibernate. Fast Csv Parser and Csv Mapper
http://simpleflatmapper.org
MIT License
435 stars 76 forks source link

Add JooqBiConverter to help JooqRecord <-> Java Object custom conversion #698

Open hank-cp opened 4 years ago

hank-cp commented 4 years ago

JooqBiConverter should be shared between RecordMapper and RecordUnmapper. It should be helpful to make code more lean and readable.

Usage:

List<JooqBiConverter> converters = new ArrayList<>();
converters.add(new JooqBiConverter<String, JSONObjectTest494.File>() {
    @Override
    public boolean match(Type modelType) {
        return TypeHelper.areEquals(modelType, JSONObjectTest494.File.class);
    }
    @Override
    public JSONObjectTest494.File get(String name, Context context) throws Exception {
        return new JSONObjectTest494.File(0L, name, null);
    }
    @Override
    public String convert(JSONObjectTest494.File file, Context context) throws Exception {
        return file.name;
    }
});

JooqMapperFactory mapperFactory = JooqMapperFactory.newInstance()
        .addBiConverters(converters);
hank-cp commented 4 years ago

Hidden rule: JooqMapperFactory.addBiConverters() could only be called once. Later converters will override previous added converters. RecordUnmapperBuilder need to be changed to fix this.

arnaudroger commented 4 years ago

might seat on that one for a bit, I think that concept can also be useful in other integrations.