zap_marshaler

package module
v0.0.0-...-fb24ed9 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: MIT Imports: 4 Imported by: 0

README

go-proto-zap-marshaler

A protoc plugin whitch generates MarshalLogObject() functions for each generated go structs to implement zapcore.ObjectMarshaler interface for uber-go/zap. So you can use zap.Object("proto", someProtoMessage) to log contents of the proto message.

Install

There are 2 protoc plugins to generate marshaler functions for zap:

  • protoc-gen-zap-marshaler
    • which genearetes marshaler functions for all messages.
  • protoc-gen-zap-marshaler-secure
    • which genearetes marshaler functions for all messages, but only logs fields enabled by proto option.

You can install those plugins by the following command:

$ go get github.com/kazegusuri/go-proto-zap-marshaler/protoc-gen-zap-marshaler
$ go get github.com/kazegusuri/go-proto-zap-marshaler/protoc-gen-zap-marshaler-secure

Usage

protoc-gen-zap-marshaler

To generate marshaler functions from proto, use --zap-marshaler_out with protoc command. That runs protoc-gen-zap-marshaler internally and then generates *.zap.go files.

$ protoc --zap-marshaler_out=. path/to/example.proto
protoc-gen-zap-marshaler-secure

This plugin generates marshaler functions as well as protoc-gen-zap-marshaler, but the funtions does not log anything as default. The marshaler only marshal fields enabled by zap_marshaler option.

To enable field option for message fields, you need to define proto like this:

message SimpleMessage {
  string string_value = 1 [(kazegusuri.zap_mashaler.field) = {enabled: true}];
  bool bool_value = 2 [(kazegusuri.zap_mashaler.field) = {enabled: true}];
}

To generate marshaler functions from proto, use --zap-marshaler-secure_out with protoc command. That runs protoc-gen-zap-marshaler-secure internally and then generates *.zap.go files.

$ protoc --zap-marshaler-secure_out=. path/to/example.proto
Field option
message ZapMarshalerRule {
    bool enabled = 1;
}
  • enabled
    • The marshaler only logs a field whose value is true
Example

For this proto without field option, the secure plugin generates a go function:

syntax = "proto3";
package example;

message SimpleMessage {
  string string_value = 1;
  bool bool_value = 2;
}
func (m *SimpleMessage) MarshalLogObject(enc go_uber_org_zap_zapcore.ObjectEncoder) error {
	var keyName string
	_ = keyName

	return nil
}

The generated function does nothing.

By changing the proto with field option like this, the secure plugin generates a go function:

syntax = "proto3";
package example;

import "kazegusuri/go-prot-zap-marshaler/zap_marshaler.proto";

message SimpleMessage {
  string string_value = 1 [(kazegusuri.zap_mashaler.field) = {enabled: true}];
  bool bool_value = 2 [(kazegusuri.zap_mashaler.field) = {enabled: true}];
}
func (m *SimpleMessage) MarshalLogObject(enc go_uber_org_zap_zapcore.ObjectEncoder) error {
	var keyName string
	_ = keyName

	keyName = "string_value" // field string_value = 1
	enc.AddString(keyName, m.StringValue)

	keyName = "bool_value" // field bool_value = 2
	enc.AddBool(keyName, m.BoolValue)

	return nil
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var E_Field = &proto.ExtensionDesc{
	ExtendedType:  (*descriptor.FieldOptions)(nil),
	ExtensionType: (*ZapMarshalerRule)(nil),
	Field:         64553,
	Name:          "kazegusuri.zap_mashaler.field",
	Tag:           "bytes,64553,opt,name=field",
	Filename:      "zap_marshaler.proto",
}

Functions

This section is empty.

Types

type ZapMarshalerRule

type ZapMarshalerRule struct {
	Enabled              bool     `protobuf:"varint,1,opt,name=enabled,proto3" json:"enabled,omitempty"`
	XXX_NoUnkeyedLiteral struct{} `json:"-"`
	XXX_unrecognized     []byte   `json:"-"`
	XXX_sizecache        int32    `json:"-"`
}

func (*ZapMarshalerRule) Descriptor

func (*ZapMarshalerRule) Descriptor() ([]byte, []int)

func (*ZapMarshalerRule) GetEnabled

func (m *ZapMarshalerRule) GetEnabled() bool

func (*ZapMarshalerRule) ProtoMessage

func (*ZapMarshalerRule) ProtoMessage()

func (*ZapMarshalerRule) Reset

func (m *ZapMarshalerRule) Reset()

func (*ZapMarshalerRule) String

func (m *ZapMarshalerRule) String() string

func (*ZapMarshalerRule) XXX_DiscardUnknown

func (m *ZapMarshalerRule) XXX_DiscardUnknown()

func (*ZapMarshalerRule) XXX_Marshal

func (m *ZapMarshalerRule) XXX_Marshal(b []byte, deterministic bool) ([]byte, error)

func (*ZapMarshalerRule) XXX_Merge

func (dst *ZapMarshalerRule) XXX_Merge(src proto.Message)

func (*ZapMarshalerRule) XXX_Size

func (m *ZapMarshalerRule) XXX_Size() int

func (*ZapMarshalerRule) XXX_Unmarshal

func (m *ZapMarshalerRule) XXX_Unmarshal(b []byte) error

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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