Compare commits
2 Commits
e7315c38ae
...
d50fb45547
| Author | SHA1 | Date | |
|---|---|---|---|
| d50fb45547 | |||
| a7f7c4bb29 |
@ -12,4 +12,9 @@ export default class GoIpcApi {
|
|||||||
const results = await this.ipc.call("GoIpcApi.Div", a, b);
|
const results = await this.ipc.call("GoIpcApi.Div", a, b);
|
||||||
return results[0] as number;
|
return results[0] as number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async XorData(data1: Uint8Array, data2: Uint8Array): Promise<Uint8Array> {
|
||||||
|
const results = await this.ipc.call("GoIpcApi.XorData", data1, data2);
|
||||||
|
return results[0] as Uint8Array;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -97,10 +97,13 @@ func (p *GoApiParser) parseFile(sourceFile string) ([]api.Endpoint, error) {
|
|||||||
if recvIdent.Name == endpoint.Name {
|
if recvIdent.Name == endpoint.Name {
|
||||||
var apiMethod api.Method
|
var apiMethod api.Method
|
||||||
apiMethod.Name = funcDecl.Name.Name
|
apiMethod.Name = funcDecl.Name.Name
|
||||||
for _, param := range funcDecl.Type.Params.List {
|
for i, param := range funcDecl.Type.Params.List {
|
||||||
apiPar, err := fieldToVal(param)
|
apiPar, err := fieldToVal(param, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parse parameter %s: %w", param.Names[0].Name, err)
|
return nil, fmt.Errorf("parse parameter %d for method %s: %w", i, apiMethod.Name, err)
|
||||||
|
}
|
||||||
|
if apiPar == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(param.Names) != 1 {
|
if len(param.Names) != 1 {
|
||||||
@ -108,19 +111,22 @@ func (p *GoApiParser) parseFile(sourceFile string) ([]api.Endpoint, error) {
|
|||||||
}
|
}
|
||||||
apiPar.Name = param.Names[0].Name
|
apiPar.Name = param.Names[0].Name
|
||||||
|
|
||||||
apiMethod.Params = append(apiMethod.Params, apiPar)
|
apiMethod.Params = append(apiMethod.Params, *apiPar)
|
||||||
}
|
}
|
||||||
for _, ret := range funcDecl.Type.Results.List {
|
for i, ret := range funcDecl.Type.Results.List {
|
||||||
apiRet, err := fieldToVal(ret)
|
apiRet, err := fieldToVal(ret, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("parse return value %s: %w", ret.Names[0].Name, err)
|
return nil, fmt.Errorf("parse return value %d for method %s: %w", i, apiMethod.Name, err)
|
||||||
|
}
|
||||||
|
if apiRet == nil {
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(ret.Names) > 0 {
|
if len(ret.Names) > 0 {
|
||||||
apiRet.Name = ret.Names[0].Name
|
apiRet.Name = ret.Names[0].Name
|
||||||
}
|
}
|
||||||
|
|
||||||
apiMethod.Ret = append(apiMethod.Ret, apiRet)
|
apiMethod.Ret = append(apiMethod.Ret, *apiRet)
|
||||||
}
|
}
|
||||||
endpoints[i].Methods = append(endpoints[i].Methods, apiMethod)
|
endpoints[i].Methods = append(endpoints[i].Methods, apiMethod)
|
||||||
}
|
}
|
||||||
@ -129,7 +135,7 @@ func (p *GoApiParser) parseFile(sourceFile string) ([]api.Endpoint, error) {
|
|||||||
return endpoints, nil
|
return endpoints, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func fieldToVal(param *ast.Field) (api.Val, error) {
|
func fieldToVal(param *ast.Field, returning bool) (*api.Val, error) {
|
||||||
var val api.Val
|
var val api.Val
|
||||||
switch paramType := param.Type.(type) {
|
switch paramType := param.Type.(type) {
|
||||||
case *ast.Ident:
|
case *ast.Ident:
|
||||||
@ -140,8 +146,14 @@ func fieldToVal(param *ast.Field) (api.Val, error) {
|
|||||||
val.Type = api.TString
|
val.Type = api.TString
|
||||||
case "bool":
|
case "bool":
|
||||||
val.Type = api.TBool
|
val.Type = api.TBool
|
||||||
|
case "error":
|
||||||
|
if returning {
|
||||||
|
return nil, nil
|
||||||
|
} else {
|
||||||
|
return nil, fmt.Errorf("errors are supported only as return types")
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return val, fmt.Errorf("parameter type %s is not supported yet", paramType.Name)
|
return nil, fmt.Errorf("parameter type %s is not supported yet", paramType.Name)
|
||||||
}
|
}
|
||||||
case *ast.ArrayType:
|
case *ast.ArrayType:
|
||||||
switch elementType := paramType.Elt.(type) {
|
switch elementType := paramType.Elt.(type) {
|
||||||
@ -150,11 +162,13 @@ func fieldToVal(param *ast.Field) (api.Val, error) {
|
|||||||
case "byte":
|
case "byte":
|
||||||
val.Type = api.TBlob
|
val.Type = api.TBlob
|
||||||
default:
|
default:
|
||||||
return val, fmt.Errorf("parameter type %s is not supported yet", elementType.Name)
|
return nil, fmt.Errorf("parameter type %s is not supported yet", elementType.Name)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return val, fmt.Errorf("parameter type %T is not supported yet", paramType)
|
return nil, fmt.Errorf("parameter type %T is not supported yet", elementType)
|
||||||
}
|
}
|
||||||
return val, nil
|
default:
|
||||||
|
return nil, fmt.Errorf("parameter type %T is not supported yet", paramType)
|
||||||
|
}
|
||||||
|
return &val, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@ -35,6 +35,7 @@ func (g *TypescriptApiGenerator) Generate(apis *api.Api, destFile string) error
|
|||||||
api.TInt: "number",
|
api.TInt: "number",
|
||||||
api.TString: "string",
|
api.TString: "string",
|
||||||
api.TBool: "boolean",
|
api.TBool: "boolean",
|
||||||
|
api.TBlob: "Uint8Array",
|
||||||
}[t]
|
}[t]
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", fmt.Errorf("cannot generate type %v", t)
|
return "", fmt.Errorf("cannot generate type %v", t)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user