-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
69 lines (56 loc) · 1.25 KB
/
handler.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
package rest
import (
"regexp"
"runtime"
"strings"
"github.com/labstack/echo/v4"
)
type Interactor interface {
Input() any
Output() any
Interact(c echo.Context, in, out any) error
Options() []option
Summary() string
}
type Handler[i, o any] struct {
handler interact[i, o]
options []option
summary string
}
type interact[i, o any] func(c echo.Context, in i, out *o) error
func getSummary() string {
counter, _, _, success := runtime.Caller(2)
if !success {
return ""
}
name := strings.Split(runtime.FuncForPC(counter).Name(), ".")
return camelRegexp(name[len(name)-1])
}
func camelRegexp(str string) string {
re := regexp.MustCompile(`([A-Z]+)`)
str = re.ReplaceAllString(str, ` $1`)
str = strings.Trim(str, " ")
return str
}
func NewHandler[i, o any](handler interact[i, o], ops ...option) Interactor {
return &Handler[i, o]{
handler: handler,
options: ops,
summary: getSummary(),
}
}
func (h *Handler[i, o]) Interact(c echo.Context, in, out any) error {
return h.handler(c, *in.(*i), out.(*o))
}
func (h *Handler[i, o]) Input() any {
return new(i)
}
func (h *Handler[i, o]) Output() any {
return new(o)
}
func (h *Handler[i, o]) Options() []option {
return h.options
}
func (h *Handler[i, o]) Summary() string {
return h.summary
}