jOOQ 枚举强制类型与注解

huangapple go评论137阅读模式
英文:

jOOQ enum forcedType with annotation

问题

  1. jooq {
  2. version.set(Versions.jooq)
  3. edition.set(OSS)
  4. configurations {
  5. create("main") {
  6. jooqConfiguration.apply {
  7. logging = Logging.INFO
  8. generator.apply {
  9. name = "org.jooq.codegen.KotlinGenerator"
  10. database.apply {
  11. name = "com.example.generator.StandalonePostgreSQLDatabase"
  12. inputSchema = "public"
  13. excludes = "flyway_schema_history"
  14. // The following lines are added for enum annotation handling
  15. forcedTypes.addAll(
  16. annotatedEnums.map { includeType ->
  17. ForcedType().apply {
  18. isEnumConverter = true
  19. this.includeTypes = includeType
  20. userType = "com.example.api.enums.${includeType.capitalize()}"
  21. }
  22. }
  23. )
  24. }
  25. generate.apply {
  26. isDeprecated = false
  27. isRecords = true
  28. isImmutablePojos = true
  29. isFluentSetters = true
  30. }
  31. target.apply {
  32. packageName = "com.example.api"
  33. directory = "build/generated/jooq"
  34. }
  35. strategy.name = "com.example.common.JPrefixGeneratorStrategy"
  36. }
  37. }
  38. }
  39. }
  40. }
  41. // Define a function to extract annotated enum values
  42. fun <reified T : Enum<T>> findAnnotatedEnums(): List<String> {
  43. return enumValues<T>().filter { it::class.java.isEnumAnnotated() }.map { it.name }
  44. }
  45. // Define an extension function to check if an enum class is annotated with @DatabaseEnum
  46. inline fun <reified T : Enum<T>> T::class.java.isEnumAnnotated(): Boolean {
  47. return this::class.java.isAnnotationPresent(DatabaseEnum::class.java)
  48. }
  49. // Get a list of annotated enum values
  50. val annotatedEnums = findAnnotatedEnums<DayOfWeek>()

Note: The code above provides a general idea of how you can dynamically add forcedType entries based on the @DatabaseEnum annotation. You may need to adapt and integrate this code into your Gradle build script and project structure as needed.

英文:

Currently I define the enum forcedType in gradle jooq configuration, but I would like to make it automatically set by annotation.

  1. jooq {
  2. version.set(Versions.jooq)
  3. edition.set(OSS)
  4. configurations {
  5. create(&quot;main&quot;) {
  6. jooqConfiguration.apply {
  7. logging = Logging.INFO
  8. generator.apply {
  9. name = &quot;org.jooq.codegen.KotlinGenerator&quot;
  10. database.apply {
  11. name = &quot;com.example.generator.StandalonePostgreSQLDatabase&quot;
  12. inputSchema = &quot;public&quot;
  13. excludes = &quot;flyway_schema_history&quot;
  14. forcedTypes.addAll(
  15. listOf(
  16. ForcedType().apply {
  17. isEnumConverter = true
  18. includeTypes = &quot;day_of_week&quot;
  19. userType = &quot;com.example.api.enums.DayOfWeek&quot;
  20. }
  21. )
  22. )
  23. }
  24. generate.apply {
  25. isDeprecated = false
  26. isRecords = true
  27. isImmutablePojos = true
  28. isFluentSetters = true
  29. }
  30. target.apply {
  31. packageName = &quot;com.example.api&quot;
  32. directory = &quot;build/generated/jooq&quot;
  33. }
  34. strategy.name = &quot;com.example.common.JPrefixGeneratorStrategy&quot;
  35. }
  36. }
  37. }
  38. }
  39. }

When the enum class is annotated by @DatabaseEnum, I would like to add them as forcedType. Is there any possible way to do it?

  1. @Retention(AnnotationRetention.RUNTIME)
  2. annotation class DatabaseEnum(val includeType: String)
  3. @DatabaseEnum(includeType = &quot;day_of_week&quot;)
  4. enum class DayOfWeek {
  5. MONDAY,
  6. TUESDAY,
  7. WEDNESDAY,
  8. THURSDAY,
  9. FRIDAY,
  10. SATURDAY,
  11. SUNDAY
  12. }

答案1

得分: 1

以下是已翻译的内容:

有一个关于此功能的待处理特性请求:

截止到 jOOQ 3.18(可能也包括 3.19),这个功能还没有被内置提供。

显然,您可以自己实现它。为了使其工作,您需要:

  • 在生成代码时扫描您的类路径(即需要预编译注释类型)

  • 使用某种编程代码生成配置,或者实现自己的 Gradle 插件。

英文:

There's a pending feature request for precisely this:

As of jOOQ 3.18 (and probably 3.19) this isn't available out of the box yet.

You can obviously implement it yourself. For this to work you need:

huangapple
  • 本文由 发表于 2023年8月10日 21:09:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876037.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定