简体   繁体   English

用于可选字段的 Avro maven 插件

[英]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.

相关问题 使用 avro-maven-plugin 1.9.0 生成十进制数据类型的类 - Generate classes with decimal datatype with avro-maven-plugin 1.9.0 Apache Avro Maven 插件生成 UUID 字段而不是字符串 - Apache Avro Maven Plugin generate UUID field instead of String Maven avro 插件给出了尚不支持架构的错误 - Maven avro plugin gives schema not yet supported error avro maven 插件无法为 map 架构生成代码 - avro maven plugin can not generate code for map schema 使用 build-helper-maven-plugin 添加生成的 Apache Avro 源 - Add generated Apache Avro source with build-helper-maven-plugin 是否可以使使用 Avro Maven 插件生成的 POJO 与 Jackson 兼容? - Is it possible to make POJOs generated with the Avro Maven plugin play nice with Jackson? 如何使用 Surefire 插件在 Maven 中使属性成为可选 - How to make property optional in Maven with Surefire plugin 可选 jar 包含在 maven 插件中的胖 jar 中 - Optional jar inclusion in fat jar in maven plugin 目标 org.apache.avro:avro-maven-plugin:1 .10.2:schema failed: Invalid default for field 的执行默认值 - Execution default of goal org.apache.avro:avro-maven-plugin:1 .10.2:schema failed: Invalid default for field 将 maven-bundle-plugin 包含的资源声明为可选 - Declare resource included by maven-bundle-plugin as optional
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM