protoc-gen-broker is a protoc plugin to generate a broker interface for protobuf messages.
Description
The goal is generating contracts by protobuf models. It can make easy data transfer with broker. Plugin supports JSON and protobuf encodings.
The plugin generates a broker interface with methods:
- method for sending to broker;
- method for handling received message.
Build
Execute below command for building protoc plugin.
make build
protoc plugin has been made in bin folder.
Example
There is content example.proto.
syntax = "proto3";
package example;
import "broker/broker.proto";
option go_package = "example/example";
message Foo {
option (broker.generate) = true;
option (broker.encoder) = ENCODER_JSON;
string example = 1;
}
message Bar {
option (broker.generate) = true;
option (broker.encoder) = ENCODER_PROTOBUF;
string example = 1;
}
message FooBar {
string example = 1;
}
Option broker.generate used for generating a broker interface. Option broker.encoder allows to select an encode method (default JSON)
Then, generate pb files from example.proto using below command:
PATH=`pwd`/bin:$PATH protoc -I . --proto_path=example \
--go_out=. --go_opt=paths=source_relative \
--broker_out=. --broker_opt=paths=source_relative \
example/example.proto
Messages Foo and Bar have broker interfaces. You can see it in file example/example_broker.pb.go:
...
// Foo broker interface
type FooBrokerProducerInterface interface {
Write(context.Context, *Foo) error
}
type FooHandler func(context.Context, *Foo) error
type FooBrokerConsumerInterface interface {
Handle(context.Context, *Foo) error
}
// Bar broker interface
type BarBrokerProducerInterface interface {
Write(context.Context, *Bar) error
}
type BarHandler func(context.Context, *Bar) error
type BarBrokerConsumerInterface interface {
Handle(context.Context, *Bar) error
}
...
Warning
You can generate broker interface in only folder with generated protobuf models now. Maybe I will fix it in the near future.
License
MIT