-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
74 lines (66 loc) · 1.48 KB
/
template.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
69
70
71
72
73
74
package main
import (
"bytes"
_ "embed"
"strings"
"text/template"
)
//go:embed template.go.tpl
var ginTemplate string
type serviceDesc struct {
ServiceName string
ServiceFullName string
Metadata string
Methods []*methodDesc
MethodSets map[string]*methodDesc
}
type methodDesc struct {
// method
MethodName string
Num int
Request string
Reply string
Comment string
// http_rule
Path string
Method string
HasByte bool
HasFile bool
}
func (s *serviceDesc) execute() string {
s.MethodSets = make(map[string]*methodDesc)
for _, m := range s.Methods {
s.MethodSets[m.MethodName] = m
}
buf := new(bytes.Buffer)
tmpl, err := template.New("gin").Parse(strings.TrimSpace(ginTemplate))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return strings.Trim(buf.String(), "\r\n")
}
// initPathParams 转换参数路由 {xx} --> :xx
func (m *methodDesc) initPathParams() {
paths := strings.Split(m.Path, "/")
for i, p := range paths {
if len(p) > 0 && (p[0] == '{' && p[len(p)-1] == '}') {
paths[i] = ":" + p[1:len(p)-1]
} else if len(p) > 0 && p[0] == ':' {
paths[i] = p
}
}
m.Path = strings.Join(paths, "/")
}
// HasPathParams 是否包含路由参数
func (m *methodDesc) HasPathParams() bool {
paths := strings.Split(m.Path, "/")
for _, p := range paths {
if len(p) > 0 && (p[0] == '{' && p[len(p)-1] == '}' || p[0] == ':') {
return true
}
}
return false
}