Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 119 additions & 1 deletion src/rune/runtime/base_data_class.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
'''Base class for all Rune type classes'''
import contextvars
import copy
import importlib
import json
Expand Down Expand Up @@ -47,9 +48,27 @@ def _annotation_contains(annotation: Any, base_types: tuple[type, ...], *, unkno
if isinstance(annotation, type) and issubclass(annotation, base_types):
return True
return any(_annotation_contains(arg, base_types, unknown=unknown) for arg in get_args(annotation))
_CONSTRUCTING = contextvars.ContextVar('rune_constructing', default=False)


class BaseDataClass(BaseModel, ComplexTypeMetaDataMixin):
class _RuneModelMeta(type(BaseModel)):
'''Records that an object is being built directly in Python.

The validator of a choice can't otherwise tell `ChoiceBasic(string='x')`
from the JSON `{"string": "x"}`. This has to be done here: defining
`__init__` on the model would make pydantic build nested models through
it, so that JSON would look like direct construction as well.
'''
def __call__(cls, *args, **kwargs):
token = _CONSTRUCTING.set(True)
try:
return super().__call__(*args, **kwargs)
finally:
_CONSTRUCTING.reset(token)


class BaseDataClass(BaseModel, ComplexTypeMetaDataMixin,
metaclass=_RuneModelMeta):
''' A base class for all cdm generated classes. It is derived from
`pydantic.BaseModel` which provides type checking at object creation
for all cdm classes. It provides as well the `validate_model`,
Expand Down Expand Up @@ -208,6 +227,8 @@ def _serialize_refs(self, serializer, info):
root_metadata = self.__dict__.get(ROOT_CONTAINER)
if not refs and not reference_fields:
result = serializer(self)
if self._is_rune_choice():
result = self._flatten_choice(result)
return root_metadata | result if root_metadata else result
updates = {}
list_fields = {}
Expand Down Expand Up @@ -263,6 +284,8 @@ def _serialize_refs(self, serializer, info):
key, key_type = reference
written[position] = {key_type.rune_ref_tag: key}
res[output_name] = written
if self._is_rune_choice():
res = self._flatten_choice(res)
return root_metadata | res if root_metadata else res

@staticmethod
Expand All @@ -284,13 +307,108 @@ def _serialized_field_name(self, name: str, info) -> str:
field = type(self).model_fields[name]
return (field.serialization_alias or name) if by_alias else name

@classmethod
def _is_rune_choice(cls) -> bool:
'''True for a class generated from a Rune `choice`.'''
return getattr(cls, '_RUNE_CHOICE', False)

@classmethod
def _option_class(cls, name: str):
'''The data class declared for a choice option. None if the option
is a basic type or an enum.'''
def find(tp):
if isinstance(tp, type):
return tp if issubclass(tp, BaseDataClass) else None
for arg in get_args(tp):
if found := find(arg):
return found
return None
return find(cls.model_fields[name].annotation)

def _flatten_choice(self, res: dict) -> dict:
'''Rewrites a serialized choice in the Rune JSON format: `@type` names
the selected option and its fields sit directly on the choice.'''
selected = [name for name in type(self).model_fields
if self.__dict__.get(name) is not None]
if not selected:
return {}
if len(selected) > 1:
raise ValueError(f'Multiple choice options selected for '
f'{self._fqrtn()}: {selected}')
name = selected[0]
value = self.__dict__[name]
option = res[name]
declared = self._option_class(name)
if (isinstance(value, BaseDataClass) and declared is not None
and type(value) is not declared):
raise ValueError(f'Choice option {name} of {self._fqrtn()} holds '
f'{type(value)}, but must be exactly {declared}')
option_cls = type(value) if isinstance(value, BaseDataClass) else declared
if option_cls is None: # basic type or enum
if isinstance(option, dict) and '@data' in option:
return {'@type': name} | option
return {'@type': name, '@data': option}
# pylint: disable=protected-access
if option_cls._is_rune_choice():
# A nested choice is already flat, unless the option has metadata
# or is a reference
if '@data' in option or any(k.startswith('@ref') for k in option):
return {'@type': option_cls._fqrtn()} | option
return option
return {'@type': option_cls._fqrtn()} | {
k: v for k, v in option.items() if k != '@type'}

@classmethod
def _choice_has_option(cls, rune_type: str) -> bool:
'''True if `rune_type` is an option of this choice, or of a choice
nested in it.'''
# pylint: disable=protected-access
for name in cls.model_fields:
option = cls._option_class(name)
if option is None:
if name == rune_type:
return True
elif option._fqrtn() == rune_type or (
option._is_rune_choice() and option._choice_has_option(rune_type)):
return True
return False

@classmethod
def _choice_to_fields(cls, data: dict) -> dict:
'''Converts a choice in the Rune JSON format to this class's fields,
with the option selected by `@type`.'''
if '@type' not in data:
if any(k.startswith('@ref') for k in data):
return data # a reference to a choice
if '@data' in data:
metadata = {k: v for k, v in data.items() if k != '@data'}
return metadata | cls._choice_to_fields(data['@data'])
raise ValueError(f"Missing required '@type' for choice "
f'{cls._fqrtn()}')
rune_type = data['@type']
body = {k: v for k, v in data.items() if k != '@type'}
# pylint: disable=protected-access
for name in cls.model_fields:
option = cls._option_class(name)
if option is None:
if name == rune_type:
return {name: body['@data']}
elif option._fqrtn() == rune_type:
return {name: body}
elif option._is_rune_choice() and option._choice_has_option(rune_type):
return {name: data}
raise ValueError(f"'{rune_type}' is not an option of choice "
f'{cls._fqrtn()}')

@model_validator(mode='wrap')
@classmethod
def _deserialize_refs(cls, data: Any,
handler: ModelWrapValidatorHandler[Self]) -> Self:
'''should resolve refs after creation'''
metadata = {}
if isinstance(data, dict):
if cls._is_rune_choice() and not _CONSTRUCTING.get():
data = cls._choice_to_fields(data)
metadata = {k: v for k, v in data.items() if k.startswith('@')}
if aux := cls._create_unresolved_ref(metadata):
return aux # type: ignore
Expand Down
27 changes: 24 additions & 3 deletions src/rune/runtime/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,11 @@ def serialise(cls, obj) -> dict | list:
if isinstance(obj, list):
return [None if item is None else cls.serialise(item) for item in obj]
res = obj.serialise_meta()
res |= obj.model_dump(exclude_unset=True, exclude_defaults=True)
dumped = obj.model_dump(exclude_unset=True, exclude_defaults=True)
if obj._is_rune_choice(): # pylint: disable=protected-access
# Metadata of the attribute sits beside the flattened choice
return res | {'@data': dumped} if res else dumped
res |= dumped
if cls != obj.__class__:
# pylint: disable=protected-access
# res = {'@type': obj._FQRTN} | res
Expand Down Expand Up @@ -511,10 +515,27 @@ def deserialize(cls, obj, allowed_meta: set[str]):
for k, v in obj.items():
(metadata if k.startswith('@') else fields)[k] = v

# References deserialization treatment
if aux := cls._create_unresolved_ref(metadata):
# pylint: disable=protected-access
# References deserialization treatment. A reference beside `@type`
# belongs to the selected choice option, not to this attribute.
is_option_of_choice = cls._is_rune_choice() and '@type' in metadata
if not is_option_of_choice and (aux := cls._create_unresolved_ref(metadata)):
return aux

if cls._is_rune_choice():
if '@data' in metadata and '@type' not in metadata:
# Metadata of the attribute, beside the flattened choice
model = cls.model_validate(metadata.pop('@data'))
else:
# `@type` selects the option, and any metadata is the option's
model = cls.model_validate(obj)
metadata = {}
model.__dict__[META_CONTAINER] = metadata
if cls.meta_checks_enabled():
model._init_meta(allowed_meta)
model._register_keys(metadata)
return model

# Model creation
rune_cls = cls._type_to_cls(metadata)
if rune_cls != cls and not issubclass(rune_cls, cls):
Expand Down
Loading
Loading