-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocat.go
152 lines (139 loc) · 3.52 KB
/
protocat.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package protocat
import (
"bytes"
"fmt"
"io"
"strings"
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/desc/builder"
"github.com/jhump/protoreflect/desc/protoparse"
"github.com/jhump/protoreflect/desc/protoprint"
)
func ConcatFiles(importPaths []string, fileNames ...string) (io.Reader, error) {
files, err := parse(importPaths, fileNames...)
if err != nil {
return nil, err
}
rd, err := concat(files...)
if err != nil {
return nil, err
}
b, err := io.ReadAll(rd)
if err != nil {
return nil, err
}
f := string(b)
// remove duplicated imports
for _, fn := range fileNames {
f = strings.ReplaceAll(f, fmt.Sprintf(`import "%s";
`, fn), "")
}
return strings.NewReader(f), nil
}
func parse(importPaths []string, fileNames ...string) ([]*desc.FileDescriptor, error) {
p := protoparse.Parser{
ImportPaths: importPaths,
InferImportPaths: true,
IncludeSourceCodeInfo: true,
}
files, err := p.ParseFiles(fileNames...)
if err != nil {
return nil, fmt.Errorf("failed to parse proto files: %w", err)
}
return files, nil
}
func concat(files ...*desc.FileDescriptor) (io.Reader, error) {
if len(files) == 0 {
return strings.NewReader(""), nil
}
b := builder.NewFile("result.proto")
firstFile := files[0]
isProto3 := firstFile.IsProto3()
pkg := firstFile.GetPackage()
b.SetProto3(isProto3)
b.SetPackageName(pkg)
for _, file := range files {
if file.GetPackage() != pkg {
return nil, fmt.Errorf("package name want: %s, got: %s", pkg, file.GetPackage())
}
if file.IsProto3() != isProto3 {
return nil, fmt.Errorf("isProto3 want: %b, got: %b", isProto3, file.IsProto3())
}
for _, m := range file.GetMessageTypes() {
mb, err := fromMessage(m)
if err != nil {
return nil, err
}
b.AddMessage(mb)
}
for _, e := range file.GetEnumTypes() {
eb, err := builder.FromEnum(e)
if err != nil {
return nil, err
}
b.AddEnum(eb)
}
for _, ex := range file.GetExtensions() {
exb, err := builder.FromField(ex)
if err != nil {
return nil, err
}
b.AddExtension(exb)
}
for _, s := range file.GetServices() {
sb, err := builder.FromService(s)
if err != nil {
return nil, err
}
b.AddService(sb)
}
}
f, err := b.Build()
if err != nil {
return nil, fmt.Errorf("failed to build concatenated proto: %w", err)
}
printer := protoprint.Printer{
SortElements: true,
}
var buf bytes.Buffer
err = printer.PrintProtoFile(f, &buf)
if err != nil {
return nil, fmt.Errorf("failed to print proto: %w", err)
}
return &buf, nil
}
func fromMessage(m *desc.MessageDescriptor) (*builder.MessageBuilder, error) {
mProto := m.AsDescriptorProto()
mb := builder.NewMessage(m.GetName())
c := builder.Comments{
LeadingDetachedComments: m.GetSourceInfo().GetLeadingDetachedComments(),
LeadingComment: m.GetSourceInfo().GetLeadingComments(),
TrailingComment: m.GetSourceInfo().GetTrailingComments(),
}
mb.SetComments(c)
mb.SetExtensionRanges(mProto.GetExtensionRange())
mb.SetOptions(mProto.GetOptions())
mb.SetReservedNames(mProto.GetReservedName())
for _, f := range m.GetFields() {
fld, err := builder.FromField(f)
if err != nil {
return nil, err
}
mb.AddField(fld)
}
for _, childMsg := range m.GetNestedMessageTypes() {
msg, err := builder.FromMessage(childMsg)
if err != nil {
return nil, err
}
mb.AddNestedMessage(msg)
}
for _, childEnum := range m.GetNestedEnumTypes() {
enum, err := builder.FromEnum(childEnum)
if err != nil {
return nil, err
}
mb.AddNestedEnum(enum)
}
return mb, nil
}