-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
131 lines (110 loc) · 3.01 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package main
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
pbannotations "google.golang.org/genproto/googleapis/api/annotations"
)
type templateData struct {
Version string
SourceFilename string
Services []*Service
}
func (data *templateData) Quote() string {
return "`"
}
type Service struct {
*descriptor.ServiceDescriptorProto
}
func (s *Service) Methods() []*Method {
methods := []*Method{}
for _, method := range s.GetMethod() {
methods = append(methods, &Method{method})
}
return methods
}
type Method struct {
*descriptor.MethodDescriptorProto
}
func (method *Method) HTTPMethod() string {
if method.GetOptions() == nil {
return ""
}
if !proto.HasExtension(method.Options, pbannotations.E_Http) {
return ""
}
ext, err := proto.GetExtension(method.Options, pbannotations.E_Http)
if err != nil {
panic(err)
}
opts := ext.(*pbannotations.HttpRule)
switch opts.GetPattern().(type) {
case *pbannotations.HttpRule_Get:
return "GET"
case *pbannotations.HttpRule_Put:
return "PUT"
case *pbannotations.HttpRule_Post:
return "POST"
case *pbannotations.HttpRule_Delete:
return "DELETE"
}
return ""
}
func (method *Method) httpPath() string {
ext, err := proto.GetExtension(method.Options, pbannotations.E_Http)
if err != nil {
panic(err)
}
opts := ext.(*pbannotations.HttpRule)
switch rule := opts.GetPattern().(type) {
case *pbannotations.HttpRule_Get:
return rule.Get
case *pbannotations.HttpRule_Put:
return rule.Put
case *pbannotations.HttpRule_Post:
return rule.Post
case *pbannotations.HttpRule_Delete:
return rule.Delete
}
panic("http rule has no path")
}
func (method *Method) Path() (*Path, error) {
return parseTemplate(method.httpPath())
}
func (method *Method) HasBody() bool {
ext, err := proto.GetExtension(method.Options, pbannotations.E_Http)
if err != nil {
panic(err)
}
opts := ext.(*pbannotations.HttpRule)
return opts.Body == "*"
}
const browserTemplate = `// Code generated by protoc-gen-grpc_browser {{.Version}}, DO NOT EDIT.
// Source: {{.SourceFilename}}
import { Caller } from '@altipla/grpc-browser';
{{range .Services}}
export default class {{.GetName}}Client {
constructor(opts = {}) {
this._caller = new grpc.Caller(opts);
}{{range .Methods}}{{if .HTTPMethod}}
{{.GetName}}(req) {
{{$path := .Path -}}
{{range .Path.Segments -}}
{{with .Var -}}
{{if .Parts -}}
{
let parts = {{$.Quote}}{{.Binding}}{{$.Quote}}.split('/');
let fmt = {{$.Quote}}parameter req.{{.BindingName}} should have the format "{{.Format}}"{{$.Quote}};
if (parts.length !== {{len .Parts}}) { throw new Error(fmt); }
{{- range $i, $part := .Parts -}}{{if not (eq . "*")}}
if (parts[{{$i}}] !== '{{.}}') { throw new Error(fmt); }
{{- end -}}
{{end}}
}
{{end -}}
{{end -}}
{{end -}}
return this._caller.send('{{.HTTPMethod}}', {{$.Quote}}{{$path.Binding}}{{$.Quote}}, req, {{.HasBody}}, {{$path.UnsetKeys}});
}{{end}}{{end}}
};
{{end}}
`