Skip to content

Commit 7697f77

Browse files
authored
fix: types converting corner cases (#2)
* Fixed types corner cases * Bump version for release * Fixed linters
1 parent b0f6c54 commit 7697f77

File tree

5 files changed

+24
-15
lines changed

5 files changed

+24
-15
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ you get this JSON
5858

5959
```json
6060
{
61-
"//": "Generated by j2g at 2022-05-25 12:35:55.333570. DO NOT EDIT",
61+
"//": "Generated by pydantic-glue at 2022-05-25 12:35:55.333570. DO NOT EDIT",
6262
"columns": {
6363
"nums": "array<int>",
6464
"bars": "array<struct<name:string,age:int>>",

pydantic_glue/cli.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ def cli() -> None:
1818

1919
imported = __import__(os.path.basename(module_file))
2020
imported = getattr(imported, class_name)
21-
schema = imported.model_json_schema()
22-
schema = convert(schema)
23-
schema = {k: v for (k, v) in schema}
24-
schema = {
21+
input_schema = json.dumps(imported.model_json_schema())
22+
converted = convert(input_schema)
23+
output = {
2524
"//": f"Generated by pydantic-glue at {datetime.now()}. DO NOT EDIT",
26-
"columns": schema,
25+
"columns": {k: v for (k, v) in converted},
2726
}
2827

29-
print(json.dumps(schema, indent=2))
28+
print(json.dumps(output, indent=2))

pydantic_glue/handler.py

+7-6
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,11 @@ def handle_map(o: dict[str, Any]) -> str:
3939

4040

4141
def handle_union(o: dict[str, Any]) -> str:
42-
res = [dispatch(v) for v in o["anyOf"]]
43-
return f"union<{','.join(res)}>"
42+
types = [i for i in o["anyOf"] if i["type"] != "null"]
43+
if len(types) > 1:
44+
res = [dispatch(v) for v in types]
45+
return f"union<{','.join(res)}>"
46+
return dispatch(types[0])
4447

4548

4649
def map_dispatch(o: dict[str, Any]) -> list[tuple[str, str]]:
@@ -61,9 +64,7 @@ def handle_root(o: dict[str, Any]) -> list[tuple[str, str]]:
6164
return map_dispatch(o)
6265

6366

64-
def convert(schema: dict[str, Any]) -> Union[list[Any], list[tuple[str, str]]]:
67+
def convert(schema: str) -> Union[list[Any], list[tuple[str, str]]]:
6568
if not schema:
6669
return []
67-
68-
schema = jsonref.loads(schema)
69-
return handle_root(schema)
70+
return handle_root(jsonref.loads(schema))

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "pydantic-glue"
3-
version = "0.1.0"
3+
version = "0.2.0"
44
description = "Convert pydantic model to aws glue schema for terraform"
55
authors = ["Serhii Dimchenko <[email protected]>"]
66
readme = "README.md"

tests/unit/test_j2g.py tests/unit/test_convert.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import json
2-
from typing import Union
2+
from typing import Optional, Union
33

44
from pydantic import BaseModel
55
from pydantic_glue import convert
@@ -163,3 +163,12 @@ class A(BaseModel):
163163

164164
expected = [("stuff", "union<struct<hey:string,ho:string>,struct<lets:int,go:int>>")]
165165
assert convert(json.dumps(A.model_json_schema())) == expected
166+
167+
168+
def test_single_optional_column():
169+
class A(BaseModel):
170+
name: Optional[str] = None
171+
172+
expected = [("name", "string")]
173+
actual = json.dumps(A.model_json_schema())
174+
assert convert(actual) == expected

0 commit comments

Comments
 (0)