-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (50 loc) · 1.22 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
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/plugin"
)
func main() {
var req plugin_go.CodeGeneratorRequest
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
panic(fmt.Errorf("got error reading from stdin: %v", err))
}
if err := proto.Unmarshal(data, &req); err != nil {
panic(fmt.Errorf("got error unmarshaling request: %v", err))
}
files, err, internalErr := generate(req.GetFileToGenerate(), req.GetProtoFile())
if internalErr != nil {
panic(fmt.Errorf("error generating: %v", err))
}
resp := plugin_go.CodeGeneratorResponse{
Error: func() (out *string) {
if err != nil {
*out = fmt.Sprintf("%s", err)
return
}
return
}(),
File: func() (out []*plugin_go.CodeGeneratorResponse_File) {
if err != nil {
return
}
for _, f := range files {
out = append(out, &plugin_go.CodeGeneratorResponse_File{
Name: &f.Name,
Content: &f.Content,
})
}
return
}(),
}
data, err = proto.Marshal(&resp)
if err != nil {
panic(fmt.Errorf("error marshaling the response: %v", err))
}
if _, err := os.Stdout.Write(data); err != nil {
panic(fmt.Errorf("error writing to std out: %v", err))
}
}