protoc-gen-marshal-zap
protoc-gen-marshal-zap is a protoc plugin to generate code that implements zapcore.ObjectMarshaler interface (uber-go/zap) to the protoc message.
Requirements
- Go compiler and tools
- protoc compiler
- protoc-gen-go
See https://github.com/protocolbuffers/protobuf-go
Installation
go install github.com/kei2100/protoc-gen-marshal-zap/plugin/protoc-gen-marshal-zap@latest
Usage
Define your proto file
syntax = "proto3";
package simple;
// If you want to use "marshal_zap.mask" option, import "marshal-zap.proto"
import "marshal-zap.proto";
message SimpleMessage {
string message = 1;
string secret_message = 2 [(marshal_zap.mask) = true];
}
Generate the code by protoc (Alternatively, you can use buf)
PROTOC_GEN_MARSHAL_ZAP_VERSION=v0.1.x # replace latest version
protoc -I. -I$(go env GOMODCACHE)/github.com/kei2100/protoc-gen-marshal-zap@${PROTOC_GEN_MARSHAL_ZAP_VERSION} --go_out=. --marshal-zap_out=. simple.proto
Output results should be:
simple.pb.go # auto-generated by protoc-gen-go
simple.pb.marshal-zap.go # auto-generated by protoc-gen-marshal-zap
simple.proto # original proto file
simple.pb.marshal-zap.go
is generated as follows
// Code generated by protoc-gen-marshal-zap. DO NOT EDIT.
// source: simple.proto
package simple
import (
"go.uber.org/zap/zapcore"
)
func (x *SimpleMessage) MarshalLogObject(enc zapcore.ObjectEncoder) error {
if x == nil {
return nil
}
enc.AddString("message", x.Message)
enc.AddString("secret_message", "[MASKED]")
return nil
}
Using Buf
marshal-zap Buf repository is here.
https://buf.build/kei2100/protoc-gen-marshal-zap
So, you can buf generate
by setting up the following in your project.
# buf.yaml
version: v1
deps:
- buf.build/kei2100/protoc-gen-marshal-zap
breaking:
use:
- FILE
lint:
use:
- DEFAULT
# buf.gen.yaml
version: v1
plugins:
- plugin: buf.build/protocolbuffers/go:v1.28.1
out: gen/go
opt: paths=source_relative
- plugin: marshal-zap
out: gen/go
opt: paths=source_relative
Development
Compile marshal-zap.proto
$ make proto
Run tests
$ make test