From d7feddec4e26d5dca81be4e311675efbabe0912a Mon Sep 17 00:00:00 2001 From: Egor Aristov Date: Sat, 6 Dec 2025 13:00:03 +0300 Subject: [PATCH] refactor --- example/ts/src/remote.ts | 6 +++--- kitcom/internal/ts/tsgen.go | 12 ++++++++++++ kitcom/internal/ts/tsgen.tmpl | 6 +----- lib/ts/src/lib.ts | 6 +----- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/example/ts/src/remote.ts b/example/ts/src/remote.ts index c384b56..a242814 100644 --- a/example/ts/src/remote.ts +++ b/example/ts/src/remote.ts @@ -11,13 +11,13 @@ export default class GoIpcApi { async Div(a: number, b: number): Promise { const results = await this.ipc.call("GoIpcApi.Div", a, b); - results[0] = this.ipc.convType(results[0], "number"); + return results[0] as number; } async XorData(data1: Buffer, data2: Buffer): Promise { const results = await this.ipc.call("GoIpcApi.XorData", data1, data2); - results[0] = this.ipc.convType(results[0], "Buffer"); - return results[0] as Buffer; + + return Buffer.from(results[0], "base64"); } } diff --git a/kitcom/internal/ts/tsgen.go b/kitcom/internal/ts/tsgen.go index d668bf2..7dbb932 100644 --- a/kitcom/internal/ts/tsgen.go +++ b/kitcom/internal/ts/tsgen.go @@ -43,6 +43,18 @@ func (g *TypescriptApiGenerator) Generate(apis *api.Api, destFile string) error } return td, nil }, + "convtype": func(valDef string, t types.ValType) (string, error) { + td, ok := map[types.ValType]string{ + types.TInt: fmt.Sprintf("%s as number", valDef), + types.TString: fmt.Sprintf("%s as string", valDef), + types.TBool: fmt.Sprintf("%s as boolean", valDef), + types.TBlob: fmt.Sprintf("Buffer.from(%s, 'base64')", valDef), + }[t] + if !ok { + return "", fmt.Errorf("cannot convert type %v for val %s", t, valDef) + } + return td, nil + }, }) tpl = template.Must(tpl.Parse(templateString)) diff --git a/kitcom/internal/ts/tsgen.tmpl b/kitcom/internal/ts/tsgen.tmpl index 61bf2c2..909e1eb 100644 --- a/kitcom/internal/ts/tsgen.tmpl +++ b/kitcom/internal/ts/tsgen.tmpl @@ -20,11 +20,7 @@ export default class {{ $e.Name }} { {{ range $par := $mtd.Params }}{{ $par.Name }}, {{ end }} ); - {{- range $i, $ret := $mtd.Ret -}} - results[{{ $i }}] = this.ipc.convType(results[{{ $i }}], '{{ $ret.Type | typedef }}'); - {{- end -}} - - return {{ range $i, $ret := $mtd.Ret }}{{ if $i }}, {{ end }}results[{{ $i }}] as {{ $ret.Type | typedef }}{{ end }} + return {{ range $i, $ret := $mtd.Ret }}{{ if $i }}, {{ end }}{{ convtype (printf "results[%d]" $i) $ret.Type }}{{ end }} } {{ end }} } diff --git a/lib/ts/src/lib.ts b/lib/ts/src/lib.ts index 60392be..ae5f75f 100644 --- a/lib/ts/src/lib.ts +++ b/lib/ts/src/lib.ts @@ -192,14 +192,10 @@ abstract class IPCCommon { }); } - public convType(arg: any, toType?: string): any { + public convType(arg: any): any { // noinspection FallThroughInSwitchStatementJS switch (typeof arg) { case 'string': - if(toType === 'Buffer') { - return Buffer.from(arg, 'base64'); - } - return arg; case 'boolean': case 'number': return arg;