Skip to content

Commit d723fb9

Browse files
feat: add python discriminator support
1 parent 5d1eb7b commit d723fb9

File tree

8 files changed

+534
-191
lines changed

8 files changed

+534
-191
lines changed

jtd_codebuild/generators/python/_generator.py

Lines changed: 428 additions & 184 deletions
Large diffs are not rendered by default.

jtd_codebuild/generators/typescript/_generator.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,32 @@ def generate(self, target: TypescriptTarget) -> None:
2121
propertyFormat = target.propertyFormat
2222

2323
if propertyFormat:
24-
convert_schema_definition_keys = compose_left(
24+
convert_property_keys = compose_left(
2525
curry(convert_keys_case)(propertyFormat),
2626
dict,
2727
)
28+
29+
def convert_definition_keys_case(definition: dict) -> None:
30+
if "discriminator" in definition:
31+
definition["discriminator"] = caseconverter(
32+
propertyFormat,
33+
definition["discriminator"],
34+
)
35+
for case_ in definition["mapping"].values():
36+
convert_definition_keys_case(case_)
37+
else:
38+
properties = definition.get("properties", None)
39+
if properties:
40+
definition["properties"] = convert_property_keys(properties)
41+
optionalProperties = definition.get("optionalProperties", None)
42+
if optionalProperties:
43+
definition["optionalProperties"] = convert_property_keys(
44+
optionalProperties
45+
)
46+
2847
for class_name in definitions.keys():
2948
definition = definitions[class_name]
30-
definition["properties"] = convert_schema_definition_keys(
31-
definition.get("properties", {})
32-
)
33-
definition["optionalProperties"] = convert_schema_definition_keys(
34-
definition.get("optionalProperties", {})
35-
)
49+
convert_definition_keys_case(definition)
3650

3751
write_json(jtd_schema_path, jtd_schema)
3852

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# flake8: noqa: F401
2+
3+
from ._find import find, find_one

jtd_codebuild/utils/list/_find.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import TypeVar, Callable
2+
from toolz import pipe
3+
from toolz.curried import filter
4+
5+
T = TypeVar("T")
6+
7+
8+
def find(
9+
predicate: Callable[[T], bool],
10+
nodes: list[T],
11+
) -> list[T]:
12+
return pipe(
13+
nodes,
14+
filter(predicate),
15+
list,
16+
)
17+
18+
19+
def find_one(
20+
predicate: Callable[[T], bool],
21+
nodes: list[T],
22+
) -> T | None:
23+
found = find(predicate, nodes)
24+
if len(found) > 0:
25+
return found[0]
26+
return None
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Event:
2+
discriminator: eventType
3+
mapping:
4+
USER_CREATED:
5+
properties:
6+
id:
7+
type: string
8+
USER_PAYMENT_PLAN_CHANGED:
9+
properties:
10+
id:
11+
type: string
12+
plan:
13+
enum:
14+
- Free
15+
- Paid
16+
USER_DELETED:
17+
properties:
18+
id:
19+
type: string
20+
softDelete:
21+
type: boolean

tests/python_targets/project/src/person.jtd.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ Person:
1010

1111
metadata:
1212
description: The name of the person.
13+
14+
status:
15+
enum:
16+
- happy
17+
- sad
18+
- angry
19+
- confused
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Event:
2+
discriminator: eventType
3+
mapping:
4+
USER_CREATED:
5+
properties:
6+
id:
7+
type: string
8+
USER_PAYMENT_PLAN_CHANGED:
9+
properties:
10+
id:
11+
type: string
12+
plan:
13+
enum:
14+
- Free
15+
- Paid
16+
USER_DELETED:
17+
properties:
18+
id:
19+
type: string
20+
softDelete:
21+
type: boolean

tests/typescript_targets/project/src/person.jtd.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,10 @@ Person:
1010

1111
metadata:
1212
description: The name of the person.
13+
14+
status:
15+
enum:
16+
- happy
17+
- sad
18+
- angry
19+
- confused

0 commit comments

Comments
 (0)