def get_method() -> wrappers.Method: # Create the address where this method lives, and the types live, # and make them distinct. method_addr = metadata.Address(package=['foo', 'bar'], module='baz') types_addr = metadata.Address(package=['foo', 'bar'], module='bacon') # Create the method pb2 and set an overload in it. method_pb = descriptor_pb2.MethodDescriptorProto( name='DoTheThings', input_type='foo.bar.Input', output_type='foo.bar.Output', ) # Instantiate the wrapper class. return wrappers.Method( method_pb=method_pb, input=wrappers.MessageType( fields=[], message_pb=descriptor_pb2.DescriptorProto(name='Input'), meta=metadata.Metadata(address=types_addr), ), output=wrappers.MessageType( fields=[], message_pb=descriptor_pb2.DescriptorProto(name='Output'), meta=metadata.Metadata(address=types_addr), ), meta=metadata.Metadata(address=method_addr), )
def test_address_child(): addr = metadata.Address(package=['foo', 'bar'], module='baz') child = addr.child('bacon') assert child.parent == ['bacon'] assert str(child) == 'foo.bar.bacon' grandchild = child.child('ham') assert grandchild.parent == ['bacon', 'ham'] assert str(grandchild) == 'foo.bar.bacon.ham'
def __init__(self, file_descriptor: descriptor_pb2.FileDescriptorProto, file_to_generate: bool, prior_protos: Mapping[str, Proto] = None): self.messages = {} self.enums = {} self.services = {} self.file_descriptor = file_descriptor self.file_to_generate = file_to_generate self.prior_protos = prior_protos or {} # Iterate over the documentation and place it into a dictionary. # # The comments in protocol buffers are sorted by a concept called # the "path", which is a sequence of integers described in more # detail below; this code simply shifts from a list to a dict, # with tuples of paths as the dictionary keys. self.docs = {} for location in file_descriptor.source_code_info.location: self.docs[tuple(location.path)] = location # Everything has an "address", which is the proto where the thing # was declared. # # We put this together by a baton pass of sorts: everything in # this file *starts with* this address, which is appended to # for each item as it is loaded. address = metadata.Address( module=file_descriptor.name.split('/')[-1][:-len('.proto')], package=file_descriptor.package.split('.'), ) # Now iterate over the FileDescriptorProto and pull out each of # the messages, enums, and services. # # The hard-coded path keys sent here are based on how descriptor.proto # works; it uses the proto message number of the pieces of each # message (e.g. the hard-code `4` for `message_type` immediately # below is because `repeated DescriptorProto message_type = 4;` in # descriptor.proto itself). self._load_children(file_descriptor.message_type, self._load_message, address=address, path=(4, )) self._load_children(file_descriptor.enum_type, self._load_enum, address=address, path=(5, )) if file_to_generate: self._load_children(file_descriptor.service, self._load_service, address=address, path=(6, ))
def get_message() -> wrappers.MessageType: message_pb = descriptor_pb2.DescriptorProto(name='MyMessage') return wrappers.MessageType( fields=[], message_pb=message_pb, meta=metadata.Metadata( address=metadata.Address(package=['foo', 'bar'], module='baz'), documentation=descriptor_pb2.SourceCodeInfo.Location( leading_comments='Lorem ipsum dolor set amet', ), ), )
def get_message(dot_path: str) -> wrappers.MessageType: # Note: The `dot_path` here is distinct from the canonical proto path # because it includes the module, which the proto path does not. # # So, if trying to test the DescriptorProto message here, the path # would be google.protobuf.descriptor.DescriptorProto (whereas the proto # path is just google.protobuf.DescriptorProto). pieces = dot_path.split('.') pkg, module, name = pieces[:-2], pieces[-2], pieces[-1] return wrappers.MessageType( fields={}, message_pb=descriptor_pb2.DescriptorProto(name=name), meta=metadata.Metadata(address=metadata.Address( package=pkg, module=module, )), )
def test_address_str_parent(): addr = metadata.Address(package=['foo', 'bar'], module='baz', parent=['spam', 'eggs']) assert str(addr) == 'foo.bar.spam.eggs'
def test_address_str_no_parent(): addr = metadata.Address(package=['foo', 'bar'], module='baz') assert str(addr) == 'foo.bar'