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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package parser
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/descriptorpb"
"github.com/bufbuild/protocompile/ast"
"github.com/bufbuild/protocompile/reporter"
)
// Clone returns a copy of the given result. Since descriptor protos may be
// mutated during linking, this can return a defensive copy so that mutations
// don't impact concurrent operations in an unsafe way. This is called if the
// parse result could be re-used across concurrent operations and has unresolved
// references and options which will require mutation by the linker.
//
// If the given value has a method with the following signature, it will be
// called to perform the operation:
//
// Clone() Result
//
// If the given value does not provide a Clone method and is not the implementation
// provided by this package, it is possible for an error to occur in creating the
// copy, which may result in a panic. This can happen if the AST of the given result
// is not actually valid and a file descriptor proto cannot be successfully derived
// from it.
func Clone(r Result) Result {
if cl, ok := r.(interface{ Clone() Result }); ok {
return cl.Clone()
}
if res, ok := r.(*result); ok {
newProto := proto.Clone(res.proto).(*descriptorpb.FileDescriptorProto) //nolint:errcheck
newNodes := make(map[proto.Message]ast.Node, len(res.nodes))
newResult := &result{
file: res.file,
proto: newProto,
nodes: newNodes,
}
recreateNodeIndexForFile(res, newResult, res.proto, newProto)
return newResult
}
// Can't do the deep-copy we know how to do. So we have to take a
// different tactic.
if r.AST() == nil {
// no AST? all we have to do is copy the proto
fileProto := proto.Clone(r.FileDescriptorProto()).(*descriptorpb.FileDescriptorProto) //nolint:errcheck
return ResultWithoutAST(fileProto)
}
// Otherwise, we have an AST, but no way to clone the result's
// internals. So just re-create them from scratch.
res, err := ResultFromAST(r.AST(), false, reporter.NewHandler(nil))
if err != nil {
panic(err)
}
return res
}
func recreateNodeIndexForFile(orig, clone *result, origProto, cloneProto *descriptorpb.FileDescriptorProto) {
updateNodeIndexWithOptions[*descriptorpb.FileOptions](orig, clone, origProto, cloneProto)
for i, origMd := range origProto.MessageType {
cloneMd := cloneProto.MessageType[i]
recreateNodeIndexForMessage(orig, clone, origMd, cloneMd)
}
for i, origEd := range origProto.EnumType {
cloneEd := cloneProto.EnumType[i]
recreateNodeIndexForEnum(orig, clone, origEd, cloneEd)
}
for i, origExtd := range origProto.Extension {
cloneExtd := cloneProto.Extension[i]
updateNodeIndexWithOptions[*descriptorpb.FieldOptions](orig, clone, origExtd, cloneExtd)
}
for i, origSd := range origProto.Service {
cloneSd := cloneProto.Service[i]
updateNodeIndexWithOptions[*descriptorpb.ServiceOptions](orig, clone, origSd, cloneSd)
for j, origMtd := range origSd.Method {
cloneMtd := cloneSd.Method[j]
updateNodeIndexWithOptions[*descriptorpb.MethodOptions](orig, clone, origMtd, cloneMtd)
}
}
}
func recreateNodeIndexForMessage(orig, clone *result, origProto, cloneProto *descriptorpb.DescriptorProto) {
updateNodeIndexWithOptions[*descriptorpb.MessageOptions](orig, clone, origProto, cloneProto)
for i, origFld := range origProto.Field {
cloneFld := cloneProto.Field[i]
updateNodeIndexWithOptions[*descriptorpb.FieldOptions](orig, clone, origFld, cloneFld)
}
for i, origOod := range origProto.OneofDecl {
cloneOod := cloneProto.OneofDecl[i]
updateNodeIndexWithOptions[*descriptorpb.OneofOptions](orig, clone, origOod, cloneOod)
}
for i, origExtr := range origProto.ExtensionRange {
cloneExtr := cloneProto.ExtensionRange[i]
updateNodeIndex(orig, clone, asExtsNode(origExtr), asExtsNode(cloneExtr))
updateNodeIndexWithOptions[*descriptorpb.ExtensionRangeOptions](orig, clone, origExtr, cloneExtr)
}
for i, origRr := range origProto.ReservedRange {
cloneRr := cloneProto.ReservedRange[i]
updateNodeIndex(orig, clone, origRr, cloneRr)
}
for i, origNmd := range origProto.NestedType {
cloneNmd := cloneProto.NestedType[i]
recreateNodeIndexForMessage(orig, clone, origNmd, cloneNmd)
}
for i, origEd := range origProto.EnumType {
cloneEd := cloneProto.EnumType[i]
recreateNodeIndexForEnum(orig, clone, origEd, cloneEd)
}
for i, origExtd := range origProto.Extension {
cloneExtd := cloneProto.Extension[i]
updateNodeIndexWithOptions[*descriptorpb.FieldOptions](orig, clone, origExtd, cloneExtd)
}
}
func recreateNodeIndexForEnum(orig, clone *result, origProto, cloneProto *descriptorpb.EnumDescriptorProto) {
updateNodeIndexWithOptions[*descriptorpb.EnumOptions](orig, clone, origProto, cloneProto)
for i, origEvd := range origProto.Value {
cloneEvd := cloneProto.Value[i]
updateNodeIndexWithOptions[*descriptorpb.EnumValueOptions](orig, clone, origEvd, cloneEvd)
}
for i, origRr := range origProto.ReservedRange {
cloneRr := cloneProto.ReservedRange[i]
updateNodeIndex(orig, clone, origRr, cloneRr)
}
}
func recreateNodeIndexForOptions(orig, clone *result, origProtos, cloneProtos []*descriptorpb.UninterpretedOption) {
for i, origOpt := range origProtos {
cloneOpt := cloneProtos[i]
updateNodeIndex(orig, clone, origOpt, cloneOpt)
for j, origName := range origOpt.Name {
cloneName := cloneOpt.Name[j]
updateNodeIndex(orig, clone, origName, cloneName)
}
}
}
func updateNodeIndex[M proto.Message](orig, clone *result, origProto, cloneProto M) {
node := orig.nodes[origProto]
if node != nil {
clone.nodes[cloneProto] = node
}
}
type pointerMessage[T any] interface {
*T
proto.Message
}
type options[T any] interface {
// need this type instead of just proto.Message so we can check for nil pointer
pointerMessage[T]
GetUninterpretedOption() []*descriptorpb.UninterpretedOption
}
type withOptions[O options[T], T any] interface {
proto.Message
GetOptions() O
}
func updateNodeIndexWithOptions[O options[T], M withOptions[O, T], T any](orig, clone *result, origProto, cloneProto M) {
updateNodeIndex(orig, clone, origProto, cloneProto)
origOpts := origProto.GetOptions()
cloneOpts := cloneProto.GetOptions()
if origOpts != nil {
recreateNodeIndexForOptions(orig, clone, origOpts.GetUninterpretedOption(), cloneOpts.GetUninterpretedOption())
}
}
|