Skip to content

Commit

Permalink
feat: finalized configuration (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Jun 20, 2024
1 parent 433da9f commit 9e2c9c4
Show file tree
Hide file tree
Showing 12 changed files with 185 additions and 108 deletions.
14 changes: 13 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"inquirer": "^8.2.6",
"supports-esm": "1.0.0",
"yaml": "^2.4.5",
"zod": "^3.23.8"
"zod": "^3.23.8",
"zod-validation-error": "^3.3.0"
},
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^1.0.2",
Expand Down
78 changes: 61 additions & 17 deletions schemas/configuration-schema-0.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@
"inputPath": {
"type": "string"
},
"language": {
"type": "string",
"enum": [
"typescript",
"java"
]
},
"generators": {
"type": "array",
"items": {
Expand All @@ -23,13 +30,15 @@
"type": "object",
"properties": {
"id": {
"type": "string"
"type": "string",
"default": "payloads-typescript"
},
"dependencies": {
"type": "array",
"items": {
"type": "string"
}
},
"default": []
},
"preset": {
"type": "string",
Expand All @@ -40,7 +49,8 @@
},
"serializationType": {
"type": "string",
"const": "json"
"const": "json",
"default": "json"
},
"language": {
"type": "string",
Expand All @@ -57,33 +67,36 @@
"type": "object",
"properties": {
"id": {
"type": "string"
"type": "string",
"default": "parameters-typescript"
},
"dependencies": {
"type": "array",
"items": {
"type": "string"
}
},
"default": []
},
"preset": {
"type": "string",
"const": "parameters"
},
"outputPath": {
"type": "string"
"type": "string",
"default": "src/__gen__/parameters"
},
"serializationType": {
"type": "string",
"const": "json"
"const": "json",
"default": "json"
},
"language": {
"type": "string",
"const": "typescript"
}
},
"required": [
"preset",
"outputPath"
"preset"
],
"additionalProperties": false
},
Expand All @@ -98,7 +111,11 @@
"type": "array",
"items": {
"type": "string"
}
},
"default": [
"parameters-typescript",
"payloads-typescript"
]
},
"preset": {
"type": "string",
Expand All @@ -124,10 +141,6 @@
"type": "string",
"description": "In case you have multiple TypeScript payload generators, you can specify which one to use as the dependency for this channels generator."
},
"serializationType": {
"type": "string",
"const": "json"
},
"language": {
"type": "string",
"const": "typescript"
Expand All @@ -152,12 +165,41 @@
"type": "string"
}
},
"preset": {
"type": "string",
"const": "custom"
}
},
"required": [
"preset"
],
"additionalProperties": false
},
{
"type": "object",
"properties": {
"id": {
"type": "string",
"default": "payloads-java"
},
"dependencies": {
"type": "array",
"items": {
"type": "string"
},
"default": []
},
"preset": {
"type": "string",
"const": "payloads"
},
"outputPath": {
"type": "string"
"type": "string",
"default": "./target/generated-sources/the/codegen/project"
},
"packageName": {
"type": "string",
"default": "the.codegen.project"
},
"serializationType": {
"type": "string",
Expand All @@ -169,10 +211,12 @@
}
},
"required": [
"preset",
"outputPath"
"preset"
],
"additionalProperties": false
},
{
"$ref": "#/definitions/AsyncAPICodegenConfiguration/properties/generators/items/anyOf/3"
}
]
}
Expand Down
14 changes: 10 additions & 4 deletions src/codegen/configuration-manager.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {
TheCodegenConfiguration,
LoadArgument,
zodTheCodegenConfiguration,
Generators
zodTheCodegenConfiguration
} from './types';
import {getDefaultConfiguration} from './generators/index';
import path from 'node:path';
import yaml from 'yaml';
import fs from 'fs';
import {Logger} from '../LoggingInterface';
import { fromError } from 'zod-validation-error';
import {includeTypeScriptChannelDependencies} from './generators/typescript/channels';
// eslint-disable-next-line @typescript-eslint/no-var-requires, no-undef
const supportsESM = require('supports-esm');
Expand Down Expand Up @@ -90,14 +90,20 @@ export function realizeConfiguration(
generator.preset,
language
);
let generatorToUse: Generators = generator;
let generatorToUse = generator;
if (defaultGenerator) {
generatorToUse = {...defaultGenerator, ...generator};
}
// eslint-disable-next-line security/detect-object-injection
config.generators[index] = generatorToUse;
}
zodTheCodegenConfiguration.parse(config);
try {
zodTheCodegenConfiguration.parse(config);
} catch (e) {
const validationError = fromError(e);
Logger.error(validationError.toString().split(';').join('\n'));
throw new Error("Not a valid configuration file");
}
const newGenerators = ensureProperGenerators(config);
config.generators.push(...newGenerators);
return config;
Expand Down
27 changes: 22 additions & 5 deletions src/codegen/generators/generic/custom.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import {AsyncAPIDocumentInterface} from '@asyncapi/parser';
import {GenericCodegenContext, GenericGeneratorOptions} from '../../types';
export interface CustomGenerator extends GenericGeneratorOptions {
preset: 'custom';
renderFunction: (context: CustomContext, options: any) => any;
options?: any;
}
import {z} from 'zod';

