/protoc-gen-marshal-zap

A protoc plugin to generate code that implements uber-go/zap zapcore.ObjectMarshaler

Primary LanguageGoApache License 2.0Apache-2.0

protoc-gen-marshal-zap

test CodeQL

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

protoc -I. -I$(go env GOPATH)/src/github.com/kei2100/protoc-gen-marshal-zap --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_marshal_zap.pb.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
}

Development

Compile marshal-zap.proto

$ make proto

Run tests

$ make test