actgardner / gogen-avro

Generate Go code to serialize and deserialize Avro schemas
MIT License
366 stars 87 forks source link

Fix build error if record has empty array of fields #202

Closed nicanor-romero closed 8 months ago

nicanor-romero commented 1 year ago

Hello! I have found a corner case where an avro record with an empty array as fields will yield go code that does not build due to unused variables.

Example of avro (attention to "fields":[ ]):

{
  "type":"record",
  "name":"TestAvro",
  "fields":[
    {
      "name":"TestAvroSubType",
      "type":[
        {
          "type":"record",
          "name":"SubType_1",
          "fields":[ ]
        }
      ]
    }
  ]
}

When running gogen-avro on this test_avro.avsc it will output this in the file sub_type_1.go:

func (r SubType_1) MarshalJSON() ([]byte, error) {
    var err error
    output := make(map[string]json.RawMessage)
    return json.Marshal(output)
}

func (r *SubType_1) UnmarshalJSON(data []byte) error {
    var fields map[string]json.RawMessage
    if err := json.Unmarshal(data, &fields); err != nil {
        return err
    }

    var val json.RawMessage
    return nil
}

where the var err error and var val json.RawMessage are unused, so the build fails.


When running gogen-avro after the proposed fix, it will yield the following:

func (r SubType_1) MarshalJSON() ([]byte, error) {
    output := make(map[string]json.RawMessage)
    return json.Marshal(output)
}

func (r *SubType_1) UnmarshalJSON(data []byte) error {
    var fields map[string]json.RawMessage
    if err := json.Unmarshal(data, &fields); err != nil {
        return err
    }
    return nil
}
tanis2000 commented 1 year ago

@actgardner any chance we can get this one merged?