Skip to content

Commit

Permalink
feat(spec): Render definition and enum columns alike
Browse files Browse the repository at this point in the history
  • Loading branch information
effigies committed Jul 25, 2024
1 parent 8fdfdb9 commit 40ca7bb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 20 deletions.
16 changes: 8 additions & 8 deletions tools/schemacode/bidsschematools/render/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ def _make_object_table(
"columns": "column",
}.get(table_type)

for element in subschema.keys():
field_name = subschema[element]["name"]
for element, field in subschema.items():
field_name = field["name"]
# NOTE: Link to the glossary entry,
# except for subobjects (if table_type) and
# "additional columns" (field_name.startswith("**"))
Expand All @@ -90,19 +90,19 @@ def _make_object_table(
"[DEPRECATED](SPEC_ROOT/common-principles.md#definitions)",
)

type_string = utils.resolve_metadata_type(subschema[element])
type_string = utils.resolve_metadata_type(field)

description = utils.normalize_requirements(
subschema[element]["description"] + " " + description_addendum
f"{field['description']} {description_addendum}".strip()
)

# Append a list of valid values, if provided, to the description.
# If there are a lot of valid values, this will add a link to the description linking to
# the associated glossary entry.
if (
"enum" in subschema[element].keys()
and len(subschema[element]["enum"]) >= n_values_to_combine
):
levels = subschema[element].get("enum", []) or subschema[element].get(
"definition", {}
).get("Levels", [])
if len(levels) >= n_values_to_combine:
glossary_entry = f"{GLOSSARY_PATH}.md#objects.{table_type}.{element}"
valid_values_str = (
f"For a list of valid values for this {element_type}, see the "
Expand Down
24 changes: 13 additions & 11 deletions tools/schemacode/bidsschematools/render/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,17 +175,19 @@ def make_glossary(schema, src_path=None):
elif obj["type"] == "format":
text += f"**Regular expression**: `{obj_def['pattern']}`\n\n"

keys_to_drop = ["description", "display_name", "name", "value", "pattern"]
if "enum" in obj_def.keys():
allowed_values = []
keys_to_drop.append("enum")
for value in obj_def["enum"]:
if isinstance(value, str):
allowed_values.append(value)
else:
allowed_values.append(value["name"])

text += f"**Allowed values**: `{'`, `'.join(allowed_values)}`\n\n"
keys_to_drop = [
"description",
"display_name",
"name",
"value",
"pattern",
"enum",
"definition",
]
levels = list(obj_def.get("enum", []) or obj_def.get("definition", {}).get("Levels", {}))
if levels:
levels = [level["name"] if isinstance(level, dict) else level for level in levels]
text += f"**Allowed values**: `{'`, `'.join(levels)}`\n\n"

text += f"**Description**:\n{obj_desc}\n\n"

Expand Down
25 changes: 24 additions & 1 deletion tools/schemacode/bidsschematools/render/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ def resolve_metadata_type(definition):

string = " or ".join(substrings)

elif "definition" in definition:
json_def = definition["definition"]

if "Delimiter" in json_def:
# Delimiter indicates the value must be parsed. For BIDS purposes,
# this is a string, even if the parsed array is of numbers.
string = "string"
elif "Levels" in json_def:
# JSON keys are always strings.
string = "string"
elif "Units" in json_def:
# Values with units are always (any exceptions?) numbers.
string = "number"
else:
string = "string or number"
else:
# This clause should only catch $refs.
# The schema should be deferenced by this point, so $refs should not exist.
Expand All @@ -246,7 +261,15 @@ def describe_valid_values(definition):
str : A sentence describing valid values for the object.
"""
description = ""
if "anyOf" in definition.keys():
if "anyOf" in definition:
return description

if "definition" in definition:
levels = definition["definition"].get("Levels")
if levels:
description = (
f"Unless redefined in a sidecar file, must be one of: {', '.join(levels)}."
)
return description

if definition["type"] == "boolean":
Expand Down

0 comments on commit 40ca7bb

Please sign in to comment.