refactor
This commit is contained in:
parent
e8a5154c5c
commit
d7feddec4e
@ -11,13 +11,13 @@ export default class GoIpcApi {
|
|||||||
|
|
||||||
async Div(a: number, b: number): Promise<number> {
|
async Div(a: number, b: number): Promise<number> {
|
||||||
const results = await this.ipc.call("GoIpcApi.Div", a, b);
|
const results = await this.ipc.call("GoIpcApi.Div", a, b);
|
||||||
results[0] = this.ipc.convType(results[0], "number");
|
|
||||||
return results[0] as number;
|
return results[0] as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
async XorData(data1: Buffer, data2: Buffer): Promise<Buffer> {
|
async XorData(data1: Buffer, data2: Buffer): Promise<Buffer> {
|
||||||
const results = await this.ipc.call("GoIpcApi.XorData", data1, data2);
|
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");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -43,6 +43,18 @@ func (g *TypescriptApiGenerator) Generate(apis *api.Api, destFile string) error
|
|||||||
}
|
}
|
||||||
return td, nil
|
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))
|
tpl = template.Must(tpl.Parse(templateString))
|
||||||
|
|
||||||
|
|||||||
@ -20,11 +20,7 @@ export default class {{ $e.Name }} {
|
|||||||
{{ range $par := $mtd.Params }}{{ $par.Name }}, {{ end }}
|
{{ range $par := $mtd.Params }}{{ $par.Name }}, {{ end }}
|
||||||
);
|
);
|
||||||
|
|
||||||
{{- range $i, $ret := $mtd.Ret -}}
|
return {{ range $i, $ret := $mtd.Ret }}{{ if $i }}, {{ end }}{{ convtype (printf "results[%d]" $i) $ret.Type }}{{ end }}
|
||||||
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 }}
|
|
||||||
}
|
}
|
||||||
{{ end }}
|
{{ end }}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -192,14 +192,10 @@ abstract class IPCCommon {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public convType(arg: any, toType?: string): any {
|
public convType(arg: any): any {
|
||||||
// noinspection FallThroughInSwitchStatementJS
|
// noinspection FallThroughInSwitchStatementJS
|
||||||
switch (typeof arg) {
|
switch (typeof arg) {
|
||||||
case 'string':
|
case 'string':
|
||||||
if(toType === 'Buffer') {
|
|
||||||
return Buffer.from(arg, 'base64');
|
|
||||||
}
|
|
||||||
return arg;
|
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
case 'number':
|
case 'number':
|
||||||
return arg;
|
return arg;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user