forked from asyncapi/modelina
-
Notifications
You must be signed in to change notification settings - Fork 0
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
b29ae95
commit 6f47a2e
Showing
2 changed files
with
75 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
openapi: 3.0.1 | ||
info: | ||
title: Test | ||
paths: | ||
/example/api: | ||
post: | ||
requestBody: | ||
content: | ||
application/json: | ||
schema: | ||
$ref: '#/components/schemas/Request' | ||
required: true | ||
responses: | ||
200: | ||
description: success | ||
components: | ||
schemas: | ||
Request: | ||
type: object | ||
required: ['inputVariable'] | ||
properties: | ||
inputVariable: | ||
type: array | ||
items: | ||
$ref: '#/components/schemas/Variable' | ||
Variable: | ||
type: object | ||
properties: | ||
v: | ||
type: 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 |
---|---|---|
@@ -1,22 +1,48 @@ | ||
import { JavaScriptGenerator } from '../../src'; | ||
import { parse } from 'yaml' | ||
import { RustFileGenerator, RustRenderCompleteModelOptions, RUST_COMMON_PRESET, defaultRustRenderCompleteModelOptions, RustPackageFeatures } from '../../src/generators'; | ||
|
||
const generator = new JavaScriptGenerator(); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
email: { | ||
type: 'string', | ||
format: 'email' | ||
} | ||
} | ||
}; | ||
import * as fs from 'node:fs/promises'; | ||
import * as path from 'node:path'; | ||
|
||
function replaceExt(file: string, ext: string) : string { | ||
const basename = path.basename(file, path.extname(file)) | ||
return path.join(path.dirname(file), basename + ext) | ||
} | ||
|
||
export async function generate(file: string): Promise<void> { | ||
const spec = parse(await fs.readFile(file, 'utf8')); | ||
|
||
export async function generate() : Promise<void> { | ||
const models = await generator.generate(jsonSchemaDraft7); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
const generator = new RustFileGenerator({ | ||
presets: [ | ||
{ | ||
preset: RUST_COMMON_PRESET, options: { | ||
implementNew: true, | ||
implementDefault: true, | ||
} | ||
} | ||
] | ||
}); | ||
|
||
const outDir = path.join(__dirname, 'rust/' + replaceExt(file, '')); | ||
|
||
const models = await generator.generateToPackage(spec, outDir, { | ||
...defaultRustRenderCompleteModelOptions, | ||
supportFiles: true, // generate Cargo.toml and lib.rs | ||
package: { | ||
packageName: 'asyncapi-rs-example', | ||
packageVersion: '1.0.0', | ||
// set authors, homepage, repository, and license | ||
authors: ['AsyncAPI Rust Champions'], | ||
homepage: 'https://www.asyncapi.com/tools/modelina', | ||
repository: 'https://github.com/asyncapi/modelina', | ||
license: 'Apache-2.0', | ||
description: 'Rust models generated by AsyncAPI Modelina', | ||
// support 2018 editions and up | ||
edition: '2018', | ||
// enable serde_json | ||
packageFeatures: [RustPackageFeatures.json] as RustPackageFeatures[] | ||
} | ||
} as RustRenderCompleteModelOptions); | ||
} | ||
generate(); | ||
|
||
generate('api_example.yml'); |