Skip to content

Commit

Permalink
支持dsl指定生成指针类型
Browse files Browse the repository at this point in the history
  • Loading branch information
guonaihong committed Mar 13, 2023
1 parent b368bbd commit a251b99
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
6 changes: 6 additions & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

// 入口函数
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
16 changes: 12 additions & 4 deletions pyaml/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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))
}
Expand Down
9 changes: 7 additions & 2 deletions tmpl/dsl.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
# 直接使用配置文件里面的值
Expand Down

0 comments on commit a251b99

Please sign in to comment.