From c6cadabef3ec619494b39da868fdf30d3acdc709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Sima=CC=83o?= Date: Sun, 13 Dec 2020 12:23:09 -0300 Subject: [PATCH] fix --- src/processGoClientCmdlineOutput.go | 127 ++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 src/processGoClientCmdlineOutput.go diff --git a/src/processGoClientCmdlineOutput.go b/src/processGoClientCmdlineOutput.go new file mode 100644 index 0000000..e31a5f6 --- /dev/null +++ b/src/processGoClientCmdlineOutput.go @@ -0,0 +1,127 @@ +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) +}