How to mask a map field by Fieldmask in Python protobuf?












1














I want to mask a protobuf message using FieldMask in Python.



proto file



syntax = "proto3";

message Msg {

message ImageInfo {
string url = 1;
int32 width = 2;
int32 height = 3;
}

string name = 1;
map<string, ImageInfo> image_info = 2;
}


Masking string field can be successfully done.



from google.protobuf import field_mask_pb2
import msg_pb2

original_msg = msg_pb2.Msg(name='image-0')
original_msg.image_info['main'].url = 'http://example.com/image-0.jpg'
original_msg.image_info['main'].width = 10
original_msg.image_info['main'].height = 10

field_mask=field_mask_pb2.FieldMask(paths=['name'])
new_msg = msg_pb2.Msg()
field_mask.MergeMessage(original_msg, new_msg)


But, masking map<string, message> field fails.



field_mask=field_mask_pb2.FieldMask(paths=['name', 'image_info'])
new_msg = msg_pb2.Msg()
field_mask.MergeMessage(original_msg, new_msg)

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 473, in MergeMessage
source, destination, replace_message_field, replace_repeated_field) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 625, in MergeMessage
self._root, source, destination, replace_message, replace_repeated) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 667, in _MergeMessage
repeated_destination.add().MergeFrom(item) AttributeError: 'google.protobuf.pyext._message.MessageMapContainer' object has no attribute 'add'


How to mask it ?



Edit



One solution is wrapper message. But It's a little bother.



syntax = "proto3";

message Msg {

message ImageInfo {
string url = 1;
int32 width = 2;
int32 height = 3;
}
message ImageInfoContainer {
map<string, ImageInfo> image_info = 1;
}

string name = 1;
ImageInfoContainer image_info = 2;
}









