/
main.go
56 lines (53 loc) · 1.31 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
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/edstell/protoc-gen-lambda/generator"
"github.com/edstell/protoc-gen-lambda/template"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
)
func main() {
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(err)
}
req := &plugin.CodeGeneratorRequest{}
if err := proto.Unmarshal(data, req); err != nil {
panic(fmt.Sprint("failed to unmarshal the generation request:", err))
}
funcs := map[string]interface{}{
"toLower": strings.ToLower,
"rhs": func(s string) string {
ss := strings.Split(s, ".")
return ss[len(ss)-1]
},
}
names := []string{"client", "router"}
files := make([]*plugin.CodeGeneratorResponse_File, 0, len(req.ProtoFile)*len(names))
for _, protoFile := range req.ProtoFile {
if len(protoFile.Service) == 0 {
continue
}
for _, name := range names {
template, err := template.FromAsset(name, funcs)
if err != nil {
panic(err)
}
file, err := generator.FromTemplate(name, template).Generate(protoFile)
if err != nil {
panic(fmt.Sprint("failed to generate file", err))
}
files = append(files, file)
}
}
data, err = proto.Marshal(&plugin.CodeGeneratorResponse{
File: files,
})
if err != nil {
panic(err)
}
os.Stdout.Write(data)
}