@@ -163,6 +163,25 @@ public final class DescriptorSet {
163
163
}
164
164
}
165
165
166
+ /// Options for collected a proto object from a Descriptor.
167
+ public struct ExtractProtoOptions {
168
+
169
+ /// If the `SourceCodeInfo` should also be included in the proto file.
170
+ ///
171
+ /// If embedding the descriptor in a binary for some reason, normally the `SourceCodeInfo`
172
+ /// isn't needed and would just be an increas in binary size.
173
+ public var includeSourceCodeInfo : Bool = false
174
+
175
+ /// Copy on the _header_ for the descriptor. This mainly means leave out any of the nested
176
+ /// descriptors (messages, enums, etc.).
177
+ public var headerOnly : Bool = false
178
+
179
+ // NOTE: in the future maybe add toggles to model the behavior of the *Descriptor::Copy*To()
180
+ // apis.
181
+
182
+ public init ( ) { }
183
+ }
184
+
166
185
/// Models a .proto file. `FileDescriptor`s are not directly created,
167
186
/// instead they are constructed/fetched via the `DescriptorSet` or
168
187
/// they are directly accessed via a `file` property on all the other
@@ -226,12 +245,41 @@ public final class FileDescriptor {
226
245
227
246
private let sourceCodeInfo : Google_Protobuf_SourceCodeInfo
228
247
229
- /// The proto version of the descriptor that defines this File .
248
+ /// Extract contents of this descriptor in Proto form .
230
249
///
231
- /// Thanks to Editions, this isn't likely to be exactly what
232
- /// folks want anymore, so wave any other plugins off it.
233
- @available ( * , deprecated,
234
- message: " Use the properties directly or open a GitHub issue for something missing " )
250
+ /// - Parameters:
251
+ /// - options: Controls what information is include/excluded when creating the Proto version.
252
+ ///
253
+ /// - Returns: A `Google_Protobuf_FileDescriptorProto`.
254
+ public func extractProto(
255
+ options: ExtractProtoOptions = ExtractProtoOptions ( )
256
+ ) -> Google_Protobuf_FileDescriptorProto {
257
+ // In the future it might make sense to model this like the C++, where the protos is built up
258
+ // on demand instead of keeping the object around.
259
+ var result = _proto
260
+
261
+ if !options. includeSourceCodeInfo {
262
+ result. clearSourceCodeInfo ( )
263
+ }
264
+
265
+ if options. headerOnly {
266
+ // For FileDescriptor, make `headerOnly` mean the same things as C++
267
+ // `FileDescriptor::CopyHeaderTo()`.
268
+ result. dependency = [ ]
269
+ result. publicDependency = [ ]
270
+ result. weakDependency = [ ]
271
+ result. messageType = [ ]
272
+ result. enumType = [ ]
273
+ result. messageType = [ ]
274
+ result. service = [ ]
275
+ result. extension = [ ]
276
+ }
277
+
278
+ return result
279
+ }
280
+
281
+ /// The proto version of the descriptor that defines this File.
282
+ @available ( * , deprecated, renamed: " extractProto() " )
235
283
public var proto : Google_Protobuf_FileDescriptorProto { return _proto }
236
284
private let _proto : Google_Protobuf_FileDescriptorProto
237
285
0 commit comments