Skip to content

Commit 601e03a

Browse files
committed
Let CodeGenerators provide extension for CustomOptions.
This will get those extensions recognized when the request from protoc is parsed, and then a generator can use the existing SwiftProtobuf extension related apis to access them. Fixes #1677
1 parent 37dc651 commit 601e03a

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Sources/SwiftProtobufPluginLibrary/CodeGenerator.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public protocol CodeGenerator {
5252
/// error.
5353
var supportedEditionRange: ClosedRange<Google_Protobuf_Edition> { get }
5454

55+
/// A list of extensions that define Custom Options
56+
/// (https://protobuf.dev/programming-guides/proto2/#customoptions) for this generator so
57+
/// they will be exposed on the `Descriptor` options.
58+
var customOptionExtensions: [any AnyMessageExtension] { get }
59+
5560
/// If provided, the argument parsing will support `--version` and report
5661
/// this value.
5762
var version: String? { get }
@@ -110,10 +115,29 @@ extension CodeGenerator {
110115
return
111116
}
112117

118+
var extensionMap = SimpleExtensionMap()
119+
if !customOptionExtensions.isEmpty {
120+
for e in customOptionExtensions {
121+
// Don't include Google_Protobuf_FeatureSet, that will be handing via custom features.
122+
precondition(e.messageType == Google_Protobuf_EnumOptions.self ||
123+
e.messageType == Google_Protobuf_EnumValueOptions.self ||
124+
e.messageType == Google_Protobuf_ExtensionRangeOptions.self ||
125+
e.messageType == Google_Protobuf_FieldOptions.self ||
126+
e.messageType == Google_Protobuf_FileOptions.self ||
127+
e.messageType == Google_Protobuf_MessageOptions.self ||
128+
e.messageType == Google_Protobuf_MethodOptions.self ||
129+
e.messageType == Google_Protobuf_OneofOptions.self ||
130+
e.messageType == Google_Protobuf_ServiceOptions.self,
131+
"CodeGenerator `customOptionExtensions` must only extend the descriptor.proto 'Options' messages \(e.messageType).")
132+
}
133+
extensionMap.insert(contentsOf: customOptionExtensions)
134+
}
135+
113136
let response: Google_Protobuf_Compiler_CodeGeneratorResponse
114137
do {
115138
let request = try Google_Protobuf_Compiler_CodeGeneratorRequest(
116-
serializedBytes: FileHandle.standardInput.readDataToEndOfFile()
139+
serializedBytes: FileHandle.standardInput.readDataToEndOfFile(),
140+
extensions: extensionMap
117141
)
118142
response = generateCode(request: request, generator: self)
119143
} catch let e {
@@ -150,6 +174,7 @@ extension CodeGenerator {
150174
// they support editions.
151175
return Google_Protobuf_Edition.unknown...Google_Protobuf_Edition.unknown
152176
}
177+
public var customOptionExtensions: [any AnyMessageExtension] { return [] }
153178
public var version: String? { return nil }
154179
public var projectURL: String? { return nil }
155180
public var copyrightLine: String? { return nil }

0 commit comments

Comments
 (0)