예제 #1
0
  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})
예제 #2
0
  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",
    })
예제 #3
0
  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
예제 #4
0
  def testTransform(self):
    i32 = wrappers_pb2.Int32Value()
    i32.value = 1337

    dct = utils.MessageToFlatDict(i32, lambda _, value: value * 2)
    self.assertEqual(dct, {"value": 1337 * 2})
예제 #5
0
  def testSingleField(self):
    string = wrappers_pb2.StringValue()
    string.value = "foo"

    dct = utils.MessageToFlatDict(string, lambda _, value: value)
    self.assertEqual(dct, {"value": "foo"})
예제 #6
0
 def testEmpty(self):
   dct = utils.MessageToFlatDict(empty_pb2.Empty(), lambda _, value: value)
   self.assertEmpty(dct)