forked from pseudomuto/protokit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
105 lines (86 loc) · 2.73 KB
/
main.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
package main
import (
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/plugin"
"github.com/ruanspies/protokit"
"google.golang.org/genproto/googleapis/api/annotations"
"bytes"
"encoding/json"
"fmt"
"log"
)
func main() {
if err := protokit.RunPlugin(new(plugin)); err != nil {
log.Fatal(err)
}
}
type plugin struct{}
func (p *plugin) Generate(req *plugin_go.CodeGeneratorRequest) (*plugin_go.CodeGeneratorResponse, error) {
descriptors := protokit.ParseCodeGenRequest(req)
files := make([]*file, len(descriptors))
for i, d := range descriptors {
files[i] = newFile(d)
}
buf := new(bytes.Buffer)
enc := json.NewEncoder(buf)
enc.SetIndent("", " ")
if err := enc.Encode(files); err != nil {
return nil, err
}
resp := new(plugin_go.CodeGeneratorResponse)
resp.File = append(resp.File, &plugin_go.CodeGeneratorResponse_File{
Name: proto.String("output.json"),
Content: proto.String(buf.String()),
})
return resp, nil
}
type file struct {
Name string `json:"name"`
Description string `json:"description"`
Services []*service `json:"services"`
}
func newFile(fd *protokit.FileDescriptor) *file {
svcs := make([]*service, len(fd.GetServices()))
for i, sd := range fd.GetServices() {
svcs[i] = newService(sd)
}
return &file{
Name: fmt.Sprintf("%s.%s", fd.GetPackage(), fd.GetName()),
Description: fd.GetComments().String(),
Services: svcs,
}
}
type service struct {
Name string `json:"name"`
Methods []*method `json:"methods"`
}
func newService(sd *protokit.ServiceDescriptor) *service {
methods := make([]*method, len(sd.GetMethods()))
for i, md := range sd.GetMethods() {
methods[i] = newMethod(md)
}
return &service{Name: sd.GetName(), Methods: methods}
}
type method struct {
Name string `json:"name"`
HTTPRules []string `json:"http_rules"`
}
func newMethod(md *protokit.MethodDescriptor) *method {
httpRules := make([]string, 0)
if httpRule, ok := md.OptionExtensions["google.api.http"].(*annotations.HttpRule); ok {
switch httpRule.GetPattern().(type) {
case *annotations.HttpRule_Get:
httpRules = append(httpRules, fmt.Sprintf("GET %s", httpRule.GetGet()))
case *annotations.HttpRule_Put:
httpRules = append(httpRules, fmt.Sprintf("PUT %s", httpRule.GetPut()))
case *annotations.HttpRule_Post:
httpRules = append(httpRules, fmt.Sprintf("POST %s", httpRule.GetPost()))
case *annotations.HttpRule_Delete:
httpRules = append(httpRules, fmt.Sprintf("DELETE %s", httpRule.GetDelete()))
case *annotations.HttpRule_Patch:
httpRules = append(httpRules, fmt.Sprintf("PATCH %s", httpRule.GetPatch()))
}
// Append more for each rule in httpRule.AdditionalBindings...
}
return &method{Name: md.GetName(), HTTPRules: httpRules}
}