Jooq: Add <enumConverter/> flag in <forcedType/> to auto-generate EnumConverter

Created on 15 Feb 2017  路  10Comments  路  Source: jOOQ/jOOQ

Enum converters follow a pretty dumb pattern. For example:

public class AppTypeConverter extends EnumConverter<String, AppType> {
    public AppTypeConverter() {
        super(String.class, AppType.class);
    }
}

Couldn't this work somehow be picked up by the jOOQ generator instead of having to do it manually?

Suggestion:

<forcedType>
    <userType>com.abc.AppType</userType>
    <enumConverter/>
    <expression>.*app_type</expression>
    <types>text</types>
</forcedType>

And the generator would then detect the <enumConverter/> tag and generate the specific converter.

Code Generation Medium Fixed Enhancement

Most helpful comment

OK, implementing <enumConverter>true</enumConverter> right now.

All 10 comments

Thanks a lot for your suggestion. You're right and I've thought about this myself, before - I don't know why I didn't follow up with this thought.

Will definitely implement this for 3.10

I have thought about this again. I think we can go ahead exactly as suggested, including the XML element name. The XSD specification will be amended from:

      <element name="converter" type="string" minOccurs="0" maxOccurs="1" />

to

      <choice>
        <element name="converter" type="string" minOccurs="0" maxOccurs="1" />
        <element name="enumConverter" type="boolean" minOccurs="0" maxOccurs="1" fixed="true" />
      </choice>

XJC doesn't reflect this choice element, nor the fixed attribute, but I think it will be clear from the API what is meant, if users use this feature programmatically.

Hmm, it looks as though the fixed semantics is not supported in this way when going through the Maven plugin. I guess we'll have to require users to explicitly pass the flag:

    <enumConverter>true</enumConverter>

Have you considered the alternative: keeping the same tag but using a special value that can't possibly conflict with a valid class name. For example:

<converter>[ENUM]</converter>

This would have the advantage of making it easy to add other special generated converters over time without changing the XML schema.

I'd rather not cleverly recycle XML elements. That will make things rather confusing, e.g.:

  • Documentation
  • Implementation
  • Future features that are not possible to model with a simple "tag"

How about a mixture of both approaches:

<autoConverter>enum</autoConverter>

(This adds extensibility, but then again it most likely doesn't cover cases like #5876 )

Same problem:

  • Future features that are not possible to model with a simple "tag"

What if an <autoConverter> needs additional parameters?

Point taken. I agree with you.

OK, implementing <enumConverter>true</enumConverter> right now.

Finally documented this in the manual for versions 3.10+: https://github.com/jOOQ/jOOQ/issues/6932

Was this page helpful?
0 / 5 - 0 ratings