Skip to content

Commit

Permalink
Fix DifferentialResult serialization (#306)
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gu <[email protected]>
  • Loading branch information
tylergu authored Jan 15, 2024
1 parent aeca446 commit 8e03b3e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
10 changes: 3 additions & 7 deletions acto/checker/impl/consistency.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import copy
import json
import re
from typing import List, Optional, Tuple
from typing import List, Optional, Tuple, Union

from acto.acto_config import ACTO_CONFIG
from acto.checker.checker import CheckerInterface
Expand All @@ -21,11 +21,7 @@
from acto.input import InputModel
from acto.input.get_matched_schemas import find_matched_schema
from acto.k8s_util.k8sutil import canonicalize_quantity
from acto.result import (
ConsistencyOracleResult,
InvalidInputResult,
OracleResult,
)
from acto.result import ConsistencyOracleResult, InvalidInputResult
from acto.schema import ArraySchema, BaseSchema, ObjectSchema, extract_schema
from acto.serialization import ActoEncoder
from acto.snapshot import Snapshot
Expand Down Expand Up @@ -291,7 +287,7 @@ def encode_dependency(self, depender: list, dependee: list):

def check(
self, generation: int, snapshot: Snapshot, prev_snapshot: Snapshot
) -> Optional[OracleResult]:
) -> Optional[Union[ConsistencyOracleResult, InvalidInputResult]]:
"""
System state oracle
Expand Down
15 changes: 13 additions & 2 deletions acto/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import enum
import json
import os
from typing import Optional
from typing import Optional, Union

import deepdiff
import deepdiff.helper
Expand Down Expand Up @@ -111,7 +111,9 @@ class OracleResults(pydantic.BaseModel):
description="The result of the operator log oracle",
default=None,
)
consistency: Optional[OracleResult] = pydantic.Field(
consistency: Optional[
Union[ConsistencyOracleResult, InvalidInputResult]
] = pydantic.Field(
description="The result of the state consistentcy oracle",
default=None,
)
Expand All @@ -134,6 +136,15 @@ def is_error(self) -> bool:
or self.custom is not None
)

@pydantic.field_serializer("consistency")
def serialize_consistency(
self, value: Union[ConsistencyOracleResult, InvalidInputResult]
):
"""Serialize the consistency oracle result"""
if value is None:
return None
return value.model_dump()


class CliStatus(enum.Enum):
"""Status of the KubeCtl CLI"""
Expand Down
5 changes: 3 additions & 2 deletions acto/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from datetime import date, datetime

import ordered_set
import pydantic
from deepdiff import DeepDiff
from deepdiff.helper import NotPresent

Expand All @@ -25,8 +26,8 @@ def default(self, o):
"""Default encoder"""

# this section is for pydantic basemodels
if hasattr(o, "serialize"):
return o.serialize()
if isinstance(o, pydantic.BaseModel):
return o.model_dump()

# this section is from deepdiff
if isinstance(o, ordered_set.OrderedSet):
Expand Down

0 comments on commit 8e03b3e

Please sign in to comment.