Skip to content

Commit 8d36867

Browse files
author
Rares Polenciuc
committed
refactor: replace dataclasses.replace with instance factories
1 parent 245dd3c commit 8d36867

File tree

2 files changed

+653
-0
lines changed

2 files changed

+653
-0
lines changed

src/aws_durable_execution_sdk_python/lambda_service.py

Lines changed: 350 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,356 @@ class Operation:
708708
callback_details: CallbackDetails | None = None
709709
chained_invoke_details: ChainedInvokeDetails | None = None
710710

711+
def create_succeeded(
712+
self, end_timestamp: datetime.datetime | None = None
713+
) -> Operation:
714+
"""Create a succeeded operation with end timestamp."""
715+
return Operation(
716+
operation_id=self.operation_id,
717+
operation_type=self.operation_type,
718+
status=OperationStatus.SUCCEEDED,
719+
parent_id=self.parent_id,
720+
name=self.name,
721+
start_timestamp=self.start_timestamp,
722+
end_timestamp=end_timestamp,
723+
sub_type=self.sub_type,
724+
execution_details=self.execution_details,
725+
context_details=self.context_details,
726+
step_details=self.step_details,
727+
wait_details=self.wait_details,
728+
callback_details=self.callback_details,
729+
chained_invoke_details=self.chained_invoke_details,
730+
)
731+
732+
def create_failed(
733+
self, end_timestamp: datetime.datetime | None = None
734+
) -> Operation:
735+
"""Create a failed operation with end timestamp."""
736+
return Operation(
737+
operation_id=self.operation_id,
738+
operation_type=self.operation_type,
739+
status=OperationStatus.FAILED,
740+
parent_id=self.parent_id,
741+
name=self.name,
742+
start_timestamp=self.start_timestamp,
743+
end_timestamp=end_timestamp,
744+
sub_type=self.sub_type,
745+
execution_details=self.execution_details,
746+
context_details=self.context_details,
747+
step_details=self.step_details,
748+
wait_details=self.wait_details,
749+
callback_details=self.callback_details,
750+
chained_invoke_details=self.chained_invoke_details,
751+
)
752+
753+
def create_ready(self) -> Operation:
754+
"""Create a ready operation."""
755+
updated_step_details = None
756+
if self.step_details:
757+
updated_step_details = StepDetails(
758+
attempt=self.step_details.attempt,
759+
next_attempt_timestamp=None,
760+
result=self.step_details.result,
761+
error=self.step_details.error,
762+
)
763+
return Operation(
764+
operation_id=self.operation_id,
765+
operation_type=self.operation_type,
766+
status=OperationStatus.READY,
767+
parent_id=self.parent_id,
768+
name=self.name,
769+
start_timestamp=self.start_timestamp,
770+
end_timestamp=self.end_timestamp,
771+
sub_type=self.sub_type,
772+
execution_details=self.execution_details,
773+
context_details=self.context_details,
774+
step_details=updated_step_details,
775+
wait_details=self.wait_details,
776+
callback_details=self.callback_details,
777+
chained_invoke_details=self.chained_invoke_details,
778+
)
779+
780+
def create_completed_retry(self) -> Operation:
781+
"""Create an operation with completed retry (clears next_attempt_timestamp)."""
782+
updated_step_details = None
783+
if self.step_details:
784+
updated_step_details = StepDetails(
785+
attempt=self.step_details.attempt,
786+
next_attempt_timestamp=None,
787+
result=self.step_details.result,
788+
error=self.step_details.error,
789+
)
790+
return Operation(
791+
operation_id=self.operation_id,
792+
operation_type=self.operation_type,
793+
status=OperationStatus.READY,
794+
parent_id=self.parent_id,
795+
name=self.name,
796+
start_timestamp=self.start_timestamp,
797+
end_timestamp=self.end_timestamp,
798+
sub_type=self.sub_type,
799+
execution_details=self.execution_details,
800+
context_details=self.context_details,
801+
step_details=updated_step_details,
802+
wait_details=self.wait_details,
803+
callback_details=self.callback_details,
804+
chained_invoke_details=self.chained_invoke_details,
805+
)
806+
807+
def create_callback_result(
808+
self, result: str | None, end_timestamp: datetime.datetime | None = None
809+
) -> Operation:
810+
"""Create a succeeded callback operation with result."""
811+
updated_callback_details = None
812+
if self.callback_details:
813+
updated_callback_details = CallbackDetails(
814+
callback_id=self.callback_details.callback_id,
815+
result=result,
816+
error=self.callback_details.error,
817+
)
818+
return Operation(
819+
operation_id=self.operation_id,
820+
operation_type=self.operation_type,
821+
status=OperationStatus.SUCCEEDED,
822+
parent_id=self.parent_id,
823+
name=self.name,
824+
start_timestamp=self.start_timestamp,
825+
end_timestamp=end_timestamp,
826+
sub_type=self.sub_type,
827+
execution_details=self.execution_details,
828+
context_details=self.context_details,
829+
step_details=self.step_details,
830+
wait_details=self.wait_details,
831+
callback_details=updated_callback_details,
832+
chained_invoke_details=self.chained_invoke_details,
833+
)
834+
835+
def create_callback_failure(
836+
self, error: ErrorObject, end_timestamp: datetime.datetime | None = None
837+
) -> Operation:
838+
"""Create a failed callback operation with error."""
839+
updated_callback_details = None
840+
if self.callback_details:
841+
updated_callback_details = CallbackDetails(
842+
callback_id=self.callback_details.callback_id,
843+
result=self.callback_details.result,
844+
error=error,
845+
)
846+
return Operation(
847+
operation_id=self.operation_id,
848+
operation_type=self.operation_type,
849+
status=OperationStatus.FAILED,
850+
parent_id=self.parent_id,
851+
name=self.name,
852+
start_timestamp=self.start_timestamp,
853+
end_timestamp=end_timestamp,
854+
sub_type=self.sub_type,
855+
execution_details=self.execution_details,
856+
context_details=self.context_details,
857+
step_details=self.step_details,
858+
wait_details=self.wait_details,
859+
callback_details=updated_callback_details,
860+
chained_invoke_details=self.chained_invoke_details,
861+
)
862+
863+
def create_execution_end(
864+
self, status: OperationStatus, end_timestamp: datetime.datetime | None = None
865+
) -> Operation:
866+
"""Create an ended execution operation."""
867+
return Operation(
868+
operation_id=self.operation_id,
869+
operation_type=self.operation_type,
870+
status=status,
871+
parent_id=self.parent_id,
872+
name=self.name,
873+
start_timestamp=self.start_timestamp,
874+
end_timestamp=end_timestamp,
875+
sub_type=self.sub_type,
876+
execution_details=self.execution_details,
877+
context_details=self.context_details,
878+
step_details=self.step_details,
879+
wait_details=self.wait_details,
880+
callback_details=self.callback_details,
881+
chained_invoke_details=self.chained_invoke_details,
882+
)
883+
884+
def create_merged_from_previous(self, previous_operation: Operation) -> Operation:
885+
"""Merge current operation with previous operation, preserving previous state."""
886+
return Operation(
887+
operation_id=self.operation_id,
888+
operation_type=self.operation_type,
889+
status=self.status,
890+
parent_id=self.parent_id or previous_operation.parent_id,
891+
name=self.name or previous_operation.name,
892+
start_timestamp=previous_operation.start_timestamp,
893+
end_timestamp=previous_operation.end_timestamp,
894+
sub_type=self.sub_type or previous_operation.sub_type,
895+
execution_details=previous_operation.execution_details,
896+
context_details=previous_operation.context_details,
897+
step_details=previous_operation.step_details,
898+
wait_details=previous_operation.wait_details,
899+
callback_details=previous_operation.callback_details,
900+
chained_invoke_details=previous_operation.chained_invoke_details,
901+
)
902+
903+
def create_with_start_timestamp(self, timestamp: datetime.datetime) -> Operation:
904+
"""Create operation with updated start timestamp."""
905+
return Operation(
906+
operation_id=self.operation_id,
907+
operation_type=self.operation_type,
908+
status=self.status,
909+
parent_id=self.parent_id,
910+
name=self.name,
911+
start_timestamp=timestamp,
912+
end_timestamp=self.end_timestamp,
913+
sub_type=self.sub_type,
914+
execution_details=self.execution_details,
915+
context_details=self.context_details,
916+
step_details=self.step_details,
917+
wait_details=self.wait_details,
918+
callback_details=self.callback_details,
919+
chained_invoke_details=self.chained_invoke_details,
920+
)
921+
922+
def create_with_end_timestamp(self, timestamp: datetime.datetime) -> Operation:
923+
"""Create operation with updated end timestamp."""
924+
return Operation(
925+
operation_id=self.operation_id,
926+
operation_type=self.operation_type,
927+
status=self.status,
928+
parent_id=self.parent_id,
929+
name=self.name,
930+
start_timestamp=self.start_timestamp,
931+
end_timestamp=timestamp,
932+
sub_type=self.sub_type,
933+
execution_details=self.execution_details,
934+
context_details=self.context_details,
935+
step_details=self.step_details,
936+
wait_details=self.wait_details,
937+
callback_details=self.callback_details,
938+
chained_invoke_details=self.chained_invoke_details,
939+
)
940+
941+
def create_with_execution_details(
942+
self, execution_details: ExecutionDetails
943+
) -> Operation:
944+
"""Create operation with execution details."""
945+
return Operation(
946+
operation_id=self.operation_id,
947+
operation_type=self.operation_type,
948+
status=self.status,
949+
parent_id=self.parent_id,
950+
name=self.name,
951+
start_timestamp=self.start_timestamp,
952+
end_timestamp=self.end_timestamp,
953+
sub_type=self.sub_type,
954+
execution_details=execution_details,
955+
context_details=self.context_details,
956+
step_details=self.step_details,
957+
wait_details=self.wait_details,
958+
callback_details=self.callback_details,
959+
chained_invoke_details=self.chained_invoke_details,
960+
)
961+
962+
def create_with_callback_details(
963+
self, callback_details: CallbackDetails
964+
) -> Operation:
965+
"""Create operation with callback details."""
966+
return Operation(
967+
operation_id=self.operation_id,
968+
operation_type=self.operation_type,
969+
status=self.status,
970+
parent_id=self.parent_id,
971+
name=self.name,
972+
start_timestamp=self.start_timestamp,
973+
end_timestamp=self.end_timestamp,
974+
sub_type=self.sub_type,
975+
execution_details=self.execution_details,
976+
context_details=self.context_details,
977+
step_details=self.step_details,
978+
wait_details=self.wait_details,
979+
callback_details=callback_details,
980+
chained_invoke_details=self.chained_invoke_details,
981+
)
982+
983+
def create_with_step_details(self, step_details: StepDetails) -> Operation:
984+
"""Create operation with step details."""
985+
return Operation(
986+
operation_id=self.operation_id,
987+
operation_type=self.operation_type,
988+
status=self.status,
989+
parent_id=self.parent_id,
990+
name=self.name,
991+
start_timestamp=self.start_timestamp,
992+
end_timestamp=self.end_timestamp,
993+
sub_type=self.sub_type,
994+
execution_details=self.execution_details,
995+
context_details=self.context_details,
996+
step_details=step_details,
997+
wait_details=self.wait_details,
998+
callback_details=self.callback_details,
999+
chained_invoke_details=self.chained_invoke_details,
1000+
)
1001+
1002+
def create_with_wait_details(self, wait_details: WaitDetails) -> Operation:
1003+
"""Create operation with wait details."""
1004+
return Operation(
1005+
operation_id=self.operation_id,
1006+
operation_type=self.operation_type,
1007+
status=self.status,
1008+
parent_id=self.parent_id,
1009+
name=self.name,
1010+
start_timestamp=self.start_timestamp,
1011+
end_timestamp=self.end_timestamp,
1012+
sub_type=self.sub_type,
1013+
execution_details=self.execution_details,
1014+
context_details=self.context_details,
1015+
step_details=self.step_details,
1016+
wait_details=wait_details,
1017+
callback_details=self.callback_details,
1018+
chained_invoke_details=self.chained_invoke_details,
1019+
)
1020+
1021+
def create_with_context_details(self, context_details: ContextDetails) -> Operation:
1022+
"""Create operation with context details."""
1023+
return Operation(
1024+
operation_id=self.operation_id,
1025+
operation_type=self.operation_type,
1026+
status=self.status,
1027+
parent_id=self.parent_id,
1028+
name=self.name,
1029+
start_timestamp=self.start_timestamp,
1030+
end_timestamp=self.end_timestamp,
1031+
sub_type=self.sub_type,
1032+
execution_details=self.execution_details,
1033+
context_details=context_details,
1034+
step_details=self.step_details,
1035+
wait_details=self.wait_details,
1036+
callback_details=self.callback_details,
1037+
chained_invoke_details=self.chained_invoke_details,
1038+
)
1039+
1040+
def create_with_chained_invoke_details(
1041+
self, chained_invoke_details: ChainedInvokeDetails
1042+
) -> Operation:
1043+
"""Create operation with chained invoke details."""
1044+
return Operation(
1045+
operation_id=self.operation_id,
1046+
operation_type=self.operation_type,
1047+
status=self.status,
1048+
parent_id=self.parent_id,
1049+
name=self.name,
1050+
start_timestamp=self.start_timestamp,
1051+
end_timestamp=self.end_timestamp,
1052+
sub_type=self.sub_type,
1053+
execution_details=self.execution_details,
1054+
context_details=self.context_details,
1055+
step_details=self.step_details,
1056+
wait_details=self.wait_details,
1057+
callback_details=self.callback_details,
1058+
chained_invoke_details=chained_invoke_details,
1059+
)
1060+
7111061
@classmethod
7121062
def from_dict(cls, data: MutableMapping[str, Any]) -> Operation:
7131063
"""Create an Operation instance from a dictionary with the original Smithy model field names.

0 commit comments

Comments
 (0)