Skip to content

Commit 6fb1cd3

Browse files
fix(cli): improve format detection to support all 11 schema formats (#22)
2 parents 8857d45 + cb17c93 commit 6fb1cd3

3 files changed

Lines changed: 59 additions & 13 deletions

File tree

src/schemaforge/cli.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,17 @@ def check(directory: str, canonical: str, type_map_path: str | None) -> None:
137137

138138
def _detect_format(path: str) -> str:
139139
ext = Path(path).suffix.lower()
140-
if ext == ".sql":
141-
return "sql"
142-
if ext == ".prisma":
143-
return "prisma"
144-
if ext in (".ts", ".tsx"):
145-
return "drizzle"
146-
if ext == ".py":
147-
return "django"
148-
if ext in (".json",):
149-
return "typeorm"
150-
return "sql" # default
140+
ext_map = {
141+
".sql": "sql",
142+
".prisma": "prisma",
143+
".ts": "drizzle",
144+
".tsx": "drizzle",
145+
".py": "django",
146+
".json": "json_schema",
147+
".graphql": "graphql",
148+
".gql": "graphql",
149+
".cs": "ef",
150+
".scala": "scala",
151+
}
152+
return ext_map.get(ext, "sql")
151153

src/schemaforge/mcp_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def create_server() -> Any:
5555
@server.tool(
5656
name="convert",
5757
description="Convert a schema from one format to another. "
58-
"All 9 formats support conversion to and from every other format. "
58+
"All 11 formats support conversion to and from every other format. "
5959
"Returns the converted schema as text.",
6060
)
6161
def convert_tool(

tests/test_cli.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
1414

15-
from schemaforge.cli import main
15+
from schemaforge.cli import _detect_format, main
1616

1717
# ── Helpers ──
1818

@@ -460,3 +460,47 @@ def test_check_help(self):
460460
assert result.exit_code == 0
461461
assert "Usage:" in result.output
462462
assert "--dir" in result.output
463+
464+
465+
# ═══════════════════════════════════════════════════════════════
466+
# _detect_format
467+
# ═══════════════════════════════════════════════════════════════
468+
469+
class TestDetectFormat:
470+
"""Tests for the private _detect_format helper."""
471+
472+
def test_sql_extension(self):
473+
assert _detect_format("schema.sql") == "sql"
474+
475+
def test_prisma_extension(self):
476+
assert _detect_format("schema.prisma") == "prisma"
477+
478+
def test_drizzle_ts(self):
479+
assert _detect_format("schema.ts") == "drizzle"
480+
481+
def test_drizzle_tsx(self):
482+
assert _detect_format("schema.tsx") == "drizzle"
483+
484+
def test_django_python(self):
485+
assert _detect_format("models.py") == "django"
486+
487+
def test_json_schema(self):
488+
assert _detect_format("schema.json") == "json_schema"
489+
490+
def test_graphql(self):
491+
assert _detect_format("schema.graphql") == "graphql"
492+
493+
def test_graphql_gql(self):
494+
assert _detect_format("schema.gql") == "graphql"
495+
496+
def test_ef_csharp(self):
497+
assert _detect_format("entities.cs") == "ef"
498+
499+
def test_scala(self):
500+
assert _detect_format("models.scala") == "scala"
501+
502+
def test_unknown_extension_defaults_to_sql(self):
503+
assert _detect_format("schema.txt") == "sql"
504+
505+
def test_no_extension_defaults_to_sql(self):
506+
assert _detect_format("schema") == "sql"

0 commit comments

Comments
 (0)