Skip to content

Commit b91ba22

Browse files
committed
Add a plugin api for extracting back out the descriptor proto.
Modeled after the C++ Copy*To() apis, this should provide the building blocks for what is needed now and might be needed in the future. Fixes #1678
1 parent 75b2b0e commit b91ba22

File tree

4 files changed

+7976
-1464
lines changed

4 files changed

+7976
-1464
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS= \
362362
Tests/SwiftProtobufPluginLibraryTests/DescriptorTestData.swift: build ${PROTOC_GEN_SWIFT} ${SWIFT_PLUGINLIB_DESCRIPTOR_TEST_PROTOS}
363363
@${PROTOC} \
364364
--include_imports \
365+
--include_source_info \
365366
--descriptor_set_out=PluginLibDescriptorTestData.bin \
366367
-I Protos/SwiftProtobuf \
367368
-I Protos/SwiftProtobufPluginLibrary \

Sources/SwiftProtobufPluginLibrary/Descriptor.swift

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,25 @@ public final class DescriptorSet {
163163
}
164164
}
165165

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+
166185
/// Models a .proto file. `FileDescriptor`s are not directly created,
167186
/// instead they are constructed/fetched via the `DescriptorSet` or
168187
/// they are directly accessed via a `file` property on all the other
@@ -226,12 +245,41 @@ public final class FileDescriptor {
226245

227246
private let sourceCodeInfo: Google_Protobuf_SourceCodeInfo
228247

229-
/// The proto version of the descriptor that defines this File.
248+
/// Extract contents of this descriptor in Proto form.
230249
///
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()")
235283
public var proto: Google_Protobuf_FileDescriptorProto { return _proto }
236284
private let _proto: Google_Protobuf_FileDescriptorProto
237285

0 commit comments

Comments
 (0)