From 56344e64e5bd3a341fa5efb44ed999495f93de89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Sun, 13 Dec 2020 13:03:58 -0300 Subject: [PATCH] Array fix --- src/processGoClientCmdlineOutput.go | 127 ---------------------------- src/processGoClientOutput.go | 15 +++- 2 files changed, 13 insertions(+), 129 deletions(-) delete mode 100644 src/processGoClientCmdlineOutput.go diff --git a/src/processGoClientCmdlineOutput.go b/src/processGoClientCmdlineOutput.go deleted file mode 100644 index e31a5f6..0000000 --- a/src/processGoClientCmdlineOutput.go +++ /dev/null @@ -1,127 +0,0 @@ -package main - -import ( - "bytes" - "fmt" - dc "go.digitalcircle.com.br/golib/base" - "io/ioutil" - "strings" -) - -func processGoClientCmdOutput(api *API) { - b := bytes.Buffer{} - f := config.Goclifname - fparts := strings.Split(f, "/") - pkg := fparts[len(fparts)-2] - - b.WriteString(fmt.Sprintf(`package %s - -import ( - "bytes" - "encoding/json" - "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 - } - ret := json.NewDecoder(res.Body) - return ret, nil -} - -`, pkg)) - - for k, v := range api.Types { - - b.WriteString(fmt.Sprintf("type %s struct {\n", k)) - - for kf, f := range v.Fields { - - ftype := f.Type - - if f.Array { - ftype = "[]" + ftype - } else if f.Map { - fm := f.Mapkey - fv := f.Mapval - ftype = "map[" + fm + "]" + fv - } - - b.WriteString(fmt.Sprintf("\t%s %s `json:\"%s\"` \n", strings.ToUpper(kf[:1])+strings.ToLower(kf[1:]), ftype, strings.ToLower(kf))) - } - - b.WriteString(fmt.Sprintf("}\n\n")) - } - - for k, m := range api.Methods { - - 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) - ret := %s{} - err = dec.Decode(%sret) - if err != nil{ - return nil,err - } - return ret, err -}`, m.Verb, m.Path, APIParamTypeToString(m.ResType), APIParamTypeUseRef(m.ResType))) - b.WriteString("\n") - //} - - } - pmap := make(map[string]interface{}) - for k, m := range api.Methods { - pparts := strings.Split(m.Path, "/") - root := pmap - for _, v := range pparts { - entry, ok := root[v].(map[string]interface{}) - if ok { - root = entry - } else { - entry = make(map[string]interface{}) - root[v] = entry - root = entry - } - } - - 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) - ret := %s{} - err = dec.Decode(%sret) - if err != nil{ - return nil,err - } - return ret, err -}`, m.Verb, m.Path, APIParamTypeToString(m.ResType), APIParamTypeUseRef(m.ResType))) - b.WriteString("\n") - //} - - } - - err := ioutil.WriteFile(f, b.Bytes(), 0600) - dc.Err(err) -} diff --git a/src/processGoClientOutput.go b/src/processGoClientOutput.go index fb7a60d..0c8bbb9 100644 --- a/src/processGoClientOutput.go +++ b/src/processGoClientOutput.go @@ -18,6 +18,8 @@ func processGoClientOutput(api *API) { import ( "bytes" + "errors" + "io/ioutil" "encoding/json" "net/http" "time" @@ -50,6 +52,15 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { 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 } @@ -83,12 +94,12 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) { 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) ret := %s{} - err = dec.Decode(ret) + err = dec.Decode(%sret) if err != nil{ return nil,err } return ret, err -}`, m.Verb, m.Path, APIParamTypeToString(m.ResType))) +}`, m.Verb, m.Path, APIParamTypeToString(m.ResType), APIParamTypeUseRef(m.ResType))) b.WriteString("\n") //}