|
|
@@ -22,9 +22,19 @@ import struct GRPCCodeGen.CodeGenerationRequest
|
|
|
|
|
|
/// Parses a ``FileDescriptor`` object into a ``CodeGenerationRequest`` object.
|
|
|
internal struct ProtobufCodeGenParser {
|
|
|
- internal init() {}
|
|
|
- internal func parse(input: FileDescriptor) throws -> CodeGenerationRequest {
|
|
|
- var header = input.header
|
|
|
+ let input: FileDescriptor
|
|
|
+ let namer: SwiftProtobufNamer
|
|
|
+
|
|
|
+ internal init(input: FileDescriptor, protoFileModuleMappings: ProtoFileToModuleMappings) {
|
|
|
+ self.input = input
|
|
|
+ self.namer = SwiftProtobufNamer(
|
|
|
+ currentFile: input,
|
|
|
+ protoFileToModuleMappings: protoFileModuleMappings
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ internal func parse() throws -> CodeGenerationRequest {
|
|
|
+ var header = self.input.header
|
|
|
// Ensuring there is a blank line after the header.
|
|
|
if !header.isEmpty && !header.hasSuffix("\n\n") {
|
|
|
header.append("\n")
|
|
|
@@ -34,13 +44,13 @@ internal struct ProtobufCodeGenParser {
|
|
|
// swift-format-ignore-file
|
|
|
//
|
|
|
// Generated by the gRPC Swift generator plugin for the protocol buffer compiler.
|
|
|
- // Source: \(input.name)
|
|
|
+ // Source: \(self.input.name)
|
|
|
//
|
|
|
// For information on using the generated types, please see the documentation:
|
|
|
// https://github.com/grpc/grpc-swift
|
|
|
|
|
|
"""
|
|
|
- var dependencies = input.dependencies.map {
|
|
|
+ var dependencies = self.input.dependencies.map {
|
|
|
CodeGenerationRequest.Dependency(module: $0.name)
|
|
|
}
|
|
|
dependencies.append(CodeGenerationRequest.Dependency(module: "GRPCProtobuf"))
|
|
|
@@ -50,12 +60,16 @@ internal struct ProtobufCodeGenParser {
|
|
|
let lookupDeserializer: (String) -> String = { messageType in
|
|
|
"ProtobufDeserializer<\(messageType)>()"
|
|
|
}
|
|
|
- let services = input.services.map {
|
|
|
- CodeGenerationRequest.ServiceDescriptor(descriptor: $0, package: input.package)
|
|
|
+ let services = self.input.services.map {
|
|
|
+ CodeGenerationRequest.ServiceDescriptor(
|
|
|
+ descriptor: $0,
|
|
|
+ package: input.package,
|
|
|
+ protobufNamer: self.namer
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
return CodeGenerationRequest(
|
|
|
- fileName: input.name,
|
|
|
+ fileName: self.input.name,
|
|
|
leadingTrivia: header + leadingTrivia,
|
|
|
dependencies: dependencies,
|
|
|
services: services,
|
|
|
@@ -66,9 +80,16 @@ internal struct ProtobufCodeGenParser {
|
|
|
}
|
|
|
|
|
|
extension CodeGenerationRequest.ServiceDescriptor {
|
|
|
- fileprivate init(descriptor: ServiceDescriptor, package: String) {
|
|
|
+ fileprivate init(
|
|
|
+ descriptor: ServiceDescriptor,
|
|
|
+ package: String,
|
|
|
+ protobufNamer: SwiftProtobufNamer
|
|
|
+ ) {
|
|
|
let methods = descriptor.methods.map {
|
|
|
- CodeGenerationRequest.ServiceDescriptor.MethodDescriptor(descriptor: $0)
|
|
|
+ CodeGenerationRequest.ServiceDescriptor.MethodDescriptor(
|
|
|
+ descriptor: $0,
|
|
|
+ protobufNamer: protobufNamer
|
|
|
+ )
|
|
|
}
|
|
|
let name = CodeGenerationRequest.Name(
|
|
|
base: descriptor.name,
|
|
|
@@ -86,7 +107,7 @@ extension CodeGenerationRequest.ServiceDescriptor {
|
|
|
}
|
|
|
|
|
|
extension CodeGenerationRequest.ServiceDescriptor.MethodDescriptor {
|
|
|
- fileprivate init(descriptor: MethodDescriptor) {
|
|
|
+ fileprivate init(descriptor: MethodDescriptor, protobufNamer: SwiftProtobufNamer) {
|
|
|
let name = CodeGenerationRequest.Name(
|
|
|
base: descriptor.name,
|
|
|
generatedUpperCase: NamingUtils.toUpperCamelCase(descriptor.name),
|
|
|
@@ -98,8 +119,8 @@ extension CodeGenerationRequest.ServiceDescriptor.MethodDescriptor {
|
|
|
name: name,
|
|
|
isInputStreaming: descriptor.clientStreaming,
|
|
|
isOutputStreaming: descriptor.serverStreaming,
|
|
|
- inputType: descriptor.inputType.name,
|
|
|
- outputType: descriptor.outputType.name
|
|
|
+ inputType: protobufNamer.fullName(message: descriptor.inputType),
|
|
|
+ outputType: protobufNamer.fullName(message: descriptor.outputType)
|
|
|
)
|
|
|
}
|
|
|
}
|