Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support debug_redact field option #50

Merged
merged 1 commit into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
![test](https://github.com/kei2100/protoc-gen-marshal-zap/workflows/test/badge.svg)
![CodeQL](https://github.com/kei2100/protoc-gen-marshal-zap/workflows/CodeQL/badge.svg)

protoc-gen-marshal-zap is a protoc plugin to generate code that implements zapcore.ObjectMarshaler interface ([uber-go/zap](https://github.com/uber-go/zap)) to the protoc message.
protoc-gen-marshal-zap is a protoc plugin for implementing zapcore.ObjectMarshaler interface ([uber-go/zap](https://github.com/uber-go/zap)) on proto messages.

## Requirements

Expand Down Expand Up @@ -30,8 +30,14 @@ package simple;
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];
}
```

Expand All @@ -40,6 +46,9 @@ Generate the code by protoc (Alternatively, you can [use buf](#Using-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:
Expand Down Expand Up @@ -70,6 +79,8 @@ func (x *SimpleMessage) MarshalLogObject(enc zapcore.ObjectEncoder) error {

enc.AddString("secret_message", "[MASKED]")

enc.AddString("secret_message2", "[MASKED]")

return nil
}
```
Expand Down
39 changes: 25 additions & 14 deletions example/simple/simple.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions example/simple/simple.pb.marshal-zap.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions example/simple/simple.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ import "marshal-zap.proto";
message SimpleMessage {
string message = 1;
string secret_message = 2 [(marshal_zap.mask) = true];
string secret_message2 = 3 [debug_redact = true];
}
2 changes: 1 addition & 1 deletion plugin/protoc-gen-marshal-zap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func generatePrimitiveField(g *protogen.GeneratedFile, f *protogen.Field) {
}

func isMasked(opts *descriptorpb.FieldOptions) bool {
return proto.GetExtension(opts, pbzap.E_Mask).(bool)
return proto.GetExtension(opts, pbzap.E_Mask).(bool) || opts.GetDebugRedact()
}

func handleExplicitPresence(g *protogen.GeneratedFile, f *protogen.Field, generateFunc func(*protogen.GeneratedFile, *protogen.Field)) {
Expand Down
Loading