added array to params

master
Paulo Simão 2020-12-12 09:21:12 -03:00
parent 65facd7a02
commit 3d72f44205
12 changed files with 138 additions and 118 deletions

View File

@ -110,7 +110,22 @@ func addFunction(a *ast.FuncDecl) {
case *ast.SelectorExpr: case *ast.SelectorExpr:
reqType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name reqType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
} }
case *ast.ArrayType:
reqType.IsArray = true
switch y := x.Elt.(type) {
case *ast.Ident:
reqType.Typename = y.Name
case *ast.SelectorExpr:
reqType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
case *ast.StarExpr:
reqType.Ispointer = true
switch z := y.X.(type) {
case *ast.Ident:
reqType.Typename = z.Name
case *ast.SelectorExpr:
reqType.Typename = z.X.(*ast.Ident).Name + "." + z.Sel.Name
}
}
case *ast.Ident: case *ast.Ident:
reqType.Typename = x.Name reqType.Typename = x.Name
} }
@ -127,12 +142,25 @@ func addFunction(a *ast.FuncDecl) {
case *ast.SelectorExpr: case *ast.SelectorExpr:
resType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name resType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
} }
case *ast.ArrayType:
resType.IsArray = true
switch y := x.Elt.(type) {
case *ast.Ident: case *ast.Ident:
resType.Typename = x.Name resType.Typename = y.Name
case *ast.SelectorExpr:
resType.Typename = y.X.(*ast.Ident).Name + "." + y.Sel.Name
case *ast.StarExpr:
resType.Ispointer = true
switch z := y.X.(type) {
case *ast.Ident:
resType.Typename = z.Name
case *ast.SelectorExpr:
resType.Typename = z.X.(*ast.Ident).Name + "." + z.Sel.Name
} }
} }
}
if md["RAW"] == "true" { if md["RAW"] == "true" {
reqType.Typename = md["REQ"] reqType.Typename = md["REQ"]
resType.Typename = md["RES"] resType.Typename = md["RES"]
@ -152,29 +180,9 @@ func addFunction(a *ast.FuncDecl) {
Raw: md["RAW"] == "true", Raw: md["RAW"] == "true",
} }
api.Methods[a.Name.Name] = &fn api.Methods[a.Name.Name] = &fn
} }
//func processHTTPTestOutput(f string, api *API) { }
//
// for k, m := range api.Methods {
// fname := path.Join(httptestdir, k+".http")
// if dc.FileExists(fname) {
// continue
// }
// b := bytes.Buffer{}
// b.WriteString(fmt.Sprintf(`%s{{BASEURL}}%s
//Content-Type: application/json
//Cookie: dc={{COOKIE}}
//
//{}
//
//###
//`, m.Verb, api.BasePath+m.Path))
//
// err := ioutil.WriteFile(fname, b.Bytes(), 0600)
// dc.Err(err)
// }
//}
func mapHttp(api *API) { func mapHttp(api *API) {
for k, v := range api.Methods { for k, v := range api.Methods {

View File

@ -80,15 +80,16 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
for k, m := range api.Methods { for k, m := range api.Methods {
b.WriteString(fmt.Sprintf("func %s(req *%s) (res *%s, err error){\n", k, m.ReqType.Typename, m.ResType.Typename)) b.WriteString(fmt.Sprintf("func %s(req %s) (res %s, err error){\n", k, APIParamTypeDecToString(m.ReqType), APIParamTypeDecToString(m.ResType)))
b.WriteString(fmt.Sprintf(` dec, err := invoke("%s", "%s", req) b.WriteString(fmt.Sprintf(` dec, err := invoke("%s", "%s", req)
ret := &%s{} ret := %s{}
err = dec.Decode(ret) err = dec.Decode(ret)
if err != nil{ if err != nil{
return nil,err return nil,err
} }
return ret, err return ret, err
}`, m.Verb, m.Path, m.ResType.Typename)) }`, m.Verb, m.Path, APIParamTypeToString(m.ResType)))
b.WriteString("\n")
//} //}
} }

View File

@ -12,15 +12,6 @@ import (
func processGoServerOutput(api *API) { func processGoServerOutput(api *API) {
APIParamTypeToString := func(t *APIParamType) string {
ret := ""
if t.Ispointer {
ret = ret + "&"
}
ret = ret + t.Typename
return ret
}
b := bytes.Buffer{} b := bytes.Buffer{}
f := config.Gofname f := config.Gofname

View File

@ -9,6 +9,14 @@ import (
) )
func processTSClientOutput(f string, api *API) { func processTSClientOutput(f string, api *API) {
_typeName := func(m *APIParamType) string {
if m.IsArray {
return m.Typename + "[]"
}
return m.Typename
}
b := bytes.Buffer{} b := bytes.Buffer{}
if f == "" { if f == "" {
f = config.Tsfname f = config.Tsfname
@ -137,15 +145,8 @@ async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise<b
if m.Desc != "" { if m.Desc != "" {
b.WriteString(fmt.Sprintf("/**\n%s*/\n", m.Desc)) b.WriteString(fmt.Sprintf("/**\n%s*/\n", m.Desc))
} }
//if m.Raw {
// { b.WriteString(fmt.Sprintf("export async function %s(req:%s):Promise<%s>{\n", k, _typeName(m.ReqType), _typeName(m.ResType)))
// b.WriteString(fmt.Sprintf("export async function API_%s(req:any):Promise<any>{\n", k, m.ReqType))
// b.WriteString(fmt.Sprintf("\treturn InvokeJSON(\"%s\",\"%s\",req)\n", m.Path, m.Verb))
// b.WriteString(fmt.Sprintf("}\n\n"))
// }
//
//} else {
b.WriteString(fmt.Sprintf("export async function %s(req:%s):Promise<%s>{\n", k, m.ReqType.Typename, m.ResType.Typename))
b.WriteString(fmt.Sprintf("\treturn InvokeJSON(\"%s\",\"%s\",req)\n", m.Path, m.Verb)) b.WriteString(fmt.Sprintf("\treturn InvokeJSON(\"%s\",\"%s\",req)\n", m.Path, m.Verb))
b.WriteString(fmt.Sprintf("}\n\n")) b.WriteString(fmt.Sprintf("}\n\n"))
//} //}

View File

@ -25,6 +25,7 @@ type APIType struct {
type APIParamType struct { type APIParamType struct {
Typename string Typename string
Ispointer bool Ispointer bool
IsArray bool
} }
type APIMethod struct { type APIMethod struct {
@ -36,3 +37,37 @@ type APIMethod struct {
ResType *APIParamType ResType *APIParamType
Raw bool `yaml:"raw"` Raw bool `yaml:"raw"`
} }
func APIParamTypeToString(t *APIParamType) string {
ret := ""
if t.IsArray {
ret = "[]"
if t.Ispointer {
ret = ret + "*"
}
ret = ret + t.Typename
return ret
}
if t.Ispointer {
ret = ret + "&"
}
ret = ret + t.Typename
return ret
}
func APIParamTypeDecToString(t *APIParamType) string {
ret := ""
if t.IsArray {
ret = "[]"
if t.Ispointer {
ret = ret + "*"
}
ret = ret + t.Typename
return ret
}
if t.Ispointer {
ret = ret + "*"
}
ret = ret + t.Typename
return ret
}

View File

@ -27,7 +27,7 @@ func Init() API {
mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) { mux.HandleFunc("/someapi", func(w http.ResponseWriter, r *http.Request) {
switch r.Method { switch r.Method {
case "POST": case "POST":
h_SomeAPI(w, r) h_SomeAPI2(w, r)
default: default:
http.Error(w, "Method not allowed", 500) http.Error(w, "Method not allowed", 500)
} }
@ -35,12 +35,12 @@ func Init() API {
return ret return ret
} }
func h_SomeAPI(w http.ResponseWriter, r *http.Request) { func h_SomeAPI2(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
ctx = context.WithValue(r.Context(), "REQ", r) ctx = context.WithValue(r.Context(), "REQ", r)
ctx = context.WithValue(ctx, "RES", w) ctx = context.WithValue(ctx, "RES", w)
req := &ARequestStruct{} req := &AStr{}
if r.Method != http.MethodGet && r.Method != http.MethodHead { if r.Method != http.MethodGet && r.Method != http.MethodHead {
err := json.NewDecoder(r.Body).Decode(&req) err := json.NewDecoder(r.Body).Decode(&req)
if err != nil { if err != nil {
@ -49,7 +49,7 @@ func h_SomeAPI(w http.ResponseWriter, r *http.Request) {
} }
} }
res, err := SomeAPI(ctx, req) res, err := SomeAPI2(ctx, req)
if err != nil { if err != nil {
http.Error(w, err.Error(), 500) http.Error(w, err.Error(), 500)
return return

View File

@ -2,29 +2,42 @@ package goapi
import ( import (
"context" "context"
"time"
) )
/*@API*/ //
type ARequestStruct struct { ///*@API*/
A *string `json:"a"` //type ARequestStruct struct {
B int64 // A *string `json:"a"`
C time.Time // B int64
D *string // C time.Time
} // D *string
//}
//
///*@API*/
//type AResponseStruct struct {
// A string
// B int64
// C time.Time
// D *string
//}
//
///*
//@API
//@PATH: /someapi
//*/
//func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) {
// return nil, nil
//}
/*@API*/ /*@API*/
type AResponseStruct struct { type AStr struct {
A string A string
B int64
C time.Time
D *string
} }
/* /*
@API @API
@PATH: /someapi @PATH: /someapi
*/ */
func SomeAPI(ctx context.Context, a *ARequestStruct) (*AResponseStruct, error) { func SomeAPI2(ctx context.Context, a *AStr) ([]*AStr, error) {
return nil, nil return nil, nil
} }

View File

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"net/http" "net/http"
"time"
) )
var Basepath string = "" var Basepath string = ""
@ -38,31 +37,13 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
return ret, nil return ret, nil
} }
func Some() (*struct{}, error) { type AStr struct {
dec, err := invoke("a", "/c/d", struct{}{})
ret := &struct{}{}
dec.Decode(ret)
return ret, err
}
type ARequestStruct struct {
A string `json:"a"` A string `json:"a"`
B int64 `json:"b"`
C time.Time `json:"c"`
D string `json:"d"`
} }
type AResponseStruct struct { func SomeAPI2(req *AStr) (res []*AStr, err error) {
A string `json:"a"`
B int64 `json:"b"`
C time.Time `json:"c"`
D string `json:"d"`
}
func SomeAPI(req *ARequestStruct) (res *AResponseStruct, err error) {
dec, err := invoke("POST", "/someapi", req) dec, err := invoke("POST", "/someapi", req)
ret := &AResponseStruct{} ret := []*AStr{}
err = dec.Decode(ret) err = dec.Decode(ret)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -81,26 +81,16 @@ async function InvokeOk(path: string, method: HTMLMethod, body?: any): Promise<b
//#endregion //#endregion
//#region Types //#region Types
export interface ARequestStruct { export interface AStr {
a:string a:string
b:number
c:Date
d:string
}
export interface AResponseStruct {
a:string
b:number
c:Date
d:string
} }
//#endregion //#endregion
//#region Methods //#region Methods
/** /**
SomeAPI*/ SomeAPI2*/
export async function SomeAPI(req:ARequestStruct):Promise<AResponseStruct>{ export async function SomeAPI2(req:AStr):Promise<AStr[]>{
return InvokeJSON("/someapi","POST",req) return InvokeJSON("/someapi","POST",req)
} }