英文:
jOOQ enum forcedType with annotation
问题
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("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"
forcedTypes.addAll(
listOf(
ForcedType().apply {
isEnumConverter = true
includeTypes = "day_of_week"
userType = "com.example.api.enums.DayOfWeek"
}
)
)
}
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"
}
}
}
}
}
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 = "day_of_week")
enum class DayOfWeek {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
答案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:
-
To scan your classpath while generating code (i.e. the annotated types need to be pre-compiled)
-
Use programmatic code generation configuration of some sort, or implement your own gradle plugin.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论