-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
135 lines (123 loc) · 3.52 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
130
131
132
133
134
135
package main
import (
"flag"
"log"
"os"
"cloud.google.com/go/longrunning/autogen/longrunningpb"
"google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
)
func main() {
var filename string
flag.StringVar(&filename, "f", "", "file descriptor set to scrub")
flag.Parse()
if filename == "" {
flag.Usage()
os.Exit(1)
}
input, err := os.ReadFile(filename)
if err != nil {
log.Fatal(err)
}
var fileDescriptorSet descriptorpb.FileDescriptorSet
if err := proto.Unmarshal(input, &fileDescriptorSet); err != nil {
log.Fatal(err)
}
for _, file := range fileDescriptorSet.GetFile() {
file.Dependency = scrubDependencies(file.GetDependency())
scrubResourceDefinitions(file)
for _, message := range file.GetMessageType() {
scrubResource(message)
scrubResourceReferences(message)
scrubFieldBehaviors(message)
scrubMessageFields(message)
}
for _, service := range file.GetService() {
scrubServiceOptions(service)
for _, method := range service.GetMethod() {
scrubLongRunningOperationInfo(method)
scrubMethodSignature(method)
}
}
}
output, err := proto.Marshal(&fileDescriptorSet)
if err != nil {
log.Fatal(err)
}
if err := os.WriteFile(filename, output, 0o600); err != nil {
log.Fatal(err)
}
}
func scrubDependencies(dependencies []string) []string {
var n int
for _, dependency := range dependencies {
switch dependency {
case "google/api/client.proto",
"google/api/resource.proto",
"google/api/field_behavior.proto":
continue
}
dependencies[n] = dependency
n++
}
return dependencies[:n]
}
func scrubResourceDefinitions(file *descriptorpb.FileDescriptorProto) {
if file.GetOptions() != nil {
proto.ClearExtension(file.GetOptions(), annotations.E_ResourceDefinition)
}
}
func scrubResource(message *descriptorpb.DescriptorProto) {
if message.GetOptions() != nil {
proto.ClearExtension(message.GetOptions(), annotations.E_Resource)
}
}
func scrubServiceOptions(service *descriptorpb.ServiceDescriptorProto) {
if service.GetOptions() != nil {
proto.ClearExtension(service.GetOptions(), annotations.E_DefaultHost)
}
}
func scrubMessageFields(message *descriptorpb.DescriptorProto) {
n := 0
for _, field := range message.GetField() {
if field.GetTypeName() != ".google.api.ResourceDescriptor" {
message.Field[n] = field
n++
}
}
message.Field = message.GetField()[:n]
for _, nestedMessage := range message.GetNestedType() {
scrubMessageFields(nestedMessage)
}
}
func scrubResourceReferences(message *descriptorpb.DescriptorProto) {
for _, field := range message.GetField() {
if field.GetOptions() != nil {
proto.ClearExtension(field.GetOptions(), annotations.E_ResourceReference)
}
}
for _, nestedMessage := range message.GetNestedType() {
scrubResourceReferences(nestedMessage)
}
}
func scrubFieldBehaviors(message *descriptorpb.DescriptorProto) {
for _, field := range message.GetField() {
if field.GetOptions() != nil {
proto.ClearExtension(field.GetOptions(), annotations.E_FieldBehavior)
}
}
for _, nestedMessage := range message.GetNestedType() {
scrubFieldBehaviors(nestedMessage)
}
}
func scrubLongRunningOperationInfo(method *descriptorpb.MethodDescriptorProto) {
if method.GetOptions() != nil {
proto.ClearExtension(method.GetOptions(), longrunningpb.E_OperationInfo)
}
}
func scrubMethodSignature(method *descriptorpb.MethodDescriptorProto) {
if method.GetOptions() != nil {
proto.ClearExtension(method.GetOptions(), annotations.E_MethodSignature)
}
}