Exemple #1
0
    def test_from_pb_w_metadata_and_kwargs(self):
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any
        from google.protobuf.struct_pb2 import Struct, Value
        from google.cloud import operation as MUT
        from unit_tests._testing import _Monkey
        TYPE_URI = 'type.googleapis.com/%s' % (Struct.DESCRIPTOR.full_name,)
        type_url_map = {TYPE_URI: Struct}

        client = _Client()
        meta = Struct(fields={'foo': Value(string_value=u'Bar')})
        metadata_pb = Any(type_url=TYPE_URI, value=meta.SerializeToString())
        operation_pb = operations_pb2.Operation(
            name=self.OPERATION_NAME, metadata=metadata_pb)
        klass = self._getTargetClass()

        with _Monkey(MUT, _TYPE_URL_MAP=type_url_map):
            operation = klass.from_pb(operation_pb, client, baz='qux')

        self.assertEqual(operation.name, self.OPERATION_NAME)
        self.assertTrue(operation.client is client)
        pb_metadata = operation.pb_metadata
        self.assertTrue(isinstance(pb_metadata, Struct))
        self.assertEqual(list(pb_metadata.fields), ['foo'])
        self.assertEqual(pb_metadata.fields['foo'].string_value, 'Bar')
        self.assertEqual(operation.metadata, {'baz': 'qux'})
    def test_registered_type(self):
        from google.protobuf import any_pb2
        from google.protobuf import descriptor_pool
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value

        pool = descriptor_pool.Default()
        type_name = "google.protobuf.Struct"
        # Make sure the descriptor is known in the registry.
        descriptor = pool.FindMessageTypeByName(type_name)
        self.assertEqual(descriptor.name, "Struct")

        type_url = "type.googleapis.com/" + type_name
        field_name = "foo"
        field_value = "Bar"
        struct_pb = Struct(
            fields={field_name: Value(string_value=field_value)})
        any_pb = any_pb2.Any(type_url=type_url,
                             value=struct_pb.SerializeToString())

        entry_pb = LogEntryPB(proto_payload=any_pb, log_name="all-good")
        result = self._call_fut(LogEntryPB.pb(entry_pb))
        expected_proto = {
            "logName": entry_pb.log_name,
            "protoPayload": {
                "@type": type_url,
                "value": {
                    field_name: field_value
                }
            },
        }
        self.assertEqual(result, expected_proto)
Exemple #3
0
    def test_from_pb_w_metadata_and_kwargs(self):
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any
        from google.protobuf.struct_pb2 import Struct
        from google.protobuf.struct_pb2 import Value
        from google.cloud import operation as MUT
        from google.cloud._testing import _Monkey

        type_url = 'type.googleapis.com/%s' % (Struct.DESCRIPTOR.full_name, )
        type_url_map = {type_url: Struct}

        client = _Client()
        meta = Struct(fields={'foo': Value(string_value=u'Bar')})
        metadata_pb = Any(type_url=type_url, value=meta.SerializeToString())
        operation_pb = operations_pb2.Operation(name=self.OPERATION_NAME,
                                                metadata=metadata_pb)
        klass = self._get_target_class()

        with _Monkey(MUT, _TYPE_URL_MAP=type_url_map):
            operation = klass.from_pb(operation_pb, client, baz='qux')

        self.assertEqual(operation.name, self.OPERATION_NAME)
        self.assertIs(operation.client, client)
        self.assertEqual(operation.metadata, meta)
        self.assertEqual(operation.caller_metadata, {'baz': 'qux'})
Exemple #4
0
def write_dict_to_proto(data: Dict[str, Any], path: str) -> str:
    from google.protobuf.struct_pb2 import Struct

    s = Struct()
    s.update(data)
    with open(f"{path}", "wb") as f:
        f.write(s.SerializeToString())
    return f"{path}"
Exemple #5
0
 def encode(cls, dictionary: Dict[str, Any]) -> bytes:
     """Serialize compatible dictionary to bytes"""
     if not isinstance(dictionary, dict):
         raise TypeError(  # pragma: nocover
             "dictionary must be of dict type, got type {}".format(type(dictionary))
         )
     # TOFIX(LR) problematic as it'll copy every message
     patched_dict = copy.deepcopy(dictionary)
     cls._patch_dict(patched_dict)
     pstruct = Struct()
     pstruct.update(patched_dict)  # pylint: disable=no-member
     return pstruct.SerializeToString()
    def test_from_pb_w_unknown_metadata(self):
        from google.longrunning import operations_pb2
        from google.protobuf.any_pb2 import Any
        from google.protobuf.struct_pb2 import Struct, Value
        TYPE_URI = 'type.googleapis.com/%s' % (Struct.DESCRIPTOR.full_name, )

        client = _Client()
        meta = Struct(fields={'foo': Value(string_value=u'Bar')})
        metadata_pb = Any(type_url=TYPE_URI, value=meta.SerializeToString())
        operation_pb = operations_pb2.Operation(name=self.OPERATION_NAME,
                                                metadata=metadata_pb)
        klass = self._getTargetClass()

        operation = klass.from_pb(operation_pb, client)

        self.assertEqual(operation.name, self.OPERATION_NAME)
        self.assertIs(operation.client, client)
        self.assertIsNone(operation.pb_metadata)
        self.assertEqual(operation.metadata, {})
Exemple #7
0
    def encode(msg: Message) -> bytes:
        """
        Encode a message into bytes using Protobuf.

        - if one of message_id, target and dialogue_reference are not defined,
          serialize only the message body/
        - otherwise, extract those fields from the body and instantiate
          a Message struct.
        """
        message_pb = ProtobufMessage()
        if msg.has_dialogue_info:
            dialogue_message_pb = Pb2DialogueMessage()
            dialogue_message_pb.message_id = msg.message_id
            dialogue_message_pb.dialogue_starter_reference = msg.dialogue_reference[0]
            dialogue_message_pb.dialogue_responder_reference = msg.dialogue_reference[1]
            dialogue_message_pb.target = msg.target

            new_body = copy(msg._body)  # pylint: disable=protected-access
            new_body.pop("message_id")
            new_body.pop("dialogue_reference")
            new_body.pop("target")

            body_json = Struct()
            body_json.update(new_body)  # pylint: disable=no-member

            dialogue_message_pb.content = (  # pylint: disable=no-member
                body_json.SerializeToString()
            )
            message_pb.dialogue_message.CopyFrom(  # pylint: disable=no-member
                dialogue_message_pb
            )
        else:
            body_json = Struct()
            body_json.update(msg._body)  # pylint: disable=no-member,protected-access
            message_pb.body.CopyFrom(body_json)  # pylint: disable=no-member

        return message_pb.SerializeToString()
Exemple #8
0
 def encode(msg: Message) -> bytes:
     """Encode a message into bytes using Protobuf."""
     body_json = Struct()
     body_json.update(msg.body)  # pylint: disable=no-member
     body_bytes = body_json.SerializeToString()
     return body_bytes
Exemple #9
0
# import json
# from google.protobuf.json_format import Parse
from a import A_EX_DICT

# message = Parse(json.dumps(A_EX_DICT), Thing())

from google.protobuf.struct_pb2 import Struct

s = Struct()
s.update(A_EX_DICT)
with open("./a.proto", "wb") as f:
    f.write(s.SerializeToString())
Exemple #10
0
 def encode(self, msg: Message) -> bytes:
     """Encode a message into bytes using Protobuf."""
     body_json = Struct()
     body_json.update(msg.body)
     body_bytes = body_json.SerializeToString()
     return body_bytes