-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8bcbd6
commit 737ade9
Showing
26 changed files
with
605 additions
and
136 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
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,17 @@ | ||
# TODO: Generate Java model from a TypeScript Types file using processor options | ||
|
||
This is a basic example of generating Java models using a Typescript type file as input and processor options. This can be extended to generating Go, C# and other supported language models. | ||
|
||
## 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 | ||
``` |
21 changes: 21 additions & 0 deletions
21
examples/java-from-typescript-type-with-options/__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,21 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to generate Java models from a TypeScript type file | ||
along with specific options and should log expected output to console 1`] = ` | ||
Array [ | ||
"public class OuterData { | ||
private Object inner; | ||
private String name; | ||
private Map<String, Object> additionalProperties; | ||
public Object getInner() { return this.inner; } | ||
public void setInner(Object inner) { this.inner = inner; } | ||
public String getName() { return this.name; } | ||
public void setName(String name) { this.name = name; } | ||
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } | ||
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; } | ||
}", | ||
] | ||
`; |
16 changes: 16 additions & 0 deletions
16
examples/java-from-typescript-type-with-options/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,16 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); | ||
import {generate} from './index'; | ||
|
||
describe(`Should be able to generate Java models from a TypeScript type file | ||
along with specific options`, () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
//GenerateWithOptions is called 4x, so even though we expect 1 model, we double it | ||
expect(spy.mock.calls.length).toEqual(4); | ||
expect(spy.mock.calls[1]).toMatchSnapshot(); | ||
}); | ||
}); | ||
|
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,31 @@ | ||
import { JavaGenerator } from '../../src'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
const generator = new JavaGenerator( | ||
{ | ||
processorOptions: { | ||
typescript: { | ||
compilerOptions: { | ||
strictNullChecks: true | ||
} | ||
} | ||
} | ||
} | ||
); | ||
|
||
const file = path.resolve(__dirname, './typescriptFile.ts'); | ||
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8'); | ||
|
||
export async function generate() : Promise<void> { | ||
const models = await generator.generate({ | ||
fileContents, | ||
baseFile: file | ||
}); | ||
|
||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
|
||
generate(); |
10 changes: 10 additions & 0 deletions
10
examples/java-from-typescript-type-with-options/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
examples/java-from-typescript-type-with-options/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,10 @@ | ||
{ | ||
"config" : { "example_name" : "java-from-typescript-type-with-options" }, | ||
"scripts": { | ||
"install": "cd ../.. && npm i", | ||
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", | ||
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", | ||
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", | ||
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts" | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/java-from-typescript-type-with-options/typescriptFile.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,10 @@ | ||
export interface InnerData { | ||
age: number; | ||
name: string; | ||
free: boolean; | ||
} | ||
|
||
export interface OuterData { | ||
inner: InnerData; | ||
name: string; | ||
} |
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 Java from a TypeScript Types file | ||
|
||
This is a basic example of generating Java models using a Typescript type file as input. This can be extended to generating Go, C# and other supported language models. | ||
|
||
## 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 | ||
``` |
24 changes: 24 additions & 0 deletions
24
examples/java-from-typescript-type/__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,24 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to generate Java model from a TypeScript type file and should log expected output to console 1`] = ` | ||
Array [ | ||
"public class InnerData { | ||
private Double age; | ||
private String name; | ||
private Boolean free; | ||
private Map<String, Object> additionalProperties; | ||
public Double getAge() { return this.age; } | ||
public void setAge(Double age) { this.age = age; } | ||
public String getName() { return this.name; } | ||
public void setName(String name) { this.name = name; } | ||
public Boolean getFree() { return this.free; } | ||
public void setFree(Boolean free) { this.free = free; } | ||
public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; } | ||
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; } | ||
}", | ||
] | ||
`; |
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,14 @@ | ||
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; }); | ||
import {generate} from './index'; | ||
|
||
describe('Should be able to generate Java model from a TypeScript type file', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
//Generate is called 4x, so even though we expect 1 model, we double it | ||
expect(spy.mock.calls.length).toEqual(4); | ||
expect(spy.mock.calls[1]).toMatchSnapshot(); | ||
}); | ||
}); |
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,21 @@ | ||
import { JavaGenerator } from '../../src'; | ||
import * as path from 'path'; | ||
import * as fs from 'fs'; | ||
|
||
const generator = new JavaGenerator(); | ||
|
||
const file = path.resolve(__dirname, './typescriptFile.ts'); | ||
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8'); | ||
|
||
export async function generate() : Promise<void> { | ||
const models = await generator.generate({ | ||
fileContents, | ||
baseFile: file | ||
}); | ||
|
||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
|
||
generate(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,10 @@ | ||
{ | ||
"config" : { "example_name" : "java-from-typescript-type" }, | ||
"scripts": { | ||
"install": "cd ../.. && npm i", | ||
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts", | ||
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts", | ||
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts", | ||
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/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,9 @@ | ||
export type Shape = { | ||
size: number; | ||
} | ||
|
||
export interface InnerData { | ||
age: number; | ||
name: string; | ||
free: boolean; | ||
} |
Oops, something went wrong.