diff --git a/go.mod b/go.mod index 13b31e3..5910bed 100644 --- a/go.mod +++ b/go.mod @@ -6,8 +6,8 @@ require ( github.com/Masterminds/sprig/v3 v3.2.3 github.com/antlabs/deepcopy v0.0.7 github.com/antlabs/gstl v0.0.7 - github.com/antlabs/pcurl v0.0.8 - github.com/antlabs/tostruct v0.0.8 + github.com/antlabs/pcurl v0.0.9 + github.com/antlabs/tostruct v0.0.9 github.com/gobeam/stringy v0.0.5 github.com/guonaihong/clop v0.2.10 github.com/stretchr/testify v1.8.1 diff --git a/go.sum b/go.sum index 731b15b..b03f466 100644 --- a/go.sum +++ b/go.sum @@ -10,12 +10,12 @@ github.com/antlabs/deepcopy v0.0.7 h1:wPPZoeDfIsduFOrCiijfX0+piufLa71A1q/MHxErK3 github.com/antlabs/deepcopy v0.0.7/go.mod h1:NKjyST7/uPcO2IPUKykZxOdnxRBSt5SJzyj/SlPXz0s= github.com/antlabs/gstl v0.0.7 h1:4/WO7pQM/063Qijfoo2BJzS/cnvV0bh1SgsG3j21d7Q= github.com/antlabs/gstl v0.0.7/go.mod h1:lPp5aVibFgN2Hn5WuNKK5TT4dmwiVQ38+Y6Ab8LZ/kw= -github.com/antlabs/pcurl v0.0.8 h1:G7RiOtLpAA16MLD2x0+Gvy8gZoV2td01wdFLunmZBVk= -github.com/antlabs/pcurl v0.0.8/go.mod h1:08JEOd9AdW+BzGpYbkNOPfUw7oDpWVLRn2kdsNdZP0U= +github.com/antlabs/pcurl v0.0.9 h1:wVlEGE8RsCDxRzJndE8HgkO37yMnajoYv7Eia1j2Ag4= +github.com/antlabs/pcurl v0.0.9/go.mod h1:08JEOd9AdW+BzGpYbkNOPfUw7oDpWVLRn2kdsNdZP0U= github.com/antlabs/strsim v0.0.2 h1:R4qjokEegYTrw+fkcYj3/UndG9Cn136fH+fpw9TIz9k= github.com/antlabs/strsim v0.0.2/go.mod h1:95XAAF2dJK9IiZMc0Ue6H9t477/i6fvYoMoeey8sEnc= -github.com/antlabs/tostruct v0.0.8 h1:+QL4nIWHUhtUZj+xsyomtRGYwc7+hUR3k2cGBEBTIHs= -github.com/antlabs/tostruct v0.0.8/go.mod h1:43BW4S5x9CCLms/hmkNtZmajU2YZffB5A0lw0pAw7CA= +github.com/antlabs/tostruct v0.0.9 h1:aXan0AiOdmV+bJwPNGecAPSiDxpAsPFY9CYUHn0aRko= +github.com/antlabs/tostruct v0.0.9/go.mod h1:43BW4S5x9CCLms/hmkNtZmajU2YZffB5A0lw0pAw7CA= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.7.0 h1:P7DyGrkLbVDzcuqagPsSFnAwwljjhmB3qVF5wzmHOxE= github.com/bytedance/sonic v1.7.0/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZXU064P/U= diff --git a/http/http.go b/http/http.go index 85577e7..6dbf16c 100644 --- a/http/http.go +++ b/http/http.go @@ -37,6 +37,7 @@ type HTTP struct { Client bool `clop:"short;long" usage:"gen http client code"` Server bool `clop:"short;long" usage:"gen http server code"` Dir string `clop:"short;long" usage:"gen dir" default:"."` + Debug bool `clop:"long" usage:"debug mode"` } // 入口函数 @@ -105,6 +106,11 @@ func (h *HTTP) SubMain() { panic("req name is empty") } var query pyaml.Query + if hp.Debug { + fmt.Printf("resp:%#v\n", h.Resp.Body) + fmt.Printf("req.Query:%#v\n", h.Req.URL) + } + if len(h.Req.URL) > 0 { query.Name = h.Req.Name + "Query" if pos := strings.Index(h.Req.URL, "?"); pos != -1 { diff --git a/model/model.go b/model/model.go index c53f7fa..e79e052 100644 --- a/model/model.go +++ b/model/model.go @@ -71,10 +71,15 @@ type Template struct { URL bool `yaml:"url"` } +type UsePtr struct { + Body []string `yaml:"body"` +} + type Req struct { Encode Encode `yaml:"encode"` URL string `yaml:"url"` Curl string `yaml:"curl"` + UsePtr UsePtr `yaml:"usePtr"` Template Template `yaml:"template"` Name string `yaml:"name"` NewType map[string]string `yaml:"newType"` diff --git a/pyaml/gen.go b/pyaml/gen.go index 17167c4..16a34be 100644 --- a/pyaml/gen.go +++ b/pyaml/gen.go @@ -26,7 +26,10 @@ func GetBody(h model.Multi, isProtobuf bool) (reqBody Body, defReqBody []model.K h.Req.Body, newReqType, h.Req.Encode, - h.Req.UseDefault.Body, isProtobuf) + h.Req.UseDefault.Body, + isProtobuf, + h.Req.UsePtr.Body, + ) if err != nil { fmt.Printf("get request body:%s\n", err) return @@ -37,7 +40,7 @@ func GetBody(h model.Multi, isProtobuf bool) (reqBody Body, defReqBody []model.K newRespType = h.Resp.NewProtobufType } - respBody, _, err = getBody(h.Resp.Name, h.Resp.Body, newRespType, model.Encode{}, nil, isProtobuf) + respBody, _, err = getBody(h.Resp.Name, h.Resp.Body, newRespType, model.Encode{}, nil, isProtobuf, nil) if err != nil { fmt.Printf("get response body:%s \n", err) all, _ := stdjson.Marshal(h.Resp.Body) @@ -64,7 +67,7 @@ func GetHeader(h model.Multi, opt ...option.OptionFunc) (reqHeader Header, defRe return } -func getBody(bodyName string, bodyData any, newType map[string]string, encode model.Encode, bodyDefKey []string, isProtobuf bool) ( +func getBody(bodyName string, bodyData any, newType map[string]string, encode model.Encode, bodyDefKey []string, isProtobuf bool, useBodyPtr []string) ( body Body, rvDefaultBody []model.KeyVal[string, string], err error) { @@ -88,24 +91,29 @@ func getBody(bodyName string, bodyData any, newType map[string]string, encode mo if isProtobuf { data, err = protobuf.Marshal(v, option.WithStructName(body.Name), - option.WithSpecifyType(newType)) + option.WithSpecifyType(newType), + option.WithUsePtrType(useBodyPtr), + ) } else { data, err = json.Marshal(v, option.WithStructName(body.Name), option.WithTagName(tagName), option.WithSpecifyType(newType), + option.WithUsePtrType(useBodyPtr), option.WithGetRawValue(getVal)) } case []any: if isProtobuf { data, err = protobuf.Marshal(v, option.WithStructName(body.Name), + option.WithUsePtrType(useBodyPtr), option.WithSpecifyType(newType), ) } else { data, err = json.Marshal(v, option.WithStructName(body.Name), option.WithTagName(tagName), + option.WithUsePtrType(useBodyPtr), option.WithSpecifyType(newType), option.WithGetRawValue(getVal)) } diff --git a/tmpl/dsl.tmpl b/tmpl/dsl.tmpl index f012373..3332886 100644 --- a/tmpl/dsl.tmpl +++ b/tmpl/dsl.tmpl @@ -15,8 +15,13 @@ init: multi: - handler: {{.StructName}}.Create{{.StructName}} req: - method: POST - url: https://host/port + # 必填字段,例 method: POST + method: + curl: >- + + # 必填字段 + # url: https://host/port + url: template: url: false # 直接使用配置文件里面的值