Skip to content

feat(capi): consider to use CAPI: 2 instead of CAPI=2: #801

Description

@tymonx

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions