-
Notifications
You must be signed in to change notification settings - Fork 3
/
gen.go
209 lines (173 loc) · 3.89 KB
/
gen.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package natsrpc
import (
"bytes"
"errors"
"fmt"
"go/build"
"go/format"
"path/filepath"
"regexp"
"strings"
"text/template"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"github.com/golang/protobuf/protoc-gen-go/plugin"
)
var (
camelRegexp = regexp.MustCompile("[A-Z][^A-Z]*")
templateFuncs = map[string]interface{}{
"lower": strings.ToLower,
"base": func(in string) string {
idx := strings.LastIndex(in, ".")
if idx == -1 {
return in
}
return in[idx+1:]
},
"hyphenize": func(s string) string {
return strings.ToLower(strings.Join(camelRegexp.FindAllString(s, -1), "-"))
},
}
)
func stringPtr(in string) *string {
if in == "" {
return nil
}
return &in
}
func outName(in *descriptor.FileDescriptorProto) string {
if in.Name != nil {
name := *in.Name
ext := filepath.Ext(name)
name = name[0 : len(name)-len(ext)]
return name + ".pb.nats.go"
}
return "service.pb.nats.go"
}
// packageName determines the name of the package.
func packageName(in *descriptor.FileDescriptorProto) (string, error) {
if in.Package != nil {
return *in.Package, nil
}
if in.Name != nil {
name := *in.Name
ext := filepath.Ext(name)
return name[0 : len(name)-len(ext)], nil
}
return "", errors.New("unable to determine package name")
}
func packagePath(in *descriptor.FileDescriptorProto) (string, error) {
goPath := filepath.Join(build.Default.GOPATH, "src")
fpath, err := filepath.Abs(*in.Name)
if err != nil {
return "", err
}
return filepath.Rel(goPath, filepath.Dir(fpath))
}
// base and lower are template helper functions.
func newTemplate(content string) (*template.Template, error) {
return template.New("page").Funcs(templateFuncs).Parse(content)
}
type service struct {
Pkg string
PkgPath string
Name string
Subject string
Methods []*method
}
type method struct {
Name string
Topic string
InputType string
OutputType string
}
type subjectParams struct {
Pkg string
Service string
}
type Options struct {
Subject string
OutFile string
}
func ParseOptions(p string) (*Options, error) {
if p == "" {
return &Options{}, nil
}
var opts Options
kvs := strings.Split(p, ",")
for _, v := range kvs {
kv := strings.SplitN(v, "=", 2)
switch strings.ToLower(kv[0]) {
case "subject":
opts.Subject = kv[1]
case "outfile":
opts.OutFile = kv[1]
default:
return nil, fmt.Errorf("unknown param: %s", kv[0])
}
}
return &opts, nil
}
func ParseFile(in *descriptor.FileDescriptorProto, tmpl string, opts Options) (*plugin_go.CodeGeneratorResponse_File, error) {
if len(in.Service) != 1 {
return nil, errors.New("exactly one sevice must be defined")
}
// Parse subject template.
if opts.Subject == "" {
opts.Subject = "{{.Pkg}}"
}
sp := in.Service[0]
pkg, err := packageName(in)
if err != nil {
return nil, err
}
pkgPath, err := packagePath(in)
if err != nil {
return nil, err
}
subjectTmpl, err := template.New("subject").Parse(opts.Subject)
if err != nil {
return nil, err
}
buf := bytes.NewBuffer(nil)
err = subjectTmpl.Execute(buf, &subjectParams{
Pkg: pkg,
Service: sp.GetName(),
})
if err != nil {
return nil, err
}
subject := buf.String()
if opts.OutFile == "" {
opts.OutFile = outName(in)
}
sd := &service{
Pkg: pkg,
PkgPath: pkgPath,
Subject: subject,
Name: sp.GetName(),
}
for _, m := range sp.Method {
sd.Methods = append(sd.Methods, &method{
Name: m.GetName(),
Topic: fmt.Sprintf("%s.%s", subject, m.GetName()),
InputType: m.GetInputType(),
OutputType: m.GetOutputType(),
})
}
buf.Reset()
t, err := newTemplate(tmpl)
if err != nil {
return nil, err
}
if err := t.Execute(buf, sd); err != nil {
return nil, err
}
src, err := format.Source(buf.Bytes())
if err != nil {
return nil, err
}
return &plugin_go.CodeGeneratorResponse_File{
Name: stringPtr(opts.OutFile),
Content: stringPtr(string(src)),
}, nil
}