...
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 package command
30
31 import (
32 "fmt"
33 "go/format"
34 "io/ioutil"
35 "os"
36 "strings"
37
38 _ "github.com/gogo/protobuf/plugin/compare"
39 _ "github.com/gogo/protobuf/plugin/defaultcheck"
40 _ "github.com/gogo/protobuf/plugin/description"
41 _ "github.com/gogo/protobuf/plugin/embedcheck"
42 _ "github.com/gogo/protobuf/plugin/enumstringer"
43 _ "github.com/gogo/protobuf/plugin/equal"
44 _ "github.com/gogo/protobuf/plugin/face"
45 _ "github.com/gogo/protobuf/plugin/gostring"
46 _ "github.com/gogo/protobuf/plugin/marshalto"
47 _ "github.com/gogo/protobuf/plugin/oneofcheck"
48 _ "github.com/gogo/protobuf/plugin/populate"
49 _ "github.com/gogo/protobuf/plugin/size"
50 _ "github.com/gogo/protobuf/plugin/stringer"
51 "github.com/gogo/protobuf/plugin/testgen"
52 _ "github.com/gogo/protobuf/plugin/union"
53 _ "github.com/gogo/protobuf/plugin/unmarshal"
54 "github.com/gogo/protobuf/proto"
55 "github.com/gogo/protobuf/protoc-gen-gogo/generator"
56 _ "github.com/gogo/protobuf/protoc-gen-gogo/grpc"
57 plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin"
58 )
59
60 func Read() *plugin.CodeGeneratorRequest {
61 g := generator.New()
62 data, err := ioutil.ReadAll(os.Stdin)
63 if err != nil {
64 g.Error(err, "reading input")
65 }
66
67 if err := proto.Unmarshal(data, g.Request); err != nil {
68 g.Error(err, "parsing input proto")
69 }
70
71 if len(g.Request.FileToGenerate) == 0 {
72 g.Fail("no files to generate")
73 }
74 return g.Request
75 }
76
77
78 func GeneratePlugin(req *plugin.CodeGeneratorRequest, p generator.Plugin, filenameSuffix string) *plugin.CodeGeneratorResponse {
79 g := generator.New()
80 g.Request = req
81 if len(g.Request.FileToGenerate) == 0 {
82 g.Fail("no files to generate")
83 }
84
85 g.CommandLineParameters(g.Request.GetParameter())
86
87 g.WrapTypes()
88 g.SetPackageNames()
89 g.BuildTypeNameMap()
90 g.GeneratePlugin(p)
91
92 for i := 0; i < len(g.Response.File); i++ {
93 g.Response.File[i].Name = proto.String(
94 strings.Replace(*g.Response.File[i].Name, ".pb.go", filenameSuffix, -1),
95 )
96 }
97 if err := goformat(g.Response); err != nil {
98 g.Error(err)
99 }
100 return g.Response
101 }
102
103 func goformat(resp *plugin.CodeGeneratorResponse) error {
104 for i := 0; i < len(resp.File); i++ {
105 formatted, err := format.Source([]byte(resp.File[i].GetContent()))
106 if err != nil {
107 return fmt.Errorf("go format error: %v", err)
108 }
109 fmts := string(formatted)
110 resp.File[i].Content = &fmts
111 }
112 return nil
113 }
114
115 func Generate(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
116
117
118
119 g := generator.New()
120 g.Request = req
121
122 g.CommandLineParameters(g.Request.GetParameter())
123
124
125
126 g.WrapTypes()
127
128 g.SetPackageNames()
129 g.BuildTypeNameMap()
130
131 g.GenerateAllFiles()
132
133 if err := goformat(g.Response); err != nil {
134 g.Error(err)
135 }
136
137 testReq := proto.Clone(req).(*plugin.CodeGeneratorRequest)
138
139 testResp := GeneratePlugin(testReq, testgen.NewPlugin(), "pb_test.go")
140
141 for i := 0; i < len(testResp.File); i++ {
142 if strings.Contains(*testResp.File[i].Content, `//These tests are generated by github.com/gogo/protobuf/plugin/testgen`) {
143 g.Response.File = append(g.Response.File, testResp.File[i])
144 }
145 }
146
147 return g.Response
148 }
149
150 func Write(resp *plugin.CodeGeneratorResponse) {
151 g := generator.New()
152
153 data, err := proto.Marshal(resp)
154 if err != nil {
155 g.Error(err, "failed to marshal output proto")
156 }
157 _, err = os.Stdout.Write(data)
158 if err != nil {
159 g.Error(err, "failed to write output proto")
160 }
161 }
162
View as plain text