Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

property is added to the schema through count references transformation #79

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def add_content_properties(

for cls_instruction in cls_instructions:
try:
target_object = resolve_schema_object_path(
target_schema = resolve_schema_object_path(
content_schema, cls_instruction.target_content.object_path
)
except KeyError as e:
Expand All @@ -58,12 +58,12 @@ def add_content_properties(
):
raise EvitableTransformationError()

target_object.setdefault("properties", {})[
target_schema.setdefault("properties", {})[
cls_instruction.target_content.property_name
] = deepcopy(cls_instruction.content_schema)

if cls_instruction.required:
target_object.setdefault("required", []).append(
target_schema.setdefault("required", []).append(
cls_instruction.target_content.property_name
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,8 @@ def assert_object_path_exists(
+ f" in class {class_name}."
) from exc

# Check if the propert_name already exists in the model
if instruction.target_content.property_name not in target_schema.get(
"properties", {}
):
# Check if the propert_name already exists in the model and raise an error if so
if instruction.target_content.property_name in target_schema.get("properties", {}):
raise ModelAssumptionError(
f"Property {
instruction.target_content.property_name} does not exist"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"""Model transformation logic for the 'count references' transformation"""

from schemapack.spec.schemapack import (
ClassDefinition,
SchemaPack,
)

Expand All @@ -26,16 +27,18 @@
)
from metldata.transform.base import EvitableTransformationError

DEFAULT_PROPERTY_SCHEMA = {"type": "integer"}


def add_count_references(
*,
model: SchemaPack,
instructions_by_class: dict[str, list[AddReferenceCountPropertyInstruction]],
) -> SchemaPack:
"""The content properties are added to the model with the 'add_content_properties
step of the workflow. Thus, this function applies no transformation.
It only checks for EvitableTransformationError.
"""The target content - object_names are added to the model with the 'add_content_properties
step of the workflow. Thus, this function only adds the property_name to a content schema.
"""
updated_class_defs: dict[str, ClassDefinition] = {}
for class_name, cls_instructions in instructions_by_class.items():
class_def = model.classes.get(class_name)

Expand All @@ -45,15 +48,24 @@ def add_count_references(
content_schema = class_def.content.json_schema_dict

for cls_instruction in cls_instructions:
object_path = cls_instruction.target_content.object_path
property_name = cls_instruction.target_content.property_name
try:
resolve_schema_object_path(
content_schema, cls_instruction.target_content.object_path
)
except KeyError as e:
raise EvitableTransformationError() from e

if cls_instruction.target_content.property_name in content_schema.get(
"properties", {}
):
target_schema = resolve_schema_object_path(content_schema, object_path)
except KeyError as exc:
raise EvitableTransformationError() from exc

if property_name in target_schema.get("properties", {}):
raise EvitableTransformationError()
return model

target_schema.setdefault("properties", {})[property_name] = (
DEFAULT_PROPERTY_SCHEMA
)

updated_class_defs[class_name] = class_def.model_validate(
{**class_def.model_dump(), "content": content_schema}
)

model_dict = model.model_dump()
model_dict["classes"].update(updated_class_defs)
return SchemaPack.model_validate(model_dict)
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,7 @@ classes:
"properties":
{
"dac_contact": { "type": "string" },
"file_summary":
{
"type": "object",
"additionalProperties": false,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
"file_summary": { "type": "object", "additionalProperties": true },
},
"type": "object",
}
Expand All @@ -46,13 +40,7 @@ classes:
"properties":
{
"description": { "type": "string" },
"file_summary":
{
"type": "object",
"additionalProperties": false,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
"file_summary": { "type": "object", "additionalProperties": true },
},
"type": "object",
}
Expand All @@ -77,12 +65,7 @@ classes:
{
"description": { "type": "string" },
"sample_summary":
{
"type": "object",
"additionalProperties": false,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
{ "type": "object", "additionalProperties": true },
},
"type": "object",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ classes:
"file_summary":
{
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
},
"type": "object",
Expand Down Expand Up @@ -49,9 +48,8 @@ classes:
"file_summary":
{
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
},
"type": "object",
Expand Down Expand Up @@ -79,9 +77,8 @@ classes:
"sample_summary":
{
"type": "object",
"additionalProperties": false,
"additionalProperties": true,
"properties": { "count": { "type": "integer" } },
"required": ["count"],
},
},
"type": "object",
Expand Down