_merge.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (c) 2025-2026 Buf Technologies, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from __future__ import annotations
  15. from typing import TypeVar
  16. from ._message import Message
  17. T = TypeVar("T", bound=Message)
  18. def merge_from(target: T, source: T, *, ignore_unknown_fields: bool = False) -> None:
  19. """Merges the fields from source into target.
  20. Merge rules by field kind:
  21. - Scalar and enum: the existing value is overwritten.
  22. - Message: recursively merged if already present, otherwise set.
  23. - Repeated: elements are appended.
  24. - Map: entries are added; existing keys are overwritten. Message-valued map entries are not merged.
  25. - Unknown fields: retained unless `ignore_unknown_fields` is `True`.
  26. Args:
  27. target: The message instance to merge into.
  28. source: The message instance to merge from.
  29. ignore_unknown_fields: Whether to ignore unknown fields during the merge.
  30. """
  31. target._merge_from(source, ignore_unknown_fields)