Skip to content

Commit

Permalink
Changes for Pydantic v2 (model_dump() and Config.frozen).
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbywater committed Oct 16, 2023
1 parent e34dd84 commit befbc46
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 9 deletions.
9 changes: 3 additions & 6 deletions eventsourcing/examples/aggregate7/domainmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class DomainEvent(BaseModel):
timestamp: datetime

class Config:
allow_mutation = False
frozen = True


def create_timestamp() -> datetime:
Expand All @@ -30,24 +30,21 @@ class Aggregate(BaseModel):
modified_on: datetime

class Config:
allow_mutation = False
frozen = True


class Snapshot(DomainEvent):
topic: str
state: Dict[str, Any]

class Config:
allow_mutation = False

@classmethod
def take(cls, aggregate: Aggregate) -> Snapshot:
return Snapshot(
originator_id=aggregate.id,
originator_version=aggregate.version,
timestamp=create_timestamp(),
topic=get_topic(type(aggregate)),
state=aggregate.dict(),
state=aggregate.model_dump(),
)


Expand Down
2 changes: 1 addition & 1 deletion eventsourcing/examples/aggregate7/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class PydanticMapper(Mapper):
def to_stored_event(self, domain_event: DomainEventProtocol) -> StoredEvent:
topic = get_topic(domain_event.__class__)
event_state = cast(BaseModel, domain_event).dict()
event_state = cast(BaseModel, domain_event).model_dump()
stored_state = self.transcoder.encode(event_state)
if self.compressor:
stored_state = self.compressor.compress(stored_state)
Expand Down
2 changes: 1 addition & 1 deletion eventsourcing/examples/aggregate8/domainmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class DomainEvent(BaseModel):
timestamp: datetime

class Config:
allow_mutation = False
frozen = True


class Aggregate(BaseAggregate):
Expand Down
2 changes: 1 addition & 1 deletion eventsourcing/examples/aggregate8/persistence.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class PydanticMapper(Mapper):
def to_stored_event(self, domain_event: DomainEventProtocol) -> StoredEvent:
topic = get_topic(domain_event.__class__)
event_state = cast(BaseModel, domain_event).dict()
event_state = cast(BaseModel, domain_event).model_dump()
stored_state = self.transcoder.encode(event_state)
if self.compressor:
stored_state = self.compressor.compress(stored_state)
Expand Down

0 comments on commit befbc46

Please sign in to comment.