From 87ddc419b059c9c30f859623c353281002ab46b7 Mon Sep 17 00:00:00 2001 From: zhoupy Date: Wed, 12 Jun 2024 19:51:09 +0800 Subject: [PATCH 1/2] add encoder/decoder map generate --- generate.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/generate.ts b/generate.ts index 2266efa..0e94194 100644 --- a/generate.ts +++ b/generate.ts @@ -980,5 +980,18 @@ export function generate(schema: Schema, options?: Options): string { lines.push(`}`); lines.push(``); + lines.push(`${codeForEnumExport('encoder')}${ts(`{ [key: string]: Function }`)} = {`); + for (const def of schema.messages) { + lines.push(` ${def.name}: ${prefix}encode${def.name},`); + } + lines.push(`};`); + lines.push(''); + + lines.push(`${codeForEnumExport('decoder')}${ts(`{ [key: string]: Function }`)} = {`); + for (const def of schema.messages) { + lines.push(` ${def.name}: ${prefix}decode${def.name},`); + } + lines.push(`};`); + lines.push(''); return lines.join('\n'); } From 96be52b019c3d3ed7c7491793baca4898ba4a063 Mon Sep 17 00:00:00 2001 From: zhoupy Date: Fri, 14 Jun 2024 15:14:15 +0800 Subject: [PATCH 2/2] ts generate should add namespace --- generate.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/generate.ts b/generate.ts index 0e94194..d45fa6d 100644 --- a/generate.ts +++ b/generate.ts @@ -47,7 +47,9 @@ export function generate(schema: Schema, options?: Options): string { function codeForEnumExport(name: string): string { return es6 ? `export const ${name}` : `${pkg}.${name}`; } - + if (typescript) { + lines.push(`namespace ${schema.package} {`); + } for (const def of schema.enums) { const prefix = def.name + '_'; const items: string[] = []; @@ -993,5 +995,8 @@ export function generate(schema: Schema, options?: Options): string { } lines.push(`};`); lines.push(''); + if (typescript) { + lines.push(`}`); + } return lines.join('\n'); }