-
-
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.
feat: add generate example preset for JS (#629)
- Loading branch information
1 parent
5567586
commit c9980fc
Showing
13 changed files
with
358 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# JavaScript Data Models with example function | ||
|
||
A basic example of how to use Modelina and output a TypeScript class with an example function. | ||
|
||
|
||
## 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/javascript-generate-example/__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 ts data model with example function and should log expected output to console 1`] = ` | ||
Array [ | ||
"class Root { | ||
email; | ||
constructor(input) { | ||
if (input.hasOwnProperty('email')) { | ||
this.email = input.email; | ||
} | ||
} | ||
get email() { return this.email; } | ||
set email(email) { this.email = email; } | ||
example(){ | ||
const instance = new Root({}); | ||
instance.email = \\"string\\"; | ||
return instance; | ||
} | ||
}", | ||
] | ||
`; |
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 ts data model with example function', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
//Generate is called 2x, so even though we expect 1 model, we double it | ||
expect(spy.mock.calls.length).toEqual(2); | ||
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,32 @@ | ||
import { JavaScriptGenerator } from '../../src'; | ||
import { JS_COMMON_PRESET } from '../../src'; | ||
|
||
const generator = new JavaScriptGenerator({ | ||
presets: [ | ||
{ | ||
preset: JS_COMMON_PRESET, | ||
options: { | ||
example: true | ||
} | ||
} | ||
] | ||
}); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
email: { | ||
type: 'string', | ||
format: 'email' | ||
} | ||
} | ||
}; | ||
|
||
export async function generate(): Promise<void> { | ||
const models = await generator.generate(jsonSchemaDraft7); | ||
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,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "javascript-generate-example" | ||
}, | ||
"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
66 changes: 66 additions & 0 deletions
66
src/generators/javascript/presets/utils/ExampleFunction.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,66 @@ | ||
import { JavaScriptRenderer } from '../../JavaScriptRenderer'; | ||
import { CommonModel } from '../../../../models'; | ||
|
||
export function renderValueFromModel(model: CommonModel, renderer: JavaScriptRenderer): string | undefined { | ||
if (model.$ref !== undefined) { | ||
return `${renderer.nameType(model.$ref)}.example()`; | ||
} | ||
if (Array.isArray(model.type)) { | ||
if (model.type.length > 0) { | ||
return renderValueFromType(model.type[0], model, renderer); | ||
} | ||
return undefined; | ||
} | ||
return renderValueFromType(model.type, model, renderer); | ||
} | ||
|
||
export function renderValueFromType(modelType: string | undefined, model: CommonModel, renderer: JavaScriptRenderer): string | undefined { | ||
if (modelType === undefined) { | ||
return undefined; | ||
} | ||
switch (modelType) { | ||
case 'string': | ||
return '"string"'; | ||
case 'integer': | ||
case 'number': | ||
return '0'; | ||
case 'boolean': | ||
return 'true'; | ||
case 'array': { | ||
if (model.items === undefined) { | ||
return '[]'; | ||
} | ||
if (Array.isArray(model.items)) { | ||
const arrayValues = model.items.map((item) => { | ||
return renderValueFromModel(item, renderer); | ||
}); | ||
return `[${arrayValues.join(', ')}]`; | ||
} | ||
const arrayType = renderValueFromModel(model.items, renderer); | ||
return `[${arrayType}]`; | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
export default function renderExampleFunction({ renderer, model }: { | ||
renderer: JavaScriptRenderer, | ||
model: CommonModel | ||
}): string { | ||
const properties = model.properties || {}; | ||
const setProperties = []; | ||
for (const [propertyName, property] of Object.entries(properties)) { | ||
const formattedPropertyName = renderer.nameProperty(propertyName, property); | ||
const potentialRenderedValue = renderValueFromModel(property, renderer); | ||
if (potentialRenderedValue === undefined) { | ||
continue; | ||
} | ||
setProperties.push(` instance.${formattedPropertyName} = ${potentialRenderedValue};`); | ||
} | ||
const formattedModelName = renderer.nameType(model.$id); | ||
return `example(){ | ||
const instance = new ${formattedModelName}({}); | ||
${(setProperties.join('\n'))} | ||
return instance; | ||
}`; | ||
} |
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,40 @@ | ||
import { JavaScriptGenerator, JS_COMMON_PRESET } from '../../../../src/generators'; | ||
const doc = { | ||
$id: 'Test', | ||
type: 'object', | ||
additionalProperties: true, | ||
required: ['string prop'], | ||
properties: { | ||
'string prop': { type: 'string' }, | ||
numberProp: { type: 'number' }, | ||
objectProp: { type: 'object', $id: 'NestedTest', properties: { stringProp: { type: 'string' } } } | ||
}, | ||
patternProperties: { | ||
'^S(.?)test': { | ||
type: 'string' | ||
} | ||
}, | ||
}; | ||
describe('Example function generation', () => { | ||
test('should render example function for model', async () => { | ||
const generator = new JavaScriptGenerator({ | ||
presets: [ | ||
{ | ||
preset: JS_COMMON_PRESET, | ||
options: { | ||
example: true | ||
} | ||
} | ||
] | ||
}); | ||
const inputModel = await generator.process(doc); | ||
const testModel = inputModel.models['Test']; | ||
const nestedTestModel = inputModel.models['NestedTest']; | ||
|
||
const testClass = await generator.renderClass(testModel, inputModel); | ||
const nestedTestClass = await generator.renderClass(nestedTestModel, inputModel); | ||
|
||
expect(testClass.result).toMatchSnapshot(); | ||
expect(nestedTestClass.result).toMatchSnapshot(); | ||
}); | ||
}); |
69 changes: 69 additions & 0 deletions
69
test/generators/javascript/preset/__snapshots__/ExamplePreset.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,69 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Example function generation should render example function for model 1`] = ` | ||
"class Test { | ||
stringProp; | ||
numberProp; | ||
objectProp; | ||
additionalProperties; | ||
sTestPatternProperties; | ||
constructor(input) { | ||
this.stringProp = input.stringProp; | ||
if (input.hasOwnProperty('numberProp')) { | ||
this.numberProp = input.numberProp; | ||
} | ||
if (input.hasOwnProperty('objectProp')) { | ||
this.objectProp = input.objectProp; | ||
} | ||
} | ||
get stringProp() { return this.stringProp; } | ||
set stringProp(stringProp) { this.stringProp = stringProp; } | ||
get numberProp() { return this.numberProp; } | ||
set numberProp(numberProp) { this.numberProp = numberProp; } | ||
get objectProp() { return this.objectProp; } | ||
set objectProp(objectProp) { this.objectProp = objectProp; } | ||
get additionalProperties() { return this.additionalProperties; } | ||
set additionalProperties(additionalProperties) { this.additionalProperties = additionalProperties; } | ||
get sTestPatternProperties() { return this.sTestPatternProperties; } | ||
set sTestPatternProperties(sTestPatternProperties) { this.sTestPatternProperties = sTestPatternProperties; } | ||
example(){ | ||
const instance = new Test({}); | ||
instance.stringProp = \\"string\\"; | ||
instance.numberProp = 0; | ||
instance.objectProp = NestedTest.example(); | ||
return instance; | ||
} | ||
}" | ||
`; | ||
|
||
exports[`Example function generation should render example function for model 2`] = ` | ||
"class NestedTest { | ||
stringProp; | ||
additionalProperties; | ||
constructor(input) { | ||
if (input.hasOwnProperty('stringProp')) { | ||
this.stringProp = input.stringProp; | ||
} | ||
} | ||
get stringProp() { return this.stringProp; } | ||
set stringProp(stringProp) { this.stringProp = stringProp; } | ||
get additionalProperties() { return this.additionalProperties; } | ||
set additionalProperties(additionalProperties) { this.additionalProperties = additionalProperties; } | ||
example(){ | ||
const instance = new NestedTest({}); | ||
instance.stringProp = \\"string\\"; | ||
return instance; | ||
} | ||
}" | ||
`; |
Oops, something went wrong.