'Internal helper to copy the key into a protobuf.'

def _key_to_pb(self, pb):

    key = self._key
    if (key is None):
        pairs = [(self._get_kind(), None)]
        ref = key_module._ReferenceFromPairs(pairs, reference=pb.mutable_key())
    else:
        ref = key.reference()
        pb.mutable_key().CopyFrom(ref)
    group = pb.mutable_entity_group()
    if ((key is not None) and key.id()):
        elem = ref.path().element(0)
        if (elem.id() or elem.name()):
            group.add_element().CopyFrom(elem)
