flipp-oss / protoc-gen-avro

Generate Avro schemas from Protobuf files.
MIT License
0 stars 2 forks source link

protoc-gen-avro

Generate Avro schemas from Protobuf files.

Usage

Install using Golang 1.21+

Simply run the following command:

go install github.com/flipp-oss/protoc-gen-avro@latest

Install Manually

Download this project from the Releases page. Put the generated binary in your path:

mv protoc-gen-avro /usr/local/bin

Generate Avro schemas from your Protobuf files:

protoc --avro_out=. *.proto

Options

protoc --avro_out=. --avro_opt=emit_only=Foo;Bar *.proto

This will generate only Foo.avsc and Bar.avsc files.

protoc --avro_out=. --avro_opt=namespace_map=foo:bar,baz:spam *.proto

...will change the output namespace for foo to bar and baz to spam.

protoc --avro_out=. --avro_opt=collapse_fields=StringList;SomeOtherRecord *.proto

If you have the input proto looking like:

message StringList {
  repeated string strings = 1;
}
message MyRecord {
    map<string, StringList> my_field = 1;
}

...the output will look like:

{
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {
      "name": "my_field",
      "type": {
        "type": "map",
        "values": {
          "type": {
            "type": "array",
            "items": "string"
          }
        }
      }
    }
  ]
}
enum Category {
  CATEGORY_GOOD = 0;
  CATEGORY_BAD = 1;
}

...with this option on, it will be translated into:

{
  "type": "enum",
  "name": "CATEGORY",
  "symbols": ["GOOD", "BAD"]
}
message MyRecord {
  map<int32, string> my_field = 1;
}

...with this option off, it will be translated into:

{
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {
      "name": "my_field",
      "type": {
        "type": "map",
        "values": "string"
      }
    }
  ]
}

...but with it on, it will be translated into:

{
  "type": "record",
  "name": "MyRecord",
  "fields": [
    {
      "name": "my_field",
      "type": {
        "type": "record",
        "fields": [
          {
            "name": "key",
            "type": "int"
          },
          {
            "name": "value",
            "type": "string"
          }
        ]
      }
    }
  ]
}

To Do List:


This project supported by Flipp.