code: def testMergeMessage(self):
      # Test merge one field.
      src = unittest_pb2.TestAllTypes()
      test_util.SetAllFields(src)
      for field in src.DESCRIPTOR.fields:
          if field.containing_oneof:
              continue
          field_name = field.name
          dst = unittest_pb2.TestAllTypes()
          # Only set one path to mask.
          mask = field_mask_pb2.FieldMask()
          mask.paths.append(field_name)
          mask.MergeMessage(src, dst)
          # The expected result message.
          msg = unittest_pb2.TestAllTypes()
          if field.label == descriptor.FieldDescriptor.LABEL_REPEATED:
              repeated_src = getattr(src, field_name)
              repeated_msg = getattr(msg, field_name)
              if (
                  field.cpp_type
                  == descriptor.FieldDescriptor.CPPTYPE_MESSAGE
              ):
                  for item in repeated_src:
                      repeated_msg.add().CopyFrom(item)
              else:
                  repeated_msg.extend(repeated_src)
          elif (
              field.cpp_type
              == descriptor.FieldDescriptor.CPPTYPE_MESSAGE
          ):
              getattr(msg, field_name).CopyFrom(
                  getattr(src, field_name)
              )
          else:
              setattr(msg, field_name, getattr(src, field_name))
          # Only field specified in mask is merged.
          self.assertEqual(msg, dst)
  
      # Test merge nested fields.
      nested_src = unittest_pb2.NestedTestAllTypes()
      nested_dst = unittest_pb2.NestedTestAllTypes()
      nested_src.child.payload.optional_int32 = 1234
      nested_src.child.child.payload.optional_int32 = 5678
      mask = field_mask_pb2.FieldMask()
      mask.FromJsonString("child.payload")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(1234, nested_dst.child.payload.optional_int32)
      self.assertEqual(0, nested_dst.child.child.payload.optional_int32)
  
      mask.FromJsonString("child.child.payload")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(1234, nested_dst.child.payload.optional_int32)
      self.assertEqual(
          5678, nested_dst.child.child.payload.optional_int32
      )
  
      nested_dst.Clear()
      mask.FromJsonString("child.child.payload")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(0, nested_dst.child.payload.optional_int32)
      self.assertEqual(
          5678, nested_dst.child.child.payload.optional_int32
      )
  
      nested_dst.Clear()
      mask.FromJsonString("child")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(1234, nested_dst.child.payload.optional_int32)
      self.assertEqual(
          5678, nested_dst.child.child.payload.optional_int32
      )
  
      # Test MergeOptions.
      nested_dst.Clear()
      nested_dst.child.payload.optional_int64 = 4321
      # Message fields will be merged by default.
      mask.FromJsonString("child.payload")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(1234, nested_dst.child.payload.optional_int32)
      self.assertEqual(4321, nested_dst.child.payload.optional_int64)
      # Change the behavior to replace message fields.
      mask.FromJsonString("child.payload")
      mask.MergeMessage(nested_src, nested_dst, True, False)
      self.assertEqual(1234, nested_dst.child.payload.optional_int32)
      self.assertEqual(0, nested_dst.child.payload.optional_int64)
  
      # By default, fields missing in source are not cleared in destination.
      nested_dst.payload.optional_int32 = 1234
      self.assertTrue(nested_dst.HasField("payload"))
      mask.FromJsonString("payload")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertTrue(nested_dst.HasField("payload"))
      # But they are cleared when replacing message fields.
      nested_dst.Clear()
      nested_dst.payload.optional_int32 = 1234
      mask.FromJsonString("payload")
      mask.MergeMessage(nested_src, nested_dst, True, False)
      self.assertFalse(nested_dst.HasField("payload"))
  
      nested_src.payload.repeated_int32.append(1234)
      nested_dst.payload.repeated_int32.append(5678)
      # Repeated fields will be appended by default.
      mask.FromJsonString("payload.repeatedInt32")
      mask.MergeMessage(nested_src, nested_dst)
      self.assertEqual(2, len(nested_dst.payload.repeated_int32))
      self.assertEqual(5678, nested_dst.payload.repeated_int32[0])
      self.assertEqual(1234, nested_dst.payload.repeated_int32[1])
      # Change the behavior to replace repeated fields.
      mask.FromJsonString("payload.repeatedInt32")
      mask.MergeMessage(nested_src, nested_dst, False, True)
      self.assertEqual(1, len(nested_dst.payload.repeated_int32))
      self.assertEqual(1234, nested_dst.payload.repeated_int32[0])
  
imports: from datetime import datetime
  from google.protobuf import any_pb2
  from google.protobuf import duration_pb2
  from google.protobuf import field_mask_pb2
  from google.protobuf import struct_pb2
  from google.protobuf import timestamp_pb2
  from google.protobuf import unittest_pb2
  from google.protobuf.internal import any_test_pb2
  from google.protobuf.internal import test_util
  from google.protobuf.internal import well_known_types
  from google.protobuf import descriptor
  from google.protobuf import text_format
label_desc: Use image node_id 6 for calling the FieldMaskTest obj's underlying member method code with example usage: obj.testMergeMessage() without return types