-
-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added example to generate all models within the same file (#1054)
- Loading branch information
1 parent
de5a94b
commit 5a8650a
Showing
8 changed files
with
163 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
output |
17 changes: 17 additions & 0 deletions
17
examples/generate-all-models-within-the-same-file/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Generate all models within the same file | ||
|
||
A basic example of how to generate all models within the same file. | ||
|
||
## How to run this example | ||
|
||
Run this example using: | ||
|
||
```sh | ||
npm i && npm run start | ||
``` | ||
|
||
If you are on Windows, use the `start:windows` script instead: | ||
|
||
```sh | ||
npm i && npm run start:windows | ||
``` |
41 changes: 41 additions & 0 deletions
41
examples/generate-all-models-within-the-same-file/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to generate models to files and should log expected output to console 1`] = ` | ||
Array [ | ||
" | ||
public class Root { | ||
private Email email; | ||
public Email getEmail() { return this.email; } | ||
public void setEmail(Email email) { this.email = email; } | ||
} | ||
public enum Email { | ||
EXAMPLE1_AT_TEST_DOT_COM((String)\\"example1@test.com\\"), EXAMPLE2_AT_TEST_DOT_COM((String)\\"example2@test.com\\"); | ||
private String value; | ||
Email(String value) { | ||
this.value = value; | ||
} | ||
public String getValue() { | ||
return value; | ||
} | ||
public static Email fromValue(String value) { | ||
for (Email e : Email.values()) { | ||
if (e.value.equals(value)) { | ||
return e; | ||
} | ||
} | ||
throw new IllegalArgumentException(\\"Unexpected value '\\" + value + \\"'\\"); | ||
} | ||
@Override | ||
public String toString() { | ||
return String.valueOf(value); | ||
} | ||
}", | ||
] | ||
`; |
24 changes: 24 additions & 0 deletions
24
examples/generate-all-models-within-the-same-file/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { | ||
return; | ||
}); | ||
import { generate } from './index'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
describe('Should be able to generate models to files', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
const expectedRootDir = __dirname.includes('examples') | ||
? __dirname | ||
: path.resolve( | ||
__dirname, | ||
'./examples/generate-all-models-within-the-same-file/output' | ||
); | ||
const expectedFilePath = path.resolve(expectedRootDir, 'Root.java'); | ||
await generate(); | ||
expect(spy.mock.calls.length).toBeGreaterThanOrEqual(1); | ||
expect(spy.mock.calls[spy.mock.calls.length - 1]).toMatchSnapshot(); | ||
expect(fs.existsSync(expectedFilePath)); | ||
}); | ||
}); |
35 changes: 35 additions & 0 deletions
35
examples/generate-all-models-within-the-same-file/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { JavaFileGenerator } from '../../src'; | ||
import { promises as fsPromises } from 'fs'; | ||
|
||
const generator = new JavaFileGenerator(); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
email: { | ||
type: 'string', | ||
enum: ['example1@test.com', 'example2@test.com'], | ||
}, | ||
}, | ||
}; | ||
export async function generate(): Promise<void> { | ||
const outputFile = | ||
'./output'; | ||
const models = await generator.generate(jsonSchemaDraft7); | ||
const modelCode = models.map((outputModel) => { | ||
return outputModel.result; | ||
}); | ||
const imports = models.map((outputModel) => { | ||
return outputModel.dependencies; | ||
}); | ||
const uniqueImports = [...new Set(imports)]; | ||
const codeWithImports = `${uniqueImports.join('\n')} | ||
${modelCode.join('\n')}`; | ||
|
||
await fsPromises.writeFile(outputFile, codeWithImports); | ||
console.log(codeWithImports); | ||
} | ||
if (require.main === module) { | ||
generate(); | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/generate-all-models-within-the-same-file/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
examples/generate-all-models-within-the-same-file/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "generate-all-models-within-the-same-file" | ||
}, | ||
"scripts": { | ||
"install": "cd ../.. && npm i", | ||
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/generate-all-models-within-the-same-file/index.ts", | ||
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\generate-all-models-within-the-same-file\\index.ts", | ||
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/generate-all-models-within-the-same-file/index.spec.ts", | ||
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/generate-all-models-within-the-same-file/index.spec.ts" | ||
} | ||
} |