forked from MasterJoyHunan/gengin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_query.go
68 lines (58 loc) · 1.45 KB
/
parse_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package pkg
import (
"fmt"
"strings"
"github.com/zeromicro/go-zero/tools/goctl/api/spec"
)
const req = "req"
type ParseRequestBody struct{}
func (p *ParseRequestBody) BuildParseRequestStr(requestName string, types []spec.Type) string {
if requestName == "" {
return ""
}
var sb strings.Builder
var structType spec.DefineStruct
for _, t := range types {
if t.Name() == requestName {
structType = t.(spec.DefineStruct)
break
}
}
sb.WriteString(p.from(structType))
sb.WriteString(p.header(structType))
sb.WriteString(p.uri(structType))
return sb.String()
}
func (p *ParseRequestBody) header(i spec.DefineStruct) string {
if p.hasTag(i, "header") {
return p.returnCode("ShouldBindHeader")
}
return ""
}
func (p *ParseRequestBody) uri(i spec.DefineStruct) string {
if p.hasTag(i, "path") {
return p.returnCode("ShouldBindUri")
}
return ""
}
func (p *ParseRequestBody) from(i spec.DefineStruct) string {
if p.hasTag(i, "form") || p.hasTag(i, "json") {
return p.returnCode("ShouldBind")
}
return ""
}
func (p *ParseRequestBody) hasTag(i spec.DefineStruct, needTag string) bool {
for _, m := range i.Members {
before, _, found := strings.Cut(m.Tag, ":")
if found && strings.HasSuffix(before, needTag) {
return true
}
}
return false
}
func (p *ParseRequestBody) returnCode(method string) string {
return fmt.Sprintf(` if err := c.%s(&%s); err != nil {
response.HandleResponse(c, nil, err)
return
}`, method, req) + "\n"
}