-
Notifications
You must be signed in to change notification settings - Fork 566
/
main.go
129 lines (107 loc) · 4.16 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
//nolint:wrapcheck
package main
import (
"fmt"
"io"
"os"
"sort"
descriptor "github.com/golang/protobuf/protoc-gen-go/descriptor"
plugin "github.com/golang/protobuf/protoc-gen-go/plugin"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"google.golang.org/protobuf/types/pluginpb"
)
const generatedFileWarning = "// Code generated by pachgen (etc/proto/pachgen). DO NOT EDIT.\n"
// Generate:
// Unsupported transaction client (overridden by user code) - existing in client/transaction.go
// ProtoCodeGenerator is an interface to be implemented by each type of output
// file we are generating. 'AddProto' is called for each input .proto file, and
// then 'Finish' is called to collect the resulting generated .go file.
type ProtoCodeGenerator interface {
AddProto(*descriptor.FileDescriptorProto) error
Finish() ([]*plugin.CodeGeneratorResponse_File, error)
}
// CodeGenerators is the set of all code generators that will be run for any
// input files. It is populated by 'init' in each code generator's
// implementation.
var CodeGenerators = []ProtoCodeGenerator{}
// errResponse is a helper function to easily construct an error response to send to protoc.
func errResponse(err error) *plugin.CodeGeneratorResponse {
errString := err.Error()
return &plugin.CodeGeneratorResponse{Error: &errString}
}
type fdProtoSlice []*descriptorpb.FileDescriptorProto
func (s fdProtoSlice) Len() int { return len(s) }
func (s fdProtoSlice) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() }
func (s fdProtoSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type serviceSlice []*descriptorpb.ServiceDescriptorProto
func (s serviceSlice) Len() int { return len(s) }
func (s serviceSlice) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() }
func (s serviceSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
type methodSlice []*descriptorpb.MethodDescriptorProto
func (s methodSlice) Len() int { return len(s) }
func (s methodSlice) Less(i, j int) bool { return s[i].GetName() < s[j].GetName() }
func (s methodSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func sortProtos(pp []*descriptorpb.FileDescriptorProto) {
sort.Sort(fdProtoSlice(pp))
for _, p := range pp {
sort.Sort(serviceSlice(p.GetService()))
for _, s := range p.GetService() {
sort.Sort(methodSlice(s.GetMethod()))
}
}
}
// runInternal will send each proto file to each code generator, then collect
// their output files into a single response.
func runInternal(req *plugin.CodeGeneratorRequest) *plugin.CodeGeneratorResponse {
sortProtos(req.ProtoFile)
for _, proto := range req.ProtoFile {
for _, gen := range CodeGenerators {
if err := gen.AddProto(proto); err != nil {
return errResponse(err)
}
}
}
resp := &plugin.CodeGeneratorResponse{}
resp.SupportedFeatures = proto.Uint64(resp.GetSupportedFeatures() | uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL))
for _, gen := range CodeGenerators {
if respFiles, err := gen.Finish(); err != nil {
fmt.Fprintf(os.Stderr, "error from codegen: %v\n", err)
// return errResponse(err)
} else {
for _, respFile := range respFiles {
// Add the generated file warning here rather than count on the generators to do it
newContent := generatedFileWarning + *respFile.Content
respFile.Content = &newContent
resp.File = append(resp.File, respFile)
}
}
}
return resp
}
// run handles the input/output of communicating with 'protoc', reading a
// request from stdin and writing a response to stdout.
func run() error {
req := &plugin.CodeGeneratorRequest{}
if data, err := io.ReadAll(os.Stdin); err != nil {
return err
} else if err := proto.Unmarshal(data, req); err != nil {
return err
}
resp := runInternal(req)
if marshalled, err := proto.Marshal(resp); err != nil {
return err
} else if _, err := os.Stdout.Write(marshalled); err != nil {
return err
} else if resp.Error != nil {
return fmt.Errorf("%s", *resp.Error)
}
return nil
}
// main wraps 'run', which returns an error if we should have a non-zero exit code
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "Fatal error: %v\n", err)
os.Exit(1)
}
}