xref: /aosp_15_r20/tools/netsim/testing/netsim-grpc/tests/utils.py (revision cf78ab8cffb8fc9207af348f23af247fb04370a6)
1# Copyright 2020 The Android Open Source Project
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.
14import google.protobuf.text_format
15
16
17def fmt_proto(msg: google.protobuf.Message) -> str:
18  """Formats a `google.protobuf.Message` object as a string.
19
20  Parameters:
21      msg: A `google.protobuf.Message` object.
22
23  Returns:
24      A string representation of the `msg` object, with each line on a separate
25      line.
26
27  Raises:
28      TypeError: If `msg` is not a `google.protobuf.Message` object.
29
30  Example:
31
32      >>> from google.protobuf import message
33      >>> msg = message.Message()
34      >>> msg.set_field1("value1")
35      >>> msg.set_field2(123)
36      >>> fmt_proto(msg)
37      'field1: value1\nfield2: 123'
38  """
39  return google.protobuf.text_format.MessageToString(msg, as_one_line=True)
40