Skip to content

Commit f46b28f

Browse files
authored
Merge pull request #5856 from opsmill/dga-202502060-core-schema
Split core schema into multiple files
2 parents fda08da + c8e2c1c commit f46b28f

File tree

6 files changed

+360
-307
lines changed

6 files changed

+360
-307
lines changed

backend/infrahub/core/schema/attribute_schema.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import annotations
22

33
import enum
4+
from enum import Enum
45
from typing import TYPE_CHECKING, Any, Optional, Union
56

67
from pydantic import field_validator, model_validator
@@ -36,6 +37,13 @@ def is_relationship(self) -> bool:
3637
def is_deprecated(self) -> bool:
3738
return bool(self.deprecation)
3839

40+
def to_dict(self) -> dict:
41+
data = self.model_dump(exclude_unset=True, exclude_none=True, exclude_defaults=True)
42+
for field_name, value in data.items():
43+
if isinstance(value, Enum):
44+
data[field_name] = value.value
45+
return data
46+
3947
@field_validator("kind")
4048
@classmethod
4149
def kind_options(cls, v: str) -> str:

backend/infrahub/core/schema/basenode_schema.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import keyword
55
import os
66
from dataclasses import asdict, dataclass
7+
from enum import Enum
78
from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, Optional, Union, overload
89

910
from infrahub_sdk.utils import compare_lists, intersection
@@ -62,6 +63,17 @@ def __hash__(self) -> int:
6263
Be careful hash generated from hash() have a salt by default and they will not be the same across run"""
6364
return hash(self.get_hash())
6465

66+
def to_dict(self) -> dict:
67+
data = self.model_dump(
68+
exclude_unset=True, exclude_none=True, exclude_defaults=True, exclude={"attributes", "relationships"}
69+
)
70+
for field_name, value in data.items():
71+
if isinstance(value, Enum):
72+
data[field_name] = value.value
73+
data["attributes"] = [attr.to_dict() for attr in self.attributes]
74+
data["relationships"] = [rel.to_dict() for rel in self.relationships]
75+
return data
76+
6577
def get_hash(self, display_values: bool = False) -> str:
6678
"""Extend the Hash Calculation to account for attributes and relationships."""
6779

0 commit comments

Comments
 (0)