-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathgocodegen.go
165 lines (147 loc) · 5.28 KB
/
gocodegen.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"fmt"
"strings"
)
func (bc *GenerousContext) GenerateGoCode() error {
bc.Task("Generating go code")
doc := bc.NewGenerousRelativeDoc("../messages/messages.go")
doc.Line("// Code generated by generous; DO NOT EDIT.")
doc.Line("")
doc.Line("package messages")
doc.Line("")
doc.Line("import (")
doc.Line(" %q", "encoding/json")
doc.Line(" %q", "github.com/pkg/errors")
doc.Line("")
doc.Line(" %q", "github.com/itchio/butler/butlerd")
doc.Line(" %q", "github.com/sourcegraph/jsonrpc2")
doc.Line(")")
doc.Line("")
doc.Line("type router interface {")
doc.Line(" Register(method string, hf butlerd.RequestHandler)")
doc.Line(" RegisterNotification(method string, nf butlerd.NotificationHandler)")
doc.Line("}")
doc.Line("")
scope := newScope(bc)
must(scope.Assimilate("github.com/itchio/butler/butlerd", "types.go"))
var clientRequests []string
for _, category := range scope.categoryList {
cat := scope.categories[category]
doc.Line("")
doc.Line("//==============================")
doc.Line("// %s", category)
doc.Line("//==============================")
doc.Line("")
for _, entry := range cat.entries {
switch entry.kind {
case EntryKindParams:
ts := asType(entry.gd)
varName := strings.TrimSuffix(ts.Name.Name, "Params")
typeName := varName + "Type"
paramsTypeName := fmt.Sprintf("butlerd.%s", ts.Name.Name)
resultTypeName := fmt.Sprintf("butlerd.%sResult", strings.TrimSuffix(ts.Name.Name, "Params"))
method := entry.name
if entry.caller == CallerClient {
clientRequests = append(clientRequests, method)
}
doc.Line("// %s (Request)", method)
doc.Line("")
doc.Line("type %s struct {}", typeName)
doc.Line("")
doc.Line("var _ RequestMessage = (*%s)(nil)", typeName)
doc.Line("")
doc.Line("func (r *%s) Method() string {", typeName)
doc.Line(" return %#v", method)
doc.Line("}")
{
helperPrefix := ""
if entry.caller == CallerServer {
helperPrefix = "Test"
}
doc.Line("")
doc.Line("func (r *%s) %sRegister(router router, f func(*butlerd.RequestContext, %s) (*%s, error)) {", typeName, helperPrefix, paramsTypeName, resultTypeName)
doc.Line(" router.Register(%#v, func (rc *butlerd.RequestContext) (interface{}, error) {", method)
doc.Line(" var params %s", paramsTypeName)
doc.Line(" err := json.Unmarshal(*rc.Params, ¶ms)")
doc.Line(" if err != nil {")
doc.Line(" return nil, &butlerd.RpcError{Code: jsonrpc2.CodeParseError, Message: err.Error()}")
doc.Line(" }")
doc.Line(" err = params.Validate()")
doc.Line(" if err != nil {")
doc.Line(" return nil, err")
doc.Line(" }")
doc.Line(" res, err := f(rc, params)")
doc.Line(" if err != nil {")
doc.Line(" return nil, err")
doc.Line(" }")
doc.Line(" if res == nil {")
doc.Line(" return nil, errors.New(%#v)", fmt.Sprintf("internal error: nil result for %s", method))
doc.Line(" }")
doc.Line(" return res, nil")
doc.Line(" })")
doc.Line("}")
}
{
helperPrefix := ""
if entry.caller == CallerClient {
helperPrefix = "Test"
}
doc.Line("")
doc.Line("func (r *%s) %sCall(rc *butlerd.RequestContext, params %s) (*%s, error) {", typeName, helperPrefix, paramsTypeName, resultTypeName)
doc.Line(" var result %s", resultTypeName)
doc.Line(" err := rc.Call(%#v, params, &result)", method)
doc.Line(" return &result, err")
doc.Line("}")
}
doc.Line("")
doc.Line("var %s *%s", varName, typeName)
doc.Line("")
case EntryKindNotification:
ts := asType(entry.gd)
varName := strings.TrimSuffix(ts.Name.Name, "Notification")
typeName := varName + "Type"
paramsTypeName := fmt.Sprintf("butlerd.%s", ts.Name.Name)
method := entry.name
doc.Line("// %s (Notification)", method)
doc.Line("")
doc.Line("type %s struct {}", typeName)
doc.Line("")
doc.Line("var _ NotificationMessage = (*%s)(nil)", typeName)
doc.Line("")
doc.Line("func (r *%s) Method() string {", typeName)
doc.Line(" return %#v", method)
doc.Line("}")
doc.Line("")
doc.Line("func (r *%s) Notify(rc *butlerd.RequestContext, params %s) (error) {", typeName, paramsTypeName)
doc.Line(" return rc.Notify(%#v, params)", method)
doc.Line("}")
doc.Line("")
doc.Line("func (r *%s) Register(router router, f func(*butlerd.RequestContext, %s)) {", typeName, paramsTypeName)
doc.Line(" router.RegisterNotification(%#v, func (rc *butlerd.RequestContext) {", method)
doc.Line(" var params %s", paramsTypeName)
doc.Line(" err := json.Unmarshal(*rc.Params, ¶ms)")
doc.Line(" if err != nil {")
doc.Line(" // can't even propagate, just return")
doc.Line(" return")
doc.Line(" }")
doc.Line(" f(rc, params)")
doc.Line(" })")
doc.Line("}")
doc.Line("")
doc.Line("var %s *%s", varName, typeName)
doc.Line("")
}
}
}
doc.Line("")
doc.Line("func EnsureAllRequests(router *butlerd.Router) {")
for _, method := range clientRequests {
doc.Line(" if _, ok := router.Handlers[%#v]; !ok { panic(%#v) }", method, fmt.Sprintf("missing request handler for (%s)", method))
}
doc.Line("}")
doc.Line("")
doc.Commit("")
doc.Write()
return nil
}