-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgen_template.go
108 lines (89 loc) · 1.73 KB
/
gen_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package fastapi
import (
"fmt"
"github.com/funny/link"
)
type Service interface {
ServiceID() byte
NewRequest(byte) Message
NewResponse(byte) Message
HandleRequest(*link.Session, Message)
}
type Message interface {
ServiceID() byte
MessageID() byte
Identity() string
BinarySize() int
MarshalPacket([]byte)
UnmarshalPacket([]byte)
}
type EncodeError struct {
Message interface{}
}
func (encodeError EncodeError) Error() string {
return fmt.Sprintf("Encode Error: %v", encodeError.Message)
}
type DecodeError struct {
Message interface{}
}
func (decodeError DecodeError) Error() string {
return fmt.Sprintf("Decode Error: %v", decodeError.Message)
}
var appTemplate = `
//
// THIS FILE IS GENERATED BY fastapi
// DO NOT MODIFY BY MANUAL
//
package {{Package}}
import (
"github.com/funny/link"
"github.com/funny/fastapi"
)
{{range .Imports}}
import {{.Name}} "{{.Path}}"
{{end}}
{{range .Services}}
func (_ *{{.Name}}) ServiceID() byte {
return {{.ID}}
}
func (_ *{{.Name}}) NewRequest(id byte) (fastapi.Message) {
switch id {
{{range .Requests}}
case {{.ID}}:
return &{{.Name}}{}
{{end}}
}
return nil
}
func (_ *{{.Name}}) NewResponse(id byte) (fastapi.Message) {
switch id {
{{range .Responses}}
case {{.ID}}:
return &{{.Name}}{}
{{end}}
}
return nil
}
func (s *{{.Name}}) HandleRequest(session *link.Session, req fastapi.Message) {
switch req.MessageID() {
{{range .Handlers}}
case {{.ID}}:
{{.InvokeCode}}
{{end}}
default:
panic("Unhandled Message Type")
}
}
{{end}}
{{range .Messages}}
func (this *{{.Name}}) ServiceID() byte {
return {{.Service.ID}}
}
func (this *{{.Name}}) MessageID() byte {
return {{.ID}}
}
func (this *{{.Name}}) Identity() string {
return "{{.Service.Name}}.{{.Name}}"
}
{{end}}
`