[英]Avro maven plugin for optional fields
I am using the Avro maven plugin to process some Avro files and generate the Java classes.我正在使用 Avro maven 插件来处理一些 Avro 文件并生成 Java 类。 My goal is to tell the plugin to generate Optional getters whenever the field accepts null values, by declaring the type null.
我的目标是通过声明类型 null 来告诉插件在字段接受 null 值时生成可选 getter。 For example:
例如:
{
"namespace": "br.com.gruposaga.core.event",
"type": "record",
"name": "UserEvent",
"fields": [
{
"name": "id",
"type": "int"
},
{
"name": "name",
"type": "string"
},
{
"name": "email",
"type": [
"null",
"string"
],
"default": null
}
]
}
So, in the definition mentioned above, the plugin should generate default getters for id and name.因此,在上面提到的定义中,插件应该为 id 和 name 生成默认的 getter。 But for the e-mail, that can be null, I want the plugin to just generate the Optional getter.
但是对于电子邮件,可以是 null,我希望插件只生成可选的 getter。 I tried both gettersReturnOptional and createOptionalGetters configurations in the plugin, but none of them did achieved that.
我在插件中尝试了 gettersReturnOptional 和 createOptionalGetters 配置,但它们都没有做到这一点。 So, how can it be done?
那么,怎么做呢?
I managed to get this behaviour by customizing the record.vm velocity template.我设法通过自定义 record.vm 速度模板来获得这种行为。 Replacing
更换
#if (${this.gettersReturnOptional})
with和
#if ($field.schema().isNullable())
did the trick.成功了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.