Array fix

master
Paulo Simão 2020-12-13 13:03:58 -03:00
parent c6cadabef3
commit 56344e64e5
2 changed files with 13 additions and 129 deletions

View File

@ -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)
}

View File

@ -18,6 +18,8 @@ func processGoClientOutput(api *API) {
import ( import (
"bytes" "bytes"
"errors"
"io/ioutil"
"encoding/json" "encoding/json"
"net/http" "net/http"
"time" "time"
@ -50,6 +52,15 @@ func invoke(m string, path string, bodyo interface{}) (*json.Decoder, error) {
if err != nil { if err != nil {
return nil, err 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) ret := json.NewDecoder(res.Body)
return ret, nil 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("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(%sret)
if err != nil{ if err != nil{
return nil,err return nil,err
} }
return ret, err return ret, err
}`, m.Verb, m.Path, APIParamTypeToString(m.ResType))) }`, m.Verb, m.Path, APIParamTypeToString(m.ResType), APIParamTypeUseRef(m.ResType)))
b.WriteString("\n") b.WriteString("\n")
//} //}