Skip to content

Commit 8c7da21

Browse files
committed
Move the plugin over to the new helpers.
1 parent 2b0f7fc commit 8c7da21

File tree

4 files changed

+35
-66
lines changed

4 files changed

+35
-66
lines changed

Sources/protoc-gen-swift/GenerationError.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,19 @@ enum GenerationError: Error {
1515
case invalidParameterValue(name: String, value: String)
1616
/// Raised to wrap another error but provide a context message.
1717
case wrappedError(message: String, error: Error)
18+
/// Raised with an specific message
19+
case message(message: String)
20+
21+
var description: String {
22+
switch self {
23+
case .unknownParameter(let name):
24+
return "Unknown generation parameter '\(name)'"
25+
case .invalidParameterValue(let name, let value):
26+
return "Unknown value for generation parameter '\(name)': '\(value)'"
27+
case .wrappedError(let message, let error):
28+
return "\(message): \(error)"
29+
case .message(let message):
30+
return message
31+
}
32+
}
1833
}

Sources/protoc-gen-swift/GeneratorOptions.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ class GeneratorOptions {
5757
/// A string snippet to insert for the visibility
5858
let visibilitySourceSnippet: String
5959

60-
init(parameter: String?) throws {
60+
init(parameter: CodeGeneratorParameter) throws {
6161
var outputNaming: OutputNaming = .fullPath
6262
var moduleMapPath: String?
6363
var visibility: Visibility = .internal
6464
var swiftProtobufModuleName: String? = nil
6565
var implementationOnlyImports: Bool = false
6666

67-
for pair in parseParameter(string:parameter) {
67+
for pair in parameter.parsedPairs {
6868
switch pair.key {
6969
case "FileNaming":
7070
if let naming = OutputNaming(flag: pair.value) {

Sources/protoc-gen-swift/StringUtils.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,28 +35,6 @@ func splitPath(pathname: String) -> (dir:String, base:String, suffix:String) {
3535
return (dir: dir, base: base, suffix: suffix)
3636
}
3737

38-
func partition(string: String, atFirstOccurrenceOf substring: String) -> (String, String) {
39-
guard let index = string.range(of: substring)?.lowerBound else {
40-
return (string, "")
41-
}
42-
return (String(string[..<index]),
43-
String(string[string.index(after: index)...]))
44-
}
45-
46-
func parseParameter(string: String?) -> [(key:String, value:String)] {
47-
guard let string = string, !string.isEmpty else {
48-
return []
49-
}
50-
let parts = string.components(separatedBy: ",")
51-
let asPairs = parts.map { partition(string: $0, atFirstOccurrenceOf: "=") }
52-
let result = asPairs.map { (key:trimWhitespace($0), value:trimWhitespace($1)) }
53-
return result
54-
}
55-
56-
func trimWhitespace(_ s: String) -> String {
57-
return s.trimmingCharacters(in: .whitespacesAndNewlines)
58-
}
59-
6038
/// The protoc parser emits byte literals using an escaped C convention.
6139
/// Fortunately, it uses only a limited subset of the C escapse:
6240
/// \n\r\t\\\'\" and three-digit octal escapes but nothing else.

Sources/protoc-gen-swift/main.swift

Lines changed: 18 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ import Foundation
2323
import SwiftProtobuf
2424
import SwiftProtobufPluginLibrary
2525

26-
extension Google_Protobuf_Compiler_Version {
27-
fileprivate var versionString: String {
28-
if !suffix.isEmpty {
29-
return "\(major).\(minor).\(patch).\(suffix)"
30-
}
31-
return "\(major).\(minor).\(patch)"
32-
}
33-
}
34-
35-
struct GeneratorPlugin {
26+
struct GeneratorPlugin: CodeGenerator {
3627
private enum Mode {
3728
case showHelp
3829
case showVersion
@@ -154,7 +145,7 @@ struct GeneratorPlugin {
154145
}
155146

156147
auditProtoCVersion(request: request)
157-
let response = generate(request: request)
148+
let response = generateCode(request: request, generator: self)
158149
guard sendReply(response: response) else { return 1 }
159150
return 0
160151
}
@@ -182,7 +173,7 @@ struct GeneratorPlugin {
182173
continue
183174
}
184175

185-
let response = generate(request: request)
176+
let response = generateCode(request: request, generator: self)
186177
if response.hasError {
187178
Stderr.print("Error while generating from \(p) - \(response.error)")
188179
result = 1
@@ -198,47 +189,32 @@ struct GeneratorPlugin {
198189
return result
199190
}
200191

201-
private func generate(
202-
request: Google_Protobuf_Compiler_CodeGeneratorRequest
203-
) -> Google_Protobuf_Compiler_CodeGeneratorResponse {
204-
let options: GeneratorOptions
205-
do {
206-
options = try GeneratorOptions(parameter: request.parameter)
207-
} catch GenerationError.unknownParameter(let name) {
208-
return Google_Protobuf_Compiler_CodeGeneratorResponse(
209-
error: "Unknown generation parameter '\(name)'")
210-
} catch GenerationError.invalidParameterValue(let name, let value) {
211-
return Google_Protobuf_Compiler_CodeGeneratorResponse(
212-
error: "Unknown value for generation parameter '\(name)': '\(value)'")
213-
} catch GenerationError.wrappedError(let message, let e) {
214-
return Google_Protobuf_Compiler_CodeGeneratorResponse(error: "\(message): \(e)")
215-
} catch let e {
216-
return Google_Protobuf_Compiler_CodeGeneratorResponse(
217-
error: "Internal Error parsing request options: \(e)")
218-
}
219-
220-
let descriptorSet = DescriptorSet(protos: request.protoFile)
192+
func generate(
193+
files: [SwiftProtobufPluginLibrary.FileDescriptor],
194+
parameter: CodeGeneratorParameter,
195+
protoCompilerContext: SwiftProtobufPluginLibrary.ProtoCompilerContext,
196+
generatorOutputs: SwiftProtobufPluginLibrary.GeneratorOutputs
197+
) throws {
198+
let options = try GeneratorOptions(parameter: parameter)
221199

222200
var errorString: String? = nil
223-
var responseFiles: [Google_Protobuf_Compiler_CodeGeneratorResponse.File] = []
224-
for name in request.fileToGenerate {
225-
let fileDescriptor = descriptorSet.fileDescriptor(named: name)!
201+
for fileDescriptor in files {
226202
let fileGenerator = FileGenerator(fileDescriptor: fileDescriptor, generatorOptions: options)
227203
var printer = CodePrinter(addNewlines: true)
228204
fileGenerator.generateOutputFile(printer: &printer, errorString: &errorString)
229205
if let errorString = errorString {
230206
// If generating multiple files, scope the message with the file that triggered it.
231-
let fullError = request.fileToGenerate.count > 1 ? "\(name): \(errorString)" : errorString
232-
return Google_Protobuf_Compiler_CodeGeneratorResponse(error: fullError)
207+
let fullError = files.count > 1 ? "\(fileDescriptor.name): \(errorString)" : errorString
208+
throw GenerationError.message(message: fullError)
233209
}
234-
responseFiles.append(
235-
Google_Protobuf_Compiler_CodeGeneratorResponse.File(name: fileGenerator.outputFilename,
236-
content: printer.content))
210+
try generatorOutputs.add(fileName: fileGenerator.outputFilename, contents: printer.content)
237211
}
238-
return Google_Protobuf_Compiler_CodeGeneratorResponse(files: responseFiles,
239-
supportedFeatures: [.proto3Optional])
240212
}
241213

214+
var supportedFeatures: [SwiftProtobufPluginLibrary.Google_Protobuf_Compiler_CodeGeneratorResponse.Feature] = [
215+
.proto3Optional,
216+
]
217+
242218
private func auditProtoCVersion(request: Google_Protobuf_Compiler_CodeGeneratorRequest) {
243219
guard request.hasCompilerVersion else {
244220
Stderr.print("WARNING: unknown version of protoc, use 3.2.x or later to ensure JSON support is correct.")

0 commit comments

Comments
 (0)