Context: #792 and #800
Using the CAPI=2: directive makes loading the correct JSON schema for validation harder not easier. Because tools will require to iterate over YAML keys like CAPI=2, CAPI=3, CAPI=4, .... The proposition is to replace the CAPI=2: with more agnostic CAPI: <version>, make it part of JSON schema but still keep it backward compatible. Additionally, the CAPI: could be optional. This will make FuseSoC core files closer to any existing YAML formats.
JSON schema proposition:
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "CAPI2",
"description": "Core API",
"type": "object",
"properties": {
"CAPI=2": {
"description": "Short for 'Core API version 2'. The schema used in core files.",
"type": "null"
},
"CAPI": {
"description": "Short for 'Core API' version. The schema used in core files.",
"type": "integer",
"minimum": 2,
"maximum": 2
}
},
"oneOf": [
"CAPI=2",
"CAPI"
]
}
This will simplify loading and validating core files to few lines:
SCHEMAS: dict[int, str] = {
2: capi2_schema,
}
@functools.cache
def schema_validator(version: int = 2) -> Callable[[object], None]:
"""Get function to validate data againts JSON schema. Used schemas are cached for performance."""
return fastjsonschema.compile(SCHEMAS[version])
def parse(file: Path | str) -> dict[str, Any]:
with open(file) as fp:
data = yaml.load(fp)
schema_validator(data.get("CAPI", 2))(data)
return data
In current implementation, compiled schema is not cached.
Context: #792 and #800
Using the
CAPI=2:directive makes loading the correct JSON schema for validation harder not easier. Because tools will require to iterate over YAML keys likeCAPI=2,CAPI=3,CAPI=4, .... The proposition is to replace theCAPI=2:with more agnosticCAPI: <version>, make it part of JSON schema but still keep it backward compatible. Additionally, theCAPI:could be optional. This will make FuseSoC core files closer to any existing YAML formats.JSON schema proposition:
{ "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "CAPI2", "description": "Core API", "type": "object", "properties": { "CAPI=2": { "description": "Short for 'Core API version 2'. The schema used in core files.", "type": "null" }, "CAPI": { "description": "Short for 'Core API' version. The schema used in core files.", "type": "integer", "minimum": 2, "maximum": 2 } }, "oneOf": [ "CAPI=2", "CAPI" ] }This will simplify loading and validating core files to few lines:
In current implementation, compiled schema is not cached.