51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

jOOQ 枚举强制类型与注解

英文:

jOOQ enum forcedType with annotation

问题 {#heading}

jooq {
  version.set(Versions.jooq)
  edition.set(OSS)

configurations { create("main") { jooqConfiguration.apply { logging = Logging.INFO generator.apply { name = "org.jooq.codegen.KotlinGenerator" database.apply { name = "com.example.generator.StandalonePostgreSQLDatabase" inputSchema = "public" excludes = "flyway_schema_history"

        // The following lines are added for enum annotation handling
        forcedTypes.addAll(
          annotatedEnums.map { includeType ->
            ForcedType().apply {
              isEnumConverter = true
              this.includeTypes = includeType
              userType = "com.example.api.enums.${includeType.capitalize()}"
            }
          }
        )
      }
      generate.apply {
        isDeprecated = false
        isRecords = true
        isImmutablePojos = true
        isFluentSetters = true
      }
      target.apply {
        packageName = "com.example.api"
        directory = "build/generated/jooq"
      }
      strategy.name = "com.example.common.JPrefixGeneratorStrategy"
    }
  }
}

} }

// Define a function to extract annotated enum values fun <reified T : Enum<T>> findAnnotatedEnums(): List<String> { return enumValues<T>().filter { it::class.java.isEnumAnnotated() }.map { it.name } }

// Define an extension function to check if an enum class is annotated with @DatabaseEnum inline fun <reified T : Enum<T>> T::class.java.isEnumAnnotated(): Boolean { return this::class.java.isAnnotationPresent(DatabaseEnum::class.java) }

// Get a list of annotated enum values 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.

jooq {
  version.set(Versions.jooq)
  edition.set(OSS)

configurations { create(&quot;main&quot;) { jooqConfiguration.apply { logging = Logging.INFO generator.apply { name = &quot;org.jooq.codegen.KotlinGenerator&quot; database.apply { name = &quot;com.example.generator.StandalonePostgreSQLDatabase&quot; inputSchema = &quot;public&quot; excludes = &quot;flyway_schema_history&quot; forcedTypes.addAll( listOf( ForcedType().apply { isEnumConverter = true includeTypes = &quot;day_of_week&quot; userType = &quot;com.example.api.enums.DayOfWeek&quot; } ) ) } generate.apply { isDeprecated = false isRecords = true isImmutablePojos = true isFluentSetters = true } target.apply { packageName = &quot;com.example.api&quot; directory = &quot;build/generated/jooq&quot; } strategy.name = &quot;com.example.common.JPrefixGeneratorStrategy&quot; } } } } }

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

@Retention(AnnotationRetention.RUNTIME)
annotation class DatabaseEnum(val includeType: String)

@DatabaseEnum(includeType = &quot;day_of_week&quot;) enum class DayOfWeek { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY }

答案1 {#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:


赞(1)
未经允许不得转载:工具盒子 » jOOQ 枚举强制类型与注解