2

I'm using protobuf version 2.6.1 and I have this message configured in my .proto file -

message Login {
    optional string host = 1;
    optional uint32 port = 2;
    optional string user = 3;
    optional string password = 4;
}

How can I mask user and password fields from being printed to log or in toString()?

Cowabunga
  • 306
  • 2
  • 12

1 Answers1

-3

One way you can achieve this is using 'FieldMasks' in Proto 3.0 version

https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/util/FieldMaskUtil.html

https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/FieldMask

Elaborating,

  1. Define you fieldMask as this:

Login.user , Login.password

  1. Create a new Message with user and password set to null

  2. Merge your source message and the above new message for printing. Use this method in FieldMaskUtil

merge(FieldMask mask, Message source, Message.Builder destination)

  • i believe answer is misleading. The FieldMask does the reverse. It only prints what you specify in FieldMask. Here's the similar question asked by me ( i didnt read the FieldMask javadoc :-) also ) and expected it to mask : https://stackoverflow.com/questions/45355892/how-to-mask-certain-fields-in-protobuf – Harry Jul 29 '17 at 23:31
  • Why this answer is checked? Question is about protobuf 2, answer about protobuf 3. – Yuriy Alevohin Aug 30 '18 at 08:39