marshal_zap

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2023 License: Apache-2.0 Imports: 4 Imported by: 0

README

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 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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// optional bool mask = 50001;
	E_Mask = &file_marshal_zap_proto_extTypes[0]
)

Extension fields to descriptorpb.FieldOptions.

View Source
var File_marshal_zap_proto protoreflect.FileDescriptor

Functions

This section is empty.

Types

This section is empty.

Directories

Path Synopsis
example
plugin
test

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL