This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
main.go
76 lines (61 loc) · 1.44 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
package main
import (
"io"
"io/ioutil"
"os"
"github.com/golang/protobuf/proto"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"go.larrymyers.com/protoc-gen-twirp_typescript/generator"
"go.larrymyers.com/protoc-gen-twirp_typescript/generator/minimal"
)
func main() {
req := readRequest(os.Stdin)
writeResponse(os.Stdout, generate(req))
}
func readRequest(r io.Reader) *plugin.CodeGeneratorRequest {
data, err := ioutil.ReadAll(r)
if err != nil {
panic(err)
}
req := new(plugin.CodeGeneratorRequest)
if err = proto.Unmarshal(data, req); err != nil {
panic(err)
}
if len(req.FileToGenerate) == 0 {
panic(err)
}
return req
}
func generate(in *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
resp := &plugin.CodeGeneratorResponse{}
params := generator.GetParameters(in)
gen, err := generator.NewGenerator(params)
if err != nil {
resp.Error = proto.String(err.Error())
return resp
}
for _, f := range in.GetProtoFile() {
files, err := gen.Generate(f)
if err != nil {
resp.Error = proto.String(err.Error())
return resp
}
for _, cf := range files {
resp.File = append(resp.File, cf)
}
}
lib, _ := params["library"]
if lib != "pbjs" {
resp.File = append(resp.File, minimal.RuntimeLibrary())
}
return resp
}
func writeResponse(w io.Writer, resp *plugin.CodeGeneratorResponse) {
data, err := proto.Marshal(resp)
if err != nil {
panic(err)
}
_, err = w.Write(data)
if err != nil {
}
}