Skip to content

Commit 877d6bb

Browse files
authored
Merge pull request #140 from nsoranzo/fix_settings_to_sample
Update ``settings_to_sample()`` to new Pydantic
2 parents d26a732 + c7e0a2a commit 877d6bb

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

gravity/settings.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -461,8 +461,10 @@ def _disable_service_instances_if_direct(cls, value: bool, info: ValidationInfo)
461461

462462
def process_property(key, value, depth=0):
463463
extra_white_space = " " * depth
464-
default = value.get("default", "")
465-
if isinstance(default, dict):
464+
default = value.get("default")
465+
if default is None:
466+
default = ""
467+
elif isinstance(default, dict):
466468
# Little hack that prevents listing the default value for tusd in the sample config
467469
default = {}
468470
if default != "":
@@ -472,11 +474,14 @@ def process_property(key, value, depth=0):
472474
default = default[: -(len("\n...\n"))]
473475
default = default.strip()
474476
description = "\n".join(f"{extra_white_space}# {desc}".rstrip() for desc in value["description"].strip().split("\n"))
475-
combined = value.get("allOf", [])
476-
if not combined and value.get("anyOf"):
477+
if value.get("anyOf"):
477478
# we've got a union
478-
combined = [c for c in value["anyOf"] if c["type"] == "object"]
479-
if combined and combined[0].get("properties"):
479+
combined = [c for c in value["anyOf"] if c["type"] != "null"]
480+
elif "enum" in value or "properties" in value:
481+
combined = [value]
482+
else:
483+
combined = []
484+
if combined and any(item.get("properties") for item in combined):
480485
# we've got a nested map, add key once
481486
description = f"{description}\n{extra_white_space}{key}:\n"
482487
has_child = False
@@ -506,7 +511,5 @@ def settings_to_sample():
506511
# expand schema for easier processing
507512
data = jsonref.replace_refs(schema, merge_props=True)
508513
strings = [process_property("gravity", data)]
509-
for key, value in data["properties"].items():
510-
strings.append(process_property(key, value, 1))
511514
concat = "\n".join(strings)
512515
return concat

tests/test_settings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,4 @@ def test_schema_to_sample():
5050
sample = settings_to_sample()
5151
settings = Settings(**yaml.safe_load(StringIO(sample))["gravity"])
5252
default_settings = Settings()
53-
assert settings.dict() == default_settings.dict()
53+
assert settings.model_dump() == default_settings.model_dump()

0 commit comments

Comments
 (0)