Skip to content

Commit 096a4c5

Browse files
author
Pouya Yarandi
committed
Add applying option in protoc
1 parent 2ea9033 commit 096a4c5

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Sources/protoc-gen-swift/GeneratorOptions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class GeneratorOptions {
5353
let protoToModuleMappings: ProtoFileToModuleMappings
5454
let visibility: Visibility
5555
let implementationOnlyImports: Bool
56+
let generateApplyingMethods: Bool
5657

5758
/// A string snippet to insert for the visibility
5859
let visibilitySourceSnippet: String
@@ -63,6 +64,7 @@ class GeneratorOptions {
6364
var visibility: Visibility = .internal
6465
var swiftProtobufModuleName: String? = nil
6566
var implementationOnlyImports: Bool = false
67+
var generateApplyingMethods: Bool = false
6668

6769
for pair in parameter.parsedPairs {
6870
switch pair.key {
@@ -100,6 +102,13 @@ class GeneratorOptions {
100102
throw GenerationError.invalidParameterValue(name: pair.key,
101103
value: pair.value)
102104
}
105+
case "Applying":
106+
if let value = Bool(pair.value) {
107+
generateApplyingMethods = value
108+
} else {
109+
throw GenerationError.invalidParameterValue(name: pair.key,
110+
value: pair.value)
111+
}
103112
default:
104113
throw GenerationError.unknownParameter(name: pair.key)
105114
}
@@ -119,6 +128,7 @@ class GeneratorOptions {
119128

120129
self.outputNaming = outputNaming
121130
self.visibility = visibility
131+
self.generateApplyingMethods = generateApplyingMethods
122132

123133
switch visibility {
124134
case .internal:

Sources/protoc-gen-swift/MessageGenerator.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,10 @@ class MessageGenerator {
194194
}
195195

196196
func generateRuntimeSupport(printer p: inout CodePrinter, file: FileGenerator, parent: MessageGenerator?) {
197+
let protoApplyingExtension = generatorOptions.generateApplyingMethods ? ", \(namer.swiftProtobufModulePrefix)_ProtoApplying" : ""
197198
p.print(
198199
"",
199-
"extension \(swiftFullName): \(namer.swiftProtobufModulePrefix)Message, \(namer.swiftProtobufModulePrefix)_MessageImplementationBase, \(namer.swiftProtobufModulePrefix)_ProtoNameProviding, \(namer.swiftProtobufModulePrefix)_ProtoApplying {")
200+
"extension \(swiftFullName): \(namer.swiftProtobufModulePrefix)Message, \(namer.swiftProtobufModulePrefix)_MessageImplementationBase, \(namer.swiftProtobufModulePrefix)_ProtoNameProviding\(protoApplyingExtension) {")
200201
p.withIndentation { p in
201202
if let parent = parent {
202203
p.print("\(visibility)static let protoMessageName: String = \(parent.swiftFullName).protoMessageName + \".\(descriptor.name)\"")
@@ -220,8 +221,10 @@ class MessageGenerator {
220221
generateTraverse(printer: &p)
221222
p.print()
222223
generateMessageEquality(printer: &p)
223-
p.print()
224-
generateMessageApplying(printer: &p)
224+
if generatorOptions.generateApplyingMethods {
225+
p.print()
226+
generateMessageApplying(printer: &p)
227+
}
225228
}
226229
p.print("}")
227230

@@ -408,7 +411,7 @@ class MessageGenerator {
408411
p.print("\(visibility)func applying(_ value: Any, for fieldNumber: Int) throws -> \(swiftFullName) {")
409412
if fields.isEmpty {
410413
p.withIndentation { p in
411-
p.print("throw \(namer.swiftProtobufModulePrefix)MessageModificationError.invalidFieldNumber")
414+
p.print("throw \(namer.swiftProtobufModulePrefix)ProtoApplyingError.invalidFieldNumber")
412415
}
413416
p.print("}")
414417
return

0 commit comments

Comments
 (0)