protoc-gen-marshal-zap is a protoc plugin for implementing zapcore.ObjectMarshaler interface (uber-go/zap) on proto messages.
- Go compiler and tools
- protoc compiler
- protoc-gen-go
See https://github.com/protocolbuffers/protobuf-go
go install github.com/kei2100/protoc-gen-marshal-zap/plugin/protoc-gen-marshal-zap@latest
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];
// You can also use `debug_redact` option instead of `marshal_zap.mask` option
// cf. https://github.com/protocolbuffers/protobuf/blob/v22.0/src/google/protobuf/descriptor.proto#L632
string secret_message2 = 3 [debug_redact = 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
# If you don't use `marshal_zap.option`, it's even simpler
# protoc -I. --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]")
enc.AddString("secret_message2", "[MASKED]")
return nil
}
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
Compile marshal-zap.proto
$ make proto
Run tests
$ make test