export interface CustomContext extends GenericCodegenContext {
inputType: 'asyncapi';
asyncapiDocument?: AsyncAPIDocumentInterface;
generator: CustomGenerator;
}

export interface CustomGenerator extends GenericGeneratorOptions {
preset: 'custom';
renderFunction: (context: CustomContext, options: any) => any;
options?: any;
}

export const zodCustomGenerator = z.object({
id: z.string().optional(),
dependencies: z.array(z.string()).optional(),
preset: z.literal('custom'),
renderFunction: z.function().args(
z.object({
inputType: z.enum(['asyncapi']),
generator: z.any(),
options: z.any()
}),
z.any()
).returns(z.boolean())
});

export const defaultCustomGenerator: CustomGenerator = {
preset: 'custom',
id: 'custom',
Expand Down
19 changes: 7 additions & 12 deletions src/codegen/generators/java/payloads.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import {JAVA_JACKSON_PRESET, JavaFileGenerator} from '@asyncapi/modelina';
import {Logger} from '../../../LoggingInterface';
import {GenericCodegenContext, GenericGeneratorOptions} from '../../types';
import {GenericCodegenContext} from '../../types';
import {z} from 'zod';

export interface JavaPayloadGenerator extends GenericGeneratorOptions {
preset: 'payloads';
outputPath: string;
serializationType?: 'json';
packageName: string;
language?: 'java';
}

export const zodJavaPayloadGenerator = z.object({
id: z.string().optional(),
dependencies: z.array(z.string()).optional(),
id: z.string().optional().default('payloads-java'),
dependencies: z.array(z.string()).optional().default([]),
preset: z.literal('payloads'),
outputPath: z.string(),
outputPath: z.string().default('./target/generated-sources/the/codegen/project'),
packageName: z.string().default('the.codegen.project'),
serializationType: z.literal('json').optional(),
language: z.literal('java').optional()
});

export type JavaPayloadGenerator = z.infer<typeof zodJavaPayloadGenerator>;

export interface JavaPayloadContext extends GenericCodegenContext {
inputType: 'asyncapi';
documentPath: string;
Expand Down
15 changes: 4 additions & 11 deletions src/codegen/generators/typescript/channels/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable sonarjs/no-duplicate-string */
import {
GenericCodegenContext,
GenericGeneratorOptions,
TheCodegenConfiguration
} from '../../../types';
import {AsyncAPIDocumentInterface} from '@asyncapi/parser';
Expand All @@ -19,18 +19,10 @@ import {
} from '../payloads';
import {z} from 'zod';
export type SupportedProtocols = 'nats';
export interface TypeScriptChannelsGenerator extends GenericGeneratorOptions {
preset: 'channels';
outputPath: string;
language?: 'typescript';
payloadGeneratorId?: string;
parameterGeneratorId?: string;
protocols: SupportedProtocols[];
}

export const zodTypescriptChannelsGenerator = z.object({
id: z.string().optional().default('channels-typescript'),
dependencies: z.array(z.string()).optional(),
dependencies: z.array(z.string()).optional().default(['parameters-typescript', 'payloads-typescript']),
preset: z.literal('channels'),
outputPath: z.string(),
protocols: z.array(z.enum(['nats'])),
Expand All @@ -46,10 +38,11 @@ export const zodTypescriptChannelsGenerator = z.object({
.describe(
'In case you have multiple TypeScript payload generators, you can specify which one to use as the dependency for this channels generator.'
),
serializationType: z.literal('json').optional(),
language: z.literal('typescript').optional()
});

export type TypeScriptChannelsGenerator = z.infer<typeof zodTypescriptChannelsGenerator>;

export const defaultTypeScriptChannelsGenerator: TypeScriptChannelsGenerator = {
preset: 'channels',
language: 'typescript',
Expand Down
18 changes: 6 additions & 12 deletions src/codegen/generators/typescript/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,21 @@ import {Logger} from '../../../LoggingInterface';
import {AsyncAPIDocumentInterface} from '@asyncapi/parser';
import {
GenericCodegenContext,
GenericGeneratorOptions,
ParameterRenderType
} from '../../types';
import {z} from 'zod';

export interface TypescriptParametersGenerator extends GenericGeneratorOptions {
preset: 'parameters';
outputPath: string;
serializationType?: 'json';
language?: 'typescript';
}

export const zodTypescriptParametersGenerator = z.object({
id: z.string().optional(),
dependencies: z.array(z.string()).optional(),
id: z.string().optional().default('parameters-typescript'),
dependencies: z.array(z.string()).optional().default([]),
preset: z.literal('parameters'),
outputPath: z.string(),
serializationType: z.literal('json').optional(),
outputPath: z.string().default('src/__gen__/parameters'),
serializationType: z.literal('json').optional().default('json'),
language: z.literal('typescript').optional()
});

export type TypescriptParametersGenerator = z.infer<typeof zodTypescriptParametersGenerator>;

export const defaultTypeScriptParametersOptions: TypescriptParametersGenerator =
{
preset: 'parameters',
Expand Down
Loading

0 comments on commit 9e2c9c4

Please sign in to comment.