Initial upload: ConfigConvert CLI with full test suite and CI/CD
This commit is contained in:
51
config_convert/utils/schema.py
Normal file
51
config_convert/utils/schema.py
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
"""JSON Schema generation from parsed configurations."""
|
||||||
|
|
||||||
|
from typing import Any, Dict, List, Union
|
||||||
|
|
||||||
|
|
||||||
|
def generate_schema(data: Any) -> Dict[str, Any]:
|
||||||
|
"""Generate a JSON Schema from a parsed configuration.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
data: The parsed configuration data
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dictionary representing the JSON Schema
|
||||||
|
"""
|
||||||
|
schema: Dict[str, Any] = {
|
||||||
|
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||||
|
"type": _infer_type(data),
|
||||||
|
}
|
||||||
|
|
||||||
|
if isinstance(data, dict):
|
||||||
|
schema["properties"] = {}
|
||||||
|
for key, value in data.items():
|
||||||
|
schema["properties"][key] = generate_schema(value)
|
||||||
|
|
||||||
|
elif isinstance(data, list):
|
||||||
|
if len(data) > 0:
|
||||||
|
schema["items"] = generate_schema(data[0])
|
||||||
|
else:
|
||||||
|
schema["items"] = {"type": "string"}
|
||||||
|
|
||||||
|
return schema
|
||||||
|
|
||||||
|
|
||||||
|
def _infer_type(data: Any) -> str:
|
||||||
|
"""Infer the JSON Schema type from a Python value."""
|
||||||
|
if data is None:
|
||||||
|
return "null"
|
||||||
|
elif isinstance(data, bool):
|
||||||
|
return "boolean"
|
||||||
|
elif isinstance(data, int):
|
||||||
|
return "integer"
|
||||||
|
elif isinstance(data, float):
|
||||||
|
return "number"
|
||||||
|
elif isinstance(data, str):
|
||||||
|
return "string"
|
||||||
|
elif isinstance(data, list):
|
||||||
|
return "array"
|
||||||
|
elif isinstance(data, dict):
|
||||||
|
return "object"
|
||||||
|
else:
|
||||||
|
return "string"
|
||||||
Reference in New Issue
Block a user