Skip to content

Commit

Permalink
[resotolib][feat] ModelExport: include name and description
Browse files Browse the repository at this point in the history
  • Loading branch information
aquamatthias committed Nov 9, 2023
1 parent 28a13a6 commit b41b4d3
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions resotolib/resotolib/core/model_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ def dataclasses_to_resotocore_model(
aggregate_root: Optional[Type[Any]] = None,
walk_subclasses: bool = True,
use_optional_as_required: bool = False,
with_description: bool = False,
) -> List[Json]:
"""
Analyze all transitive dataclasses and create the model
Expand All @@ -162,26 +163,33 @@ def dataclasses_to_resotocore_model(
:param aggregate_root: if a type is a subtype of this type, it will be considered an aggregate root.
:param walk_subclasses: if true, all subclasses of the given classes will be analyzed as well.
:param use_optional_as_required: if true, all non-optional fields will be considered required.
:param with_description: if true, include the description for classes and properties.
:return: the model definition in the resotocore json format.
"""

def prop(field: Attribute) -> List[Json]: # type: ignore
# the field itself can define the type via a type hint
# this is useful for int and float in python where the representation can not be
# detected by the type itself. Example: int32/int64 or float/double
# If not defined, we fallback to the largest container: int64 and double
# If not defined, we fall back to the largest container: int64 and double
name = field.name
meta = field.metadata.copy()
kind = meta.pop("type_hint", model_name(field.type))
desc = meta.pop("description", "")
desc = meta.pop("description", None)
desc = desc if with_description else None
required = meta.pop("required", use_optional_as_required and not is_optional(field.type)) # type: ignore
synthetic = meta.pop("synthetic", None)
synthetic = synthetic if synthetic else {}
for ps in property_metadata_to_strip:
meta.pop(ps, None)

def json(
name: str, kind_str: str, required: bool, description: str, meta: Optional[Dict[str, str]], **kwargs: Any
name: str,
kind_str: str,
required: bool,
description: Optional[str],
meta: Optional[Dict[str, str]],
**kwargs: Any,
) -> Json:
js = {"name": name, "kind": kind_str, "required": required, "description": description, **kwargs}
if meta:
Expand All @@ -193,7 +201,7 @@ def json(
synth_prop,
synth_trafo,
False,
f"Synthetic prop {synth_trafo} on {name}",
None,
None,
synthetic={"path": [name]},
)
Expand Down Expand Up @@ -236,9 +244,13 @@ def export_data_class(clazz: type) -> None:
]
root = any(sup == aggregate_root for sup in clazz.mro()) if aggregate_root else True
kind = model_name(clazz)
metadata: Optional[Json] = None
metadata: Optional[Json] = {}
if (m := getattr(clazz, "metadata", None)) and isinstance(m, dict):
metadata = m
if (s := getattr(clazz, "kind_display", None)) and isinstance(s, str):
metadata["name"] = s
if with_description and (s := getattr(clazz, "kind_description", None)) and isinstance(s, str):
metadata["description"] = s

model.append(
{
Expand All @@ -248,7 +260,7 @@ def export_data_class(clazz: type) -> None:
"allow_unknown_props": allow_unknown_props,
"successor_kinds": successors.get(kind, None),
"aggregate_root": root,
"metadata": metadata,
"metadata": metadata if metadata else None,
}
)

Expand Down

0 comments on commit b41b4d3

Please sign in to comment.