Skip to content

Commit 5282e1a

Browse files
fix: project config type validation
1 parent a6bb1c5 commit 5282e1a

File tree

15 files changed

+43
-26
lines changed

15 files changed

+43
-26
lines changed

docs/codegen/config/project.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ The path should be a folder that contains the configuration file, named `jtd-cod
1111

1212
### `include`
1313

14-
> - Required
14+
> - Optional
15+
> - Default: `[]`
1516
1617
The `include` field is an array of directories
1718
that contain the JSON Type Definition IDL files.

jtd_codebuild/bundler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def bundle(
3737

3838
# Bundle reference schemas
3939
schemas: list[dict] = pipe(
40-
config.references or [],
40+
config.references,
4141
map(resolve),
4242
map(self.bundle),
4343
)

jtd_codebuild/config/project/config.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -431,11 +431,6 @@
431431
"type": "string"
432432
}
433433
},
434-
"required": [
435-
"include",
436-
"references",
437-
"targets"
438-
],
439434
"title": "ProjectConfig",
440435
"type": "object"
441436
}

jtd_codebuild/config/project/model/_config.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from pydantic import BaseModel
1+
from pydantic import BaseModel, Field
22
from ._types import DuplicatePolicy, TargetProcessingStrategy
33
from ._target import (
44
PythonTarget,
@@ -13,8 +13,8 @@
1313

1414

1515
class ProjectConfig(BaseModel, Subscriptable):
16-
include: list[str]
17-
references: list[str] | None = None
16+
include: list[str] = Field(default_factory=lambda: [])
17+
references: list[str] = Field(default_factory=lambda: [])
1818
targets: list[
1919
PythonTarget
2020
| TypescriptTarget
@@ -23,7 +23,7 @@ class ProjectConfig(BaseModel, Subscriptable):
2323
| CSharpTarget
2424
| RustTarget
2525
| RubyTarget
26-
]
26+
] = Field(default_factory=lambda: [])
2727
targetProcessingStrategy: TargetProcessingStrategy = "parallel"
2828
jtdBundlePath: str = "gen/schema.jtd.json"
2929
duplicate: DuplicatePolicy = "error"

jtd_codebuild/config/project/scripts/__init__.py

Whitespace-only changes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from os.path import join, dirname
2+
from jtd_codebuild.config.project.model import ProjectConfig
3+
from jtd_codebuild.utils.io import write_json
4+
5+
if __name__ == "__main__":
6+
write_json(
7+
join(dirname(__file__), "..", "config.json"),
8+
ProjectConfig.model_json_schema(),
9+
)

jtd_codebuild/generators/_generator.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import abc
22
import subprocess
3+
from os import makedirs
34
from jtd_codebuild.component import Component
45
from jtd_codebuild.logger import Logger
56
from jtd_codebuild.config.project.model import Target
6-
from jtd_codebuild.utils.fs import safe_mkdir, resolve
7+
from jtd_codebuild.utils.fs import resolve
78
from jtd_codebuild.utils.subprocess import stream_logs
89

910

@@ -54,7 +55,7 @@ def generate(self, target: Target) -> None:
5455
Returns:
5556
A list of subprocesses created by the code generation.
5657
"""
57-
safe_mkdir(self.get_target_path(target))
58+
makedirs(self.get_target_path(target), exist_ok=True)
5859
process = subprocess.Popen(
5960
self._codegen_command(target),
6061
shell=True,

jtd_codebuild/utils/fs.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,6 @@ def file_is_json(file: str) -> bool:
2727
return file.endswith(".json")
2828

2929

30-
def safe_mkdir(path: str) -> None:
31-
"""Create a directory with creating its parent directories if they do not exist.
32-
33-
Args:
34-
path: The directory path.
35-
"""
36-
makedirs(path, exist_ok=True)
37-
38-
3930
def safe_open(file_path: str, mode: str) -> TextIOWrapper:
4031
"""Open a file with creating its parent directories if they do not exist.
4132
@@ -46,7 +37,7 @@ def safe_open(file_path: str, mode: str) -> TextIOWrapper:
4637
Returns:
4738
The opened file.
4839
"""
49-
safe_mkdir(dirname(file_path))
40+
makedirs(dirname(file_path), exist_ok=True)
5041
return open(file_path, mode)
5142

5243

jtd_codebuild/utils/io.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22
from typing import Any
3+
from os import makedirs
34
from os.path import dirname
4-
from .fs import safe_mkdir
55

66

77
def read(file: str) -> str:
@@ -13,7 +13,7 @@ def read(file: str) -> str:
1313
Returns:
1414
The file content.
1515
"""
16-
safe_mkdir(dirname(file))
16+
makedirs(dirname(file), exist_ok=True)
1717
with open(file, "r") as f:
1818
return f.read()
1919

@@ -25,7 +25,7 @@ def write(file: str, data: str) -> None:
2525
file: The file name.
2626
data: The file content.
2727
"""
28-
safe_mkdir(dirname(file))
28+
makedirs(dirname(file), exist_ok=True)
2929
with open(file, "w") as f:
3030
f.write(data)
3131

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ jtd-codebuild = "jtd_codebuild.__main__:cli"
5454
test = "python -m pytest"
5555
lint = "flake8 jtd_codebuild"
5656
check-format = "black jtd_codebuild --check"
57+
generate-project-config-json-schema = "python -m jtd_codebuild.config.project.scripts.generate_project_config_json_schema"
5758

5859
[tool.black]
5960
line-length = 88

0 commit comments

Comments
 (0)