Skip to content

Commit 988fe77

Browse files
committed
First version example
0 parents  commit 988fe77

10 files changed

+1755
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist
2+
node_modules

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Kim T
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# quicktype-typescript-input-demo
2+
3+
Example quicktype project using TypeScript as input, built with:
4+
5+
* NodeJS 20.x
6+
* TypeScript 5.x
7+
* Quicktype 23.x
8+
9+
10+
## Installation
11+
12+
Install dependencies using:
13+
14+
npm install
15+
16+
17+
## Usage
18+
19+
Run the dev version using:
20+
21+
npm run dev
22+
23+
Create a build using:
24+
25+
npm run build
26+
27+
28+
## Example
29+
30+
Input:
31+
32+
```
33+
export enum Direction {
34+
Up = "UP",
35+
Down = "DOWN",
36+
Left = "LEFT",
37+
Right = "RIGHT",
38+
}
39+
```
40+
41+
Output json schema:
42+
```
43+
{
44+
"$schema": "http://json-schema.org/draft-06/schema#",
45+
"$ref": "#/definitions/Direction",
46+
"definitions": {
47+
"Direction": {
48+
"type": "string",
49+
"enum": [
50+
"DOWN",
51+
"LEFT",
52+
"RIGHT",
53+
"UP"
54+
],
55+
"title": "Direction"
56+
}
57+
}
58+
}
59+
```
60+
61+
Output java:
62+
```
63+
package io.quicktype;
64+
65+
import java.io.IOException;
66+
67+
public enum Direction {
68+
DOWN, LEFT, RIGHT, UP;
69+
70+
public String toValue() {
71+
switch (this) {
72+
case DOWN: return "DOWN";
73+
case LEFT: return "LEFT";
74+
case RIGHT: return "RIGHT";
75+
case UP: return "UP";
76+
}
77+
return null;
78+
}
79+
80+
public static Direction forValue(String value) throws IOException {
81+
if (value.equals("DOWN")) return DOWN;
82+
if (value.equals("LEFT")) return LEFT;
83+
if (value.equals("RIGHT")) return RIGHT;
84+
if (value.equals("UP")) return UP;
85+
throw new IOException("Cannot deserialize Direction");
86+
}
87+
}
88+
89+
```
90+
91+
## Contact
92+
93+
For more information please contact kmturley

index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { mkdirSync, writeFileSync } from 'fs';
2+
import { globSync } from 'glob';
3+
import { dirname } from 'path';
4+
import { InputData, JSONSchemaInput, JSONSchemaSourceData, JSONSchemaStore, RendererOptions, SerializedRenderResult, quicktype } from 'quicktype-core';
5+
import { schemaForTypeScriptSources } from 'quicktype-typescript-input';
6+
7+
interface Config {
8+
lang: string;
9+
ext: string;
10+
rendererOptions: RendererOptions
11+
}
12+
13+
async function load(globPath: string, configs: Config[]) {
14+
const filePaths: string[] = globSync(globPath);
15+
console.log(`📁 ${filePaths}`);
16+
const jsonSchema: JSONSchemaSourceData = schemaForTypeScriptSources(filePaths);
17+
// @ts-ignore
18+
const jsonStore: JSONSchemaStore = new JSONSchemaStore();
19+
const jsonInput: JSONSchemaInput = new JSONSchemaInput(jsonStore);
20+
await jsonInput.addSource({ name: '#/definitions/', schema: jsonSchema.schema });
21+
const inputData: InputData = new InputData();
22+
inputData.addInput(jsonInput);
23+
for (let i = 0; i < configs.length; i++) {
24+
const { lang, ext, rendererOptions } = configs[i];
25+
const result: SerializedRenderResult = await quicktype({ inputData, rendererOptions, lang });
26+
createFile(`./dist/${lang}/types.${ext}`, result.lines.join('\n'));
27+
}
28+
}
29+
30+
function createFile(filePath: string, fileContents: string) {
31+
mkdirSync(dirname(filePath), { recursive: true });
32+
writeFileSync(filePath, fileContents);
33+
console.log(`+ ${filePath}`);
34+
}
35+
36+
load('./src/**/*.ts', [
37+
{ lang: 'java', ext: 'java', rendererOptions: { "just-types": true } },
38+
{ lang: 'schema', ext: 'json', rendererOptions: { "just-types": true } },
39+
{ lang: 'ts', ext: 'ts', rendererOptions: { "just-types": true } }
40+
]);

0 commit comments

Comments
 (0)