def testMultipleFields(self): timestamp = timestamp_pb2.Timestamp() timestamp.seconds = 1337 timestamp.nanos = 42 dct = utils.MessageToFlatDict(timestamp, lambda _, value: value) self.assertEqual(dct, {"seconds": 1337, "nanos": 42})
def testNestedMessages(self): option = type_pb2.Option() option.name = "foo" option.value.type_url = "bar.baz.quux" option.value.value = b"quux" dct = utils.MessageToFlatDict(option, lambda _, value: value) self.assertEqual(dct, { "name": "foo", "value.type_url": "bar.baz.quux", "value.value": b"quux", })
def _ArgsToQueryParams( self, args: Optional[message.Message], exclude_names: Iterable[str], ) -> Dict[str, Union[int, str]]: if args is None: return {} result = utils.MessageToFlatDict(args, self._CoerceValueToQueryStringType) for name in exclude_names: del result[name] return result
def testTransform(self): i32 = wrappers_pb2.Int32Value() i32.value = 1337 dct = utils.MessageToFlatDict(i32, lambda _, value: value * 2) self.assertEqual(dct, {"value": 1337 * 2})
def testSingleField(self): string = wrappers_pb2.StringValue() string.value = "foo" dct = utils.MessageToFlatDict(string, lambda _, value: value) self.assertEqual(dct, {"value": "foo"})
def testEmpty(self): dct = utils.MessageToFlatDict(empty_pb2.Empty(), lambda _, value: value) self.assertEmpty(dct)