Skip to content

Commit

Permalink
Add a method for getting the schema by name
Browse files Browse the repository at this point in the history
The schema class can be retrieved using a static method. This fixes
some minor typing errors as well in the cli.validate function.
  • Loading branch information
JeremyMcCormick committed Jan 13, 2024
1 parent 07ac34f commit cf991f3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 3 additions & 7 deletions python/felis/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import logging
import sys
from collections.abc import Iterable, Mapping, MutableMapping
from typing import Any
from typing import Any, Type

import click
import yaml
Expand All @@ -39,7 +39,7 @@
from .sql import SQLVisitor
from .tap import Tap11Base, TapLoadingVisitor, init_tables
from .utils import ReorderingVisitor
from .validation import RspSchema
from .validation import get_schema

logger = logging.getLogger("felis")

Expand Down Expand Up @@ -313,11 +313,7 @@ def merge(files: Iterable[io.TextIOBase]) -> None:
@click.argument("files", nargs=-1, type=click.File())
def validate(schema_name: str, files: Iterable[io.TextIOBase]) -> None:
"""Validate one or more felis YAML files."""
if schema_name == "RSP":
schema_class = RspSchema
else:
schema_class = Schema

schema_class: Type[Schema] = get_schema(schema_name)
logger.info(f"Using schema {schema_class.__name__}")

Check warning on line 317 in python/felis/cli.py

View check run for this annotation

Codecov / codecov/patch

python/felis/cli.py#L316-L317

Added lines #L316 - L317 were not covered by tests

rc = 0
Expand Down
11 changes: 11 additions & 0 deletions python/felis/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import logging
from typing import Type

from pydantic import Field, conlist, model_validator

Expand Down Expand Up @@ -89,3 +90,13 @@ def check_tap_table_indexes(cls, sch: "RspSchema") -> "RspSchema":
raise ValueError(f"Duplicate 'tap:table_index' value {table_index} found in schema")
table_indicies.add(table_index)
return sch


def get_schema(schema_name: str) -> Type[Schema]:
"""Get the schema class for the given name."""
if schema_name == "default":
return Schema

Check warning on line 98 in python/felis/validation.py

View check run for this annotation

Codecov / codecov/patch

python/felis/validation.py#L98

Added line #L98 was not covered by tests
elif schema_name == "RSP":
return RspSchema

Check warning on line 100 in python/felis/validation.py

View check run for this annotation

Codecov / codecov/patch

python/felis/validation.py#L100

Added line #L100 was not covered by tests
else:
raise ValueError(f"Unknown schema name '{schema_name}'")

Check warning on line 102 in python/felis/validation.py

View check run for this annotation

Codecov / codecov/patch

python/felis/validation.py#L102

Added line #L102 was not covered by tests

0 comments on commit cf991f3

Please sign in to comment.