Skip to content
Snippets Groups Projects
Commit 139f85e7 authored by Florian Spreckelsen's avatar Florian Spreckelsen
Browse files

ENH: Treat units

parent a0fe997d
Branches
Tags
2 merge requests!89ENH: JsonSchemaExporter accepts do_not_create parameter.,!80F simple schema export
Pipeline #42346 failed
......@@ -33,16 +33,20 @@ def _make_required_list(rt: db.RecordType):
if rt.get_importance(prop.name) == db.OBLIGATORY]
def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: Optional[dict]):
def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: Optional[dict], units_in_description: bool):
"""Return the JSON Schema segment for the given property
Parameters
----------
prop : db.Property
the property to be transformed
the property to be transformed
additional_options_for_text_props : Optional[dict]
dict of dicts that may contain the keys 'pattern' and 'format' to
further define the rules for the JSON Schema segment
dict of dicts that may contain the keys 'pattern' and 'format' to
further define the rules for the JSON Schema segment
units_in_description : bool
Whether to store the unit of a LinkAhead property in the description of
the corresponding json schema item or to create a separate `unit` key
instead.
"""
if prop.is_reference():
......@@ -71,6 +75,13 @@ def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: O
json_prop = {}
if prop.description:
json_prop["description"] = prop.description
if units_in_description and prop.unit:
if "description" in json_prop:
json_prop["description"] += f" Unit is {prop.unit}."
else:
json_prop["description"] = f"Unit is {prop.unit}."
elif prop.unit:
json_prop["unit"] = prop.unit
if prop.datatype == db.BOOLEAN:
json_prop["type"] = "boolean"
......@@ -83,7 +94,7 @@ def _make_prop_from_prop(prop: db.Property, additional_options_for_text_props: O
list_element_prop = db.Property(
name=prop.name, datatype=get_list_datatype(prop.datatype, strict=True))
json_prop["items"] = _make_prop_from_prop(
list_element_prop, additional_options_for_text_props)
list_element_prop, additional_options_for_text_props, units_in_description)
else:
raise ValueError(
f"Unknown or no property datatype. Property {prop.name} with type {prop.datatype}")
......@@ -111,7 +122,8 @@ def _make_text_property(description="", text_format=None, text_pattern=None):
def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = True,
name_and_description_in_properties: bool = False,
additional_options_for_text_props: Optional[dict] = None):
additional_options_for_text_props: Optional[dict] = None,
units_in_description: bool = True):
"""Create a jsonschema from a given RecordType that can be used, e.g., to
validate a json specifying a record of the given type.
......@@ -128,6 +140,11 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
additional_options_for_text_props : dict, optional
Dictionary containing additional "pattern" or "format" options for
string-typed properties. Optional, default is empty.
units_in_description : bool, optional
Whether to add the unit of a LinkAhead property (if it has any) to the
description of the corresponding schema entry. If set to false, an
additional `unit` key is added to the schema itself which is purely
annotational and ignored, e.g., in validation. Default is True.
Returns
-------
......@@ -163,7 +180,8 @@ def recordtype_to_json_schema(rt: db.RecordType, additional_properties: bool = T
"Creating a schema for multi-properties is not specified. "
f"Property {prop.name} occurs more than once."
)
props[prop.name] = _make_prop_from_prop(prop, additional_options_for_text_props)
props[prop.name] = _make_prop_from_prop(
prop, additional_options_for_text_props, units_in_description)
schema["properties"] = props
return schema
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment