Skip to content

Commit b665678

Browse files
committed
Map attribute kind to parameter for Yaml language server
1 parent 81e4f2b commit b665678

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

backend/infrahub/core/schema/attribute_schema.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,31 @@
3030

3131

3232
def get_attribute_schema_class_for_kind(kind: str) -> type[AttributeSchema]:
33-
attribute_schema_class_by_kind: dict[str, type[AttributeSchema]] = {
34-
"NumberPool": NumberPoolSchema,
35-
"Text": TextAttributeSchema,
36-
"TextArea": TextAttributeSchema,
37-
"Number": NumberAttributeSchema,
38-
}
3933
return attribute_schema_class_by_kind.get(kind, AttributeSchema)
4034

4135

4236
class AttributeSchema(GeneratedAttributeSchema):
4337
_sort_by: list[str] = ["name"]
4438
_enum_class: type[enum.Enum] | None = None
4539

40+
@classmethod
41+
def model_json_schema(cls, *args: Any, **kwargs: Any) -> dict[str, Any]:
42+
schema = super().model_json_schema(*args, **kwargs)
43+
44+
# Build conditional schema based on attribute_schema_class_by_kind mapping
45+
# This override allows people using the Yaml language server to get the correct mappings
46+
# for the parameters when selecting the appropriate kind
47+
schema["allOf"] = []
48+
for kind, schema_class in attribute_schema_class_by_kind.items():
49+
schema["allOf"].append(
50+
{
51+
"if": {"properties": {"kind": {"const": kind}}},
52+
"then": {"properties": {"parameters": {"$ref": f"#/definitions/{schema_class.__name__}"}}},
53+
}
54+
)
55+
56+
return schema
57+
4658
@property
4759
def is_attribute(self) -> bool:
4860
return True
@@ -247,3 +259,11 @@ class NumberAttributeSchema(AttributeSchema):
247259
description="Extra parameters specific to number attributes",
248260
json_schema_extra={"update": UpdateSupport.VALIDATE_CONSTRAINT.value},
249261
)
262+
263+
264+
attribute_schema_class_by_kind: dict[str, type[AttributeSchema]] = {
265+
"NumberPool": NumberPoolSchema,
266+
"Text": TextAttributeSchema,
267+
"TextArea": TextAttributeSchema,
268+
"Number": NumberAttributeSchema,
269+
}

0 commit comments

Comments
 (0)