Exemplo n.º 1
0
 def _decode_object(d):
     topic = d['__class__']['topic']
     state = d['__class__']['state']
     obj_class = resolve_domain_topic(topic)
     obj = object.__new__(obj_class)
     obj.__dict__.update(state)
     return obj
Exemplo n.º 2
0
 def _decode_object(d):
     topic = d['__class__']['topic']
     state = d['__class__']['state']
     obj_class = resolve_domain_topic(topic)
     obj = object.__new__(obj_class)
     obj.__dict__.update(state)
     return obj
Exemplo n.º 3
0
def entity_from_snapshot(snapshot):
    """
    Reconstructs domain entity from given snapshot.
    """
    assert isinstance(snapshot, AbstractSnapshop), type(snapshot)
    if snapshot.state is not None:
        entity_class = resolve_domain_topic(snapshot.topic)
        return reconstruct_object(entity_class, snapshot.state)
Exemplo n.º 4
0
 def test_topic_resolution_error(self):
     # Check topic resolution error is raised, if the module path is
     # broken, and if the class name is broken.
     resolve_domain_topic('eventsourcing.domain.model.events#DomainEvent')
     with self.assertRaises(TopicResolutionError):
         resolve_domain_topic('eventsourcing.domain.model.broken#DomainEvent')
     with self.assertRaises(TopicResolutionError):
         resolve_domain_topic('eventsourcing.domain.model.events#Broken')
Exemplo n.º 5
0
 def test_topic_resolution_error(self):
     # Check topic resolution error is raised, if the module path is
     # broken, and if the class name is broken.
     resolve_domain_topic('eventsourcing.domain.model.events#DomainEvent')
     with self.assertRaises(TopicResolutionError):
         resolve_domain_topic(
             'eventsourcing.domain.model.broken#DomainEvent')
     with self.assertRaises(TopicResolutionError):
         resolve_domain_topic('eventsourcing.domain.model.events#Broken')
Exemplo n.º 6
0
def deserialize_domain_event(stored_event,
                             json_decoder_cls=None,
                             without_json=False,
                             with_uuid1=False,
                             cipher=None,
                             always_encrypt=False):
    """
    Recreates original domain event from stored event topic and event attrs.
    """
    assert isinstance(stored_event, StoredEvent)

    # Get the domain event class from the topic.
    event_class = resolve_domain_topic(stored_event.event_topic)

    if not isinstance(event_class, type):
        raise ValueError("Event class is not a type: {}".format(event_class))

    if not issubclass(event_class, DomainEvent):
        raise ValueError(
            "Event class is not a DomainEvent: {}".format(event_class))

    # Deserialize event attributes from JSON, optionally decrypted with cipher.
    event_attrs = stored_event.event_attrs
    if not without_json:

        if json_decoder_cls is None:
            json_decoder_cls = ObjectJSONDecoder

        if always_encrypt or event_class.always_encrypt:
            if cipher is None:
                raise ValueError("Can't decrypt stored event without a cipher")
            event_attrs = cipher.decrypt(event_attrs)

        event_attrs = json.loads(event_attrs, cls=json_decoder_cls)

    # Set the domain event ID.
    if with_uuid1:
        event_attrs['domain_event_id'] = stored_event.event_id

    # Reinstantiate and return the domain event object.
    try:
        domain_event = object.__new__(event_class)
        domain_event.__dict__.update(event_attrs)
    except TypeError:
        raise ValueError("Unable to instantiate class '{}' with data '{}'"
                         "".format(stored_event.event_topic, event_attrs))

    return domain_event
Exemplo n.º 7
0
def deserialize_domain_entity(entity_topic, entity_attrs):
    """
    Return a new domain entity object from a given topic (a string) and attributes (a dict).
    """

    # Get the domain entity class from the entity topic.
    domain_class = resolve_domain_topic(entity_topic)

    # Instantiate the domain entity class.
    entity = object.__new__(domain_class)

    # Set the attributes.
    entity.__dict__.update(entity_attrs)

    # Return a new domain entity object.
    return entity
Exemplo n.º 8
0
def deserialize_domain_entity(entity_topic, entity_attrs):
    """
    Return a new domain entity object from a given topic (a string) and attributes (a dict).
    """

    # Get the domain entity class from the entity topic.
    domain_class = resolve_domain_topic(entity_topic)

    # Instantiate the domain entity class.
    entity = object.__new__(domain_class)

    # Set the attributes.
    entity.__dict__.update(entity_attrs)

    # Return a new domain entity object.
    return entity
Exemplo n.º 9
0
    def from_sequenced_item(self, sequenced_item):
        """
        Reconstructs domain event from stored event topic and
        event attrs. Used in the event store when getting domain events.
        """
        assert isinstance(sequenced_item, self.sequenced_item_class), (self.sequenced_item_class, type(sequenced_item))

        # Get the domain event class from the topic.
        topic = getattr(sequenced_item, self.field_names.topic)
        domain_event_class = resolve_domain_topic(topic)

        # Deserialize, optionally with decryption.
        is_encrypted = self.is_encrypted(domain_event_class)
        event_attrs = self.deserialize_event_attrs(getattr(sequenced_item, self.field_names.data), is_encrypted)

        # Reconstruct the domain event object.
        return reconstruct_object(domain_event_class, event_attrs)
Exemplo n.º 10
0
def deserialize_domain_event(stored_event, json_decoder_cls=None, without_json=False, with_uuid1=False, cipher=None,
                             always_encrypt=False):
    """
    Recreates original domain event from stored event topic and event attrs.
    """
    assert isinstance(stored_event, StoredEvent)

    # Get the domain event class from the topic.
    event_class = resolve_domain_topic(stored_event.event_topic)

    if not isinstance(event_class, type):
        raise ValueError("Event class is not a type: {}".format(event_class))

    if not issubclass(event_class, DomainEvent):
        raise ValueError("Event class is not a DomainEvent: {}".format(event_class))

    # Deserialize event attributes from JSON, optionally decrypted with cipher.
    event_attrs = stored_event.event_attrs
    if not without_json:

        if json_decoder_cls is None:
            json_decoder_cls = ObjectJSONDecoder

        if always_encrypt or event_class.always_encrypt:
            if cipher is None:
                raise ValueError("Can't decrypt stored event without a cipher")
            event_attrs = cipher.decrypt(event_attrs)

        event_attrs = json.loads(event_attrs, cls=json_decoder_cls)

    # Set the domain event ID.
    if with_uuid1:
        event_attrs['domain_event_id'] = stored_event.event_id

    # Reinstantiate and return the domain event object.
    try:
        domain_event = object.__new__(event_class)
        domain_event.__dict__.update(event_attrs)
    except TypeError:
        raise ValueError("Unable to instantiate class '{}' with data '{}'"
                         "".format(stored_event.event_topic, event_attrs))

    return domain_event
Exemplo n.º 11
0
 def class_from_topic(self, topic):
     return resolve_domain_topic(topic)