简体   繁体   中英

Avro maven plugin for optional fields

I am using the Avro maven plugin to process some Avro files and generate the Java classes. My goal is to tell the plugin to generate Optional getters whenever the field accepts null values, by declaring the type null. 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. But for the e-mail, that can be null, I want the plugin to just generate the Optional getter. I tried both gettersReturnOptional and createOptionalGetters configurations in the plugin, but none of them did achieved that. So, how can it be done?

I managed to get this behaviour by customizing the record.vm velocity template. Replacing

#if (${this.gettersReturnOptional})

with

#if ($field.schema().isNullable())

did the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM