Skip to content

Commit 0f62a91

Browse files
docs: add codegen docs
1 parent 8c5dfcd commit 0f62a91

File tree

20 files changed

+444
-312
lines changed

20 files changed

+444
-312
lines changed

README.md

Lines changed: 34 additions & 277 deletions
Original file line numberDiff line numberDiff line change
@@ -11,324 +11,76 @@ jtd-codebuild is a tool for generating language specific schemas and interfaces
1111

1212
This tool is built on top of [jtd-codegen](https://jsontypedef.com/docs/jtd-codegen/) so check out the documentation if you don't have a clue about JSON Type Definition.
1313

14-
## Quickstart
14+
## Quick Example
1515

16-
First, initialize the configuration file by running the command below.
16+
In this example, we will generate Python and TypeScript code from JSON Type Definition IDL files.
1717

18-
```bash
19-
jtd-codebuild --init
20-
```
21-
22-
## Installation
23-
24-
```bash
25-
pip install jtd-codebuild
26-
```
27-
28-
## Usage
29-
30-
First, you need to create a configuration file named `jtd-codebuild.json` in the root of your project. (See [Configuration](#configuration))
31-
32-
Then, write JSON type definition IDL files in either yaml or json format in the directory you specified in the configuration file.
33-
34-
After all of that, you can run the command below to generate code.
35-
36-
```bash
37-
jtd-codebuild path/to/the/folder/where/jtd-codebuild.json/is/located
38-
```
39-
40-
### Usage Example
41-
42-
You can find the example project here: [jtd_codebuild/tests/fixtures/example_project_1](./jtd_codebuild/tests/fixtures/example_project_1)
43-
44-
Also, you can get a sense of how this tool works by looking at the test cases: [jtd_codebuild/tests/test_example_project_1.py](./jtd_codebuild/tests/test_example_project_1.py)
45-
46-
47-
## Required conventions
48-
49-
### Configuration
50-
51-
The script will find `jtd-codebuild.json` which is the configuration file of this tooling.
52-
53-
#### `root-schema-path`
54-
55-
The path to the root schema file.
56-
Root schema file will be the entry point of the code generation,
57-
where every definition files will be merged into.
58-
59-
Defaults to `schema.jtd.yaml`
60-
61-
#### `definitions-path` (alias: `definitions-paths`)
62-
63-
The path to single or multiple definition files.
64-
This directory will be recursively searched for definition files.
65-
66-
Definition file is a file that contains a single or multiple definitions.
67-
Checkout the documentation below for more information.
68-
69-
Defaults to `[]`
70-
71-
#### `output-schema-path`
72-
73-
The path for the merged schema file converted in json format.
18+
First, copy and paste the following configuration file to the root of your project.
7419

75-
Defaults to `gen/schema.jtd.json`
76-
77-
#### `includes`
78-
79-
Array of JTD package paths to include.
80-
81-
The path should have `jtd-codebuild.json` file in it
82-
so that this tool can find the codebuild configuration.
83-
84-
If you specifiy a package path,
85-
it will reference the package's schema definitions
86-
when generating schema file you are working on.
87-
88-
Defaults to `[]`
89-
90-
#### `allow-duplicate-defs`
91-
92-
Whether to allow duplicate definitions.
93-
94-
This only applies to the definitions that are not in the same jtd codebuild project.
95-
96-
That is, definitions in `definitions-path` in each project always raises error if there are duplicate definitions.
97-
98-
Defaults to `false`
99-
100-
#### `targets`
101-
102-
Compile targets.
103-
104-
Defaults to `[]`
105-
106-
It's a JSONRecord contains the object having following properties:
107-
- `language (string)`: The language of the target.
108-
We essentially inject this value to `jtd-codegen` as target language option
109-
which is provided as a flag which is like `--{language}-out`.
110-
Available languages can be found in the documentation of `jtd-codegen`.
111-
(See: https://jsontypedef.com/)
112-
- `path (string)`: The path to the directory where the generated code will be placed.
113-
114-
##### `targets` - Language Specific Options - Python
115-
116-
- `use-pydantic (boolean)`: Whether to use pydantic as a `dataclass` decorator provider.
117-
If this is set to true, the generated code will use `pydantic.dataclasses.dataclass` as a `dataclass` decorator so that you can use pydantic's validation features.
118-
Otherwise, the generated code will be plain python dataclasses.
119-
Defaults to `false`.
120-
- `use-typeddict (boolean)`: Whether to use `TypedDict` instead of `dataclass`.
121-
If this is set to true, the generated code will use `TypedDict` instead of `dataclass`. This property cannot be set to true if `use-pydantic` is set to true. Also, subscriptable option will be ignored if this is set to true.
122-
- `property-case (enum)`: The case of the property names.
123-
This property is only effective when `use-typeddict` is set to true.
124-
Available values are `snake` and `camel`.
125-
Defaults to `snake`.
126-
- `subscriptable (boolean)`: Whether to make the generated class subscriptable.
127-
If this is set to true, the generated class will be subscriptable so that you can access the properties of the class like `obj["property"]`.
128-
Otherwise, the generated class will not be subscriptable.
129-
Defaults to `false`.
130-
131-
##### `targets` - Language Specific Options - TypeScript
132-
133-
- `tsconfig-path (string)`: The path to the tsconfig file.
134-
This will be used to compile typescript code
135-
to javascript code and type declarations.
136-
If you want to automatically generate
137-
plain javascript artifact with type declarations,
138-
you should also provide this option.
139-
140-
141-
### Configuration Example
142-
143-
Example congfiguration file is provided below. Copy it and modify it to your needs.
144-
145-
```jsonc
20+
```json
14621
{
147-
"root-schema-path": "schema.jtd.yaml",
148-
"definitions-path": "definitions",
149-
"output-schema-path": "gen/schema.jtd.json",
150-
"includes": ["../path/to/another/folder/that/contains/jtd-codebuild.json"],
22+
"include": [
23+
"src"
24+
],
25+
"references": [],
26+
"jtdBundlePath": "gen/schema.jtd.json",
15127
"targets": [
15228
{
15329
"language": "python",
15430
"path": "gen/python"
15531
},
156-
{
157-
"language": "python",
158-
"path": "gen/python-pydantic",
159-
"use-pydantic": true,
160-
"subscriptable": true
161-
},
162-
{
163-
"language": "python",
164-
"path": "gen/python-typeddict",
165-
"use-typeddict": true,
166-
"subscriptable": true
167-
},
16832
{
16933
"language": "typescript",
170-
"path": "gen/typescript",
171-
"tsconfig-path": "tsconfig.build.json"
34+
"path": "gen/typescript"
17235
}
173-
]
36+
],
37+
"$schema": "https://raw.githubusercontent.com/01Joseph-Hwang10/jtd-codebuild/master/jtd_codebuild/config/project/config.json"
17438
}
17539
```
17640

177-
### Root Schema File
178-
179-
Root schema file is the entry point of the code generation.
180-
181-
It will be the file where every definition files will be merged into.
182-
183-
If you don't need a root `Schema` type, you can just create an empty file.
184-
185-
### Definition files
186-
187-
Definition files are sharable files of which each of them contains a single or multiple definitions.
188-
189-
Each declared keys as a root key in the definition file will be merged as a key of `definitions` object in the root schema file, and those symbols can be shared across the other definition files.
190-
191-
For example, let's say you have a definition file whose code is like below.
41+
Then, we'll create some JSON Type Definition IDL files in the `src` directory.
19242

19343
```yaml
194-
book:
44+
# src/book.jtd.yaml
45+
Book:
19546
properties:
19647
id:
19748
type: string
19849
title:
19950
type: string
20051
```
201-
202-
This can be referenced in other definition files like below.
203-
20452
```yaml
205-
user:
53+
# src/user.jtd.yaml
54+
User:
20655
properties:
20756
id:
20857
type: string
20958
name:
21059
type: string
21160
books:
21261
elements:
213-
ref: book
214-
```
215-
216-
This will be merged as a single schema like below.
217-
218-
```json
219-
{
220-
"definitions": {
221-
"book": {
222-
"properties": {
223-
"id": {
224-
"type": "string"
225-
},
226-
"title": {
227-
"type": "string"
228-
}
229-
}
230-
},
231-
"user": {
232-
"properties": {
233-
"id": {
234-
"type": "string"
235-
},
236-
"name": {
237-
"type": "string"
238-
},
239-
"books": {
240-
"elements": {
241-
"ref": "book"
242-
}
243-
}
244-
}
245-
}
246-
}
247-
}
248-
```
249-
250-
Checkout more about `ref` if you don't have a clue about it. https://jsontypedef.com/docs/jtd-in-5-minutes/#ref-schemas
251-
252-
### Inheritance
253-
254-
`jtd-codebuild` supports inheritance between type definitions.
255-
256-
You can do this by using `extends` keyword like below.
257-
258-
```yaml
259-
Person:
260-
properties:
261-
id:
262-
type: string
263-
name:
264-
type: string
265-
266-
Chef:
267-
extends: Person
268-
properties:
269-
restaurant:
270-
type: string
271-
272-
HeadChef:
273-
extends: Chef
274-
properties:
275-
sousChef:
276-
ref: Chef
62+
ref: Book
27763
```
27864
279-
You can also extend multiple types like below.
280-
281-
```yaml
282-
Person:
283-
properties:
284-
id:
285-
type: string
286-
name:
287-
type: string
288-
289-
MyRestaurantMixin:
290-
properties:
291-
restaurant:
292-
type: string
65+
Finally, run the following command to generate the code.
29366
294-
Chef:
295-
extends: [Person, MyRestaurantMixin]
67+
```bash
68+
jtd-codebuild .
29669
```
29770

298-
### Manual dependency management
299-
300-
Since IDL files are basically just a bunch of JSON objects,
301-
we need to manually manage the dependency between the definition files.
71+
You can find the generated code in the `gen` directory.
30272

303-
For example, assume you have a folder structure like the below:
73+
## More Examples
30474

305-
```
306-
definitions
307-
├── book
308-
│   └── book.jtd.yaml
309-
└── user
310-
└── user.jtd.yaml
311-
```
75+
You can find more examples under the [tests] directory:
31276

313-
And assume that `book.jtd.yaml` and `user.jtd.yaml` are the root definition files of each module.
77+
- [Python Codegen Examples][python-codegen-example]
78+
- [TypeScript Codegen Examples][typescript-codegen-example]
79+
- [Monorepo Example (includes other languages' examples)][monorepo-example]
31480

315-
In this case, you need to annotate that `user.jtd.yaml` depends on `book.jtd.yaml` like below.
81+
## API Documentation
31682

317-
```yaml
318-
# user.jtd.yaml
319-
#
320-
# Depends on:
321-
# - book (at ../book/book.jtd.yaml)
322-
user:
323-
properties:
324-
id:
325-
type: string
326-
name:
327-
type: string
328-
books:
329-
elements:
330-
ref: book
331-
```
83+
See the [API Documentation](./docs/index.md) for more information.
33284

33385
## Contributing
33486

@@ -337,3 +89,8 @@ Any contribution is welcome! Check out [CONTRIBUTING.md](https://github.com/01Jo
33789
## License
33890

33991
`jtd-codebuild` is licensed under a [MIT License](https://github.com/01Joseph-Hwang10/jtd-codebuild/blob/master/LICENSE).
92+
93+
[python-codegen-example]: ./tests/python_targets/project/
94+
[typescript-codegen-example]: ./tests/typescript_targets/project/
95+
[monorepo-example]: ./tests/monorepo/workspace/
96+
[tests]: ./tests/

docs/cli/build.md

Whitespace-only changes.

docs/cli/init.md

Whitespace-only changes.

docs/codegen/config/csharp.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# CSharpTarget
2+
3+
The `CSharpTarget` class is a subclass of `Target`
4+
that is used to configure the code generation for CSharp.
5+
6+
## Configuration Options
7+
8+
### `language`
9+
10+
> - Required
11+
12+
Set the `language` field to `"csharp"` to generate CSharp code.
13+
14+
### `path`
15+
16+
> - Required
17+
18+
The `path` field is a string that specifies the path to generate the code.
19+
20+
### `namespace`
21+
22+
> - Required
23+
24+
The `namespace` specifies namespace of the generated classes.
25+
26+
### `typingBackend`
27+
28+
> - Optional
29+
> - Default: `System.Text.Json`
30+
> - Possible values: `System.Text.Json`
31+
32+
The `typingBackend` field specifies the implementation of the typing system.
33+
34+
Currently, only `System.Text.Json` is supported. (See <https://jsontypedef.com/docs/csharp-codegen/> for more information)

0 commit comments

Comments
 (0)