share|improve this question





























    1














    I want to mask a protobuf message using FieldMask in Python.



    proto file



    syntax = "proto3";

    message Msg {

    message ImageInfo {
    string url = 1;
    int32 width = 2;
    int32 height = 3;
    }

    string name = 1;
    map<string, ImageInfo> image_info = 2;
    }


    Masking string field can be successfully done.



    from google.protobuf import field_mask_pb2
    import msg_pb2

    original_msg = msg_pb2.Msg(name='image-0')
    original_msg.image_info['main'].url = 'http://example.com/image-0.jpg'
    original_msg.image_info['main'].width = 10
    original_msg.image_info['main'].height = 10

    field_mask=field_mask_pb2.FieldMask(paths=['name'])
    new_msg = msg_pb2.Msg()
    field_mask.MergeMessage(original_msg, new_msg)


    But, masking map<string, message> field fails.



    field_mask=field_mask_pb2.FieldMask(paths=['name', 'image_info'])
    new_msg = msg_pb2.Msg()
    field_mask.MergeMessage(original_msg, new_msg)

    Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 473, in MergeMessage
    source, destination, replace_message_field, replace_repeated_field) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 625, in MergeMessage
    self._root, source, destination, replace_message, replace_repeated) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 667, in _MergeMessage
    repeated_destination.add().MergeFrom(item) AttributeError: 'google.protobuf.pyext._message.MessageMapContainer' object has no attribute 'add'


    How to mask it ?



    Edit



    One solution is wrapper message. But It's a little bother.



    syntax = "proto3";

    message Msg {

    message ImageInfo {
    string url = 1;
    int32 width = 2;
    int32 height = 3;
    }
    message ImageInfoContainer {
    map<string, ImageInfo> image_info = 1;
    }

    string name = 1;
    ImageInfoContainer image_info = 2;
    }









    share|improve this question



























      1












      1








      1







      I want to mask a protobuf message using FieldMask in Python.



      proto file



      syntax = "proto3";

      message Msg {

      message ImageInfo {
      string url = 1;
      int32 width = 2;
      int32 height = 3;
      }

      string name = 1;
      map<string, ImageInfo> image_info = 2;
      }


      Masking string field can be successfully done.



      from google.protobuf import field_mask_pb2
      import msg_pb2

      original_msg = msg_pb2.Msg(name='image-0')
      original_msg.image_info['main'].url = 'http://example.com/image-0.jpg'
      original_msg.image_info['main'].width = 10
      original_msg.image_info['main'].height = 10

      field_mask=field_mask_pb2.FieldMask(paths=['name'])
      new_msg = msg_pb2.Msg()
      field_mask.MergeMessage(original_msg, new_msg)


      But, masking map<string, message> field fails.



      field_mask=field_mask_pb2.FieldMask(paths=['name', 'image_info'])
      new_msg = msg_pb2.Msg()
      field_mask.MergeMessage(original_msg, new_msg)

      Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 473, in MergeMessage
      source, destination, replace_message_field, replace_repeated_field) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 625, in MergeMessage
      self._root, source, destination, replace_message, replace_repeated) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 667, in _MergeMessage
      repeated_destination.add().MergeFrom(item) AttributeError: 'google.protobuf.pyext._message.MessageMapContainer' object has no attribute 'add'


      How to mask it ?



      Edit



      One solution is wrapper message. But It's a little bother.



      syntax = "proto3";

      message Msg {

      message ImageInfo {
      string url = 1;
      int32 width = 2;
      int32 height = 3;
      }
      message ImageInfoContainer {
      map<string, ImageInfo> image_info = 1;
      }

      string name = 1;
      ImageInfoContainer image_info = 2;
      }









      share|improve this question















      I want to mask a protobuf message using FieldMask in Python.



      proto file



      syntax = "proto3";

      message Msg {

      message ImageInfo {
      string url = 1;
      int32 width = 2;
      int32 height = 3;
      }

      string name = 1;
      map<string, ImageInfo> image_info = 2;
      }


      Masking string field can be successfully done.



      from google.protobuf import field_mask_pb2
      import msg_pb2

      original_msg = msg_pb2.Msg(name='image-0')
      original_msg.image_info['main'].url = 'http://example.com/image-0.jpg'
      original_msg.image_info['main'].width = 10
      original_msg.image_info['main'].height = 10

      field_mask=field_mask_pb2.FieldMask(paths=['name'])
      new_msg = msg_pb2.Msg()
      field_mask.MergeMessage(original_msg, new_msg)


      But, masking map<string, message> field fails.



      field_mask=field_mask_pb2.FieldMask(paths=['name', 'image_info'])
      new_msg = msg_pb2.Msg()
      field_mask.MergeMessage(original_msg, new_msg)

      Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 473, in MergeMessage
      source, destination, replace_message_field, replace_repeated_field) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 625, in MergeMessage
      self._root, source, destination, replace_message, replace_repeated) File "/opt/project/test-1/lib/python3.7/site-packages/google/protobuf/internal/well_known_types.py", line 667, in _MergeMessage
      repeated_destination.add().MergeFrom(item) AttributeError: 'google.protobuf.pyext._message.MessageMapContainer' object has no attribute 'add'


      How to mask it ?



      Edit



      One solution is wrapper message. But It's a little bother.



      syntax = "proto3";

      message Msg {

      message ImageInfo {
      string url = 1;
      int32 width = 2;
      int32 height = 3;
      }
      message ImageInfoContainer {
      map<string, ImageInfo> image_info = 1;
      }

      string name = 1;
      ImageInfoContainer image_info = 2;
      }






      protocol-buffers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 '18 at 10:02

























      asked Nov 20 '18 at 8:58









      takaomag

      717718




      717718





























          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389409%2fhow-to-mask-a-map-field-by-fieldmask-in-python-protobuf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown






























          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389409%2fhow-to-mask-a-map-field-by-fieldmask-in-python-protobuf%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Paul Cézanne

          UIScrollView CustomStickyHeader Resize height generates problems when scroll is too fast

          Angular material date-picker (MatDatepicker) auto completes the date on focus out