forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
52 lines (41 loc) · 1021 Bytes
/
action.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
package doc
import (
"strings"
"text/template"
"github.com/adams-sarah/test2doc/doc/parse"
)
var (
actionTmpl *template.Template
actionFmt = `### {{.Title}} [{{.Method}}]
{{.Description}}{{range $req, $resp := .Requests}}
{{with $req}}{{.Render}}{{end}}
{{with $resp}}{{.Render}}{{end}}{{end}}`
)
func init() {
actionTmpl = template.Must(template.New("action").Parse(actionFmt))
}
type Action struct {
Title string
Description string
Method HTTPMethod
Requests map[*Request]*Response
}
func (a *Action) Render() string {
return render(actionTmpl, a)
}
func NewAction(method, handlerName string) (*Action, error) {
title := parse.GetTitle(handlerName)
if len(title) == 0 {
title = strings.Title(method)
}
desc := parse.GetDescription(handlerName)
return &Action{
Title: title,
Description: desc,
Method: HTTPMethod(method),
Requests: map[*Request]*Response{},
}, nil
}
func (a *Action) AddRequest(req *Request, resp *Response) {
a.Requests[req] = resp
}