From 5893ad898bba219eb67ae56741a1ddd9167f6acd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Tue, 7 Sep 2021 13:15:32 -0300 Subject: [PATCH] fixes --- lib/processGoClientOutput.go | 23 +++++-- lib/processGoServerOutput.go | 54 ++++++++-------- main.go | 9 ++- test/goapi/apigen.go | 72 ---------------------- test/goapi/test.go | 73 +++++++++++----------- test/gocli/apicli.go | 116 ----------------------------------- 6 files changed, 91 insertions(+), 256 deletions(-) delete mode 100644 test/goapi/apigen.go delete mode 100644 test/gocli/apicli.go diff --git a/lib/processGoClientOutput.go b/lib/processGoClientOutput.go index a38b9e1..d3a6ac5 100644 --- a/lib/processGoClientOutput.go +++ b/lib/processGoClientOutput.go @@ -3,6 +3,7 @@ package lib import ( "bytes" "fmt" + "log" "os" ) @@ -50,18 +51,34 @@ func processGoClientOutput(f string) error { } WNL("package %s", api.Namespace) + + for k, v := range api.UsedImportsFunctions { + log.Printf("Adding import from funcions: %s => %s", k, v) + WNL(`import "%s"`, k) + } + + for k, v := range api.UsedImportsTypes { + log.Printf("Adding import from types: %s => %s", k, v) + WNL(`import "%s"`, k) + } + WNL(`import ( "bytes" "errors" "io/ioutil" "encoding/json" "net/http" - "time" ) var Basepath string = "" var Host string = "" var ExtraHeaders map[string]string = make(map[string]string) +var cli *http.Client + +func SetCli(nc *http.Client) { + cli = nc +} + func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { b := &bytes.Buffer{} err := json.NewEncoder(b).Encode(bodyo) @@ -81,14 +98,12 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { req.Header.Set(k, v) } - cli := http.Client{} res, err := cli.Do(req) if err != nil { return nil, err } - defer res.Body.Close() if res.StatusCode >= 400 { bs, err := ioutil.ReadAll(res.Body) @@ -111,7 +126,7 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { W("[]") } if f.Map { - W("map[%s]%s", f.Mapkey, f.Mapval) + WNL("map[%s]%s", f.Mapkey, f.Mapval) } else { WNL(f.Type) } diff --git a/lib/processGoServerOutput.go b/lib/processGoServerOutput.go index bcad8d4..4f1ce23 100644 --- a/lib/processGoServerOutput.go +++ b/lib/processGoServerOutput.go @@ -9,13 +9,22 @@ import ( func processGoServerOutput(f string) error { buf := &bytes.Buffer{} - W := func(s string, p ...interface{}) { - buf.WriteString(fmt.Sprintf(s, p...)) - } + //W := func(s string, p ...interface{}) { + // buf.WriteString(fmt.Sprintf(s, p...)) + //} WNL := func(s string, p ...interface{}) { buf.WriteString(fmt.Sprintf(s+"\n", p...)) } + ResImplType := func(v *APIParamType) string { + ret := "" + if !v.IsArray || v.Ispointer { + ret = ret + "&" + } + ret += v.Typename + "{}" + return ret + } + WNL("package %s", api.Namespace) WNL(`import ( "context" @@ -25,17 +34,17 @@ func processGoServerOutput(f string) error { )`) for k := range api.UsedImportsFunctions { - W(`import "%s"`, k) + WNL(`import "%s"`, k) } WNL(`type API struct { - Mux *http.ServeMux - Perms map[string]string - } + Mux *http.ServeMux + Perms map[string]string +} - func (a *API) GetPerm(r *http.Request) string { - return a.Perms[r.Method+"_"+strings.Split(r.RequestURI, "?")[0]] - } +func (a *API) GetPerm(r *http.Request) string { + return a.Perms[r.Method+"_"+strings.Split(r.RequestURI, "?")[0]] +} `) WNL(`func Init() *API{ @@ -61,18 +70,17 @@ func processGoServerOutput(f string) error { } else { WNL(` h_%s(w,r)`, v1.Method.Name) } - - WNL(` default: - http.Error(w,"Method not allowed",500)`) - - WNL(` }`) } - WNL(` })`) + + WNL(` default: + http.Error(w,"Method not allowed",500) + }`) } + WNL(` })`) + WNL(` return ret - } -`) +}`) for _, v := range api.Methods { WNL(`func h_%s(w http.ResponseWriter, r *http.Request) { @@ -80,15 +88,7 @@ func processGoServerOutput(f string) error { ctx = context.WithValue(r.Context(), "REQ", r) ctx = context.WithValue(ctx, "RES", w)`, v.Name) - W(" var req ") - - if v.ReqType.IsArray { - W("[]") - } - if v.ReqType.Ispointer { - W("*") - } - WNL(v.ReqType.Typename) + WNL(" req := %s", ResImplType(v.ReqType)) WNL(` if r.Method!=http.MethodGet && r.Method!=http.MethodHead {`) diff --git a/main.go b/main.go index 27a215b..281df61 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,14 @@ package main -import "go.digitalcircle.com.br/tools/apigen/lib" +import ( + "fmt" + "go.digitalcircle.com.br/tools/apigen/lib" + "os" +) + +var Ver string = "Tue Sep 7 12:38:32 -03 2021" func main() { + os.Stdout.WriteString(fmt.Sprintf("Version: %s\n", Ver)) lib.Run() } diff --git a/test/goapi/apigen.go b/test/goapi/apigen.go deleted file mode 100644 index 812f154..0000000 --- a/test/goapi/apigen.go +++ /dev/null @@ -1,72 +0,0 @@ -package goapi - -import ( - "github.com/gin-gonic/gin" -) - -import "crypto" - -var perms map[string]string - -func init() { - perms = make(map[string]string) - - perms["POST_/someapi"] = "ASD" - - perms["DELETE_/someapi"] = "ASD" - - perms["GET_/someapi"] = "ASD" - - perms["PUT_/someapi"] = "ASD" - -} - -func GetPerm(c *gin.Context) string { - perm, ok := perms[c.Request.Method+"_"+c.Request.URL.Path] - if !ok { - return "" - } - return perm -} - -func Build(r *gin.Engine) *gin.Engine { - - r.DELETE("/someapi", func(c *gin.Context) { - var req *crypto.Hash - c.BindJSON(req) - res, err := SomeAPI2(c.Request.Context(), req) - if err != nil { - c.Error(err) - } - c.JSON(200, res) - }) - r.GET("/someapi", func(c *gin.Context) { - var req string - c.BindJSON(&req) - res, err := SomeGET(c.Request.Context(), req) - if err != nil { - c.Error(err) - } - c.JSON(200, res) - }) - r.POST("/someapi", func(c *gin.Context) { - var req string - c.BindJSON(&req) - res, err := SomeAPI(c.Request.Context(), req) - if err != nil { - c.Error(err) - } - c.JSON(200, res) - }) - r.PUT("/someapi", func(c *gin.Context) { - var req string - c.BindJSON(&req) - res, err := SomePUT(c.Request.Context(), req) - if err != nil { - c.Error(err) - } - c.JSON(200, res) - }) - - return r -} diff --git a/test/goapi/test.go b/test/goapi/test.go index e14aa8d..d20587e 100644 --- a/test/goapi/test.go +++ b/test/goapi/test.go @@ -25,42 +25,43 @@ type AStr struct { @PERM: ASD @VERB: POST */ -func SomeAPI(ctx context.Context, s string) (out string, err error) { - print("Got:" + s) - out = time.Now().String() + " - Hey Ya!" +func SomeAPI(ctx context.Context, s *AStr) (out *AStr, err error) { + //print("Got:" + s) + //out = time.Now().String() + " - Hey Ya!" return } -/* -@API -@PATH: /someapi -@PERM: ASD -@VERB: GET -*/ -func SomeGET(ctx context.Context, s string) (out string, err error) { - print("Got:" + s) - out = time.Now().String() + " - Hey Ya!" - return -} - -/* -@API -@PATH: /someapi -@PERM: ASD -@VERB: PUT -*/ -func SomePUT(ctx context.Context, s string) (out string, err error) { - print("Got:" + s) - out = time.Now().String() + " - Hey Ya!" - return -} - -/* -@API -@PATH: /someapi -@PERM: ASD -@VERB: DELETE -*/ -func SomeAPI2(ctx context.Context, s *crypto.Hash) ([]string, error) { - return nil, nil -} +// +///* +//@API +//@PATH: /someapi +//@PERM: ASD +//@VERB: GET +//*/ +//func SomeGET(ctx context.Context, s string) (out string, err error) { +// print("Got:" + s) +// out = time.Now().String() + " - Hey Ya!" +// return +//} +// +///* +//@API +//@PATH: /someapi +//@PERM: ASD +//@VERB: PUT +//*/ +//func SomePUT(ctx context.Context, s string) (out string, err error) { +// print("Got:" + s) +// out = time.Now().String() + " - Hey Ya!" +// return +//} +// +///* +//@API +//@PATH: /someapi +//@PERM: ASD +//@VERB: DELETE +//*/ +//func SomeAPI2(ctx context.Context, s *crypto.Hash) ([]string, error) { +// return nil, nil +//} diff --git a/test/gocli/apicli.go b/test/gocli/apicli.go deleted file mode 100644 index 0c63aec..0000000 --- a/test/gocli/apicli.go +++ /dev/null @@ -1,116 +0,0 @@ -package gocli - -import ( - "bytes" - "encoding/json" - "errors" - "io/ioutil" - "net/http" - "time" -) - -var Basepath string = "" -var Host string = "" -var ExtraHeaders map[string]string = make(map[string]string) - -func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { - - b := &bytes.Buffer{} - err := json.NewEncoder(b).Encode(bodyo) - if err != nil { - return nil, err - } - body := bytes.NewReader(b.Bytes()) - - req, err := http.NewRequest(m, Host+Basepath+path, body) - if err != nil { - return nil, err - } - - req.Header.Set("Content-type", "application/json") - for k, v := range ExtraHeaders { - req.Header.Set(k, v) - } - cli := http.Client{} - res, err := cli.Do(req) - if err != nil { - return nil, err - } - - if res.StatusCode >= 400 { - bs, err := ioutil.ReadAll(res.Body) - if err != nil { - panic(err) - } - return nil, errors.New(string(bs)) - } - - ret := json.NewDecoder(res.Body) - return ret, nil -} - -type ARequestStruct struct { - C time.Time `json:"c"` - D string `json:"d"` - A string `json:"a"` - B int64 `json:"b"` -} - -type AResponseStruct struct { - A string `json:"a"` - B int64 `json:"b"` - C time.Time `json:"c"` - D string `json:"d"` -} - -type AStr struct { - A string `json:"a"` - B []string `json:"b"` -} - -type AStr2 struct { - X string `json:"x"` - Y []string `json:"y"` - Z []AStr `json:"z"` - W map[string]AStr `json:"w"` -} - -func SomeAPI3(req *AStr) (res []*AStr, err error) { - var dec *json.Decoder - dec, err = invoke("POST", "/someapi3", req) - if err != nil { - return - } - ret := []*AStr{} - err = dec.Decode(&ret) - if err != nil { - return nil, err - } - return ret, err -} -func SomeAPI(req *AStr) (res []*AStr, err error) { - var dec *json.Decoder - dec, err = invoke("PUT", "/someapi", req) - if err != nil { - return - } - ret := []*AStr{} - err = dec.Decode(&ret) - if err != nil { - return nil, err - } - return ret, err -} -func SomeAPI2(req *AStr) (res []*AStr, err error) { - var dec *json.Decoder - dec, err = invoke("POST", "/someapi", req) - if err != nil { - return - } - ret := []*AStr{} - err = dec.Decode(&ret) - if err != nil { - return nil, err - } - return ret, err -}