Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ extension Target {
dependencies: [
.protobuf,
.protobufPluginLibrary,
.grpcCodeGen
.grpcCodeGen,
.grpcProtobufCodeGen
],
exclude: [
"README.md",
Expand Down
7 changes: 7 additions & 0 deletions Sources/GRPCCodeGen/SourceGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ public struct SourceGenerator: Sendable {
/// Whether or not server code should be generated.
public var server: Bool

public init(accessLevel: AccessLevel, client: Bool, server: Bool, indentation: Int = 4) {
self.accessLevel = accessLevel
self.indentation = indentation
self.client = client
self.server = server
}

/// The possible access levels for the generated code.
public struct AccessLevel: Sendable, Hashable {
internal var level: Level
Expand Down
35 changes: 35 additions & 0 deletions Sources/GRPCProtobufCodeGen/ProtobufCodeGenerator.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2024, gRPC Authors All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import GRPCCodeGen
import SwiftProtobufPluginLibrary

public struct ProtobufCodeGenerator {
internal var configuration: SourceGenerator.Configuration

public init(configuration: SourceGenerator.Configuration) {
self.configuration = configuration
}

public func generateCode(from fileDescriptor: FileDescriptor) throws -> String {
let parser = ProtobufCodeGenParser()
let sourceGenerator = SourceGenerator(configuration: self.configuration)

let codeGenerationRequest = try parser.parse(input: fileDescriptor)
let sourceFile = try sourceGenerator.generate(codeGenerationRequest)
return sourceFile.contents
}
}
32 changes: 30 additions & 2 deletions Sources/protoc-gen-grpc-swift/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/
import Foundation
import GRPCCodeGen
import GRPCProtobufCodeGen
import SwiftProtobuf
import SwiftProtobufPluginLibrary

Expand Down Expand Up @@ -166,9 +168,16 @@ func main(args: [String]) throws {
fileNamingOption: options.fileNaming,
generatedFiles: &generatedFiles
)
let grpcGenerator = Generator(fileDescriptor, options: options)
if options.v2 {
let grpcGenerator = ProtobufCodeGenerator(
configuration: SourceGenerator.Configuration(options: options)
)
grpcFile.content = try grpcGenerator.generateCode(from: fileDescriptor)
} else {
let grpcGenerator = Generator(fileDescriptor, options: options)
grpcFile.content = grpcGenerator.code
}
grpcFile.name = grpcFileName
grpcFile.content = grpcGenerator.code
response.file.append(grpcFile)
}
}
Expand All @@ -184,3 +193,22 @@ do {
} catch {
Log("ERROR: \(error)")
}

extension SourceGenerator.Configuration {
init(options: GeneratorOptions) {
let accessLevel: SourceGenerator.Configuration.AccessLevel
switch options.visibility {
case .internal:
accessLevel = .internal
case .package:
accessLevel = .package
case .public:
accessLevel = .public
}
self.init(
accessLevel: accessLevel,
client: options.generateClient,
server: options.generateServer
)
}
}
8 changes: 8 additions & 0 deletions Sources/protoc-gen-grpc-swift/options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class GeneratorOptions {
private(set) var gRPCModuleName = "GRPC"
private(set) var swiftProtobufModuleName = "SwiftProtobuf"
private(set) var generateReflectionData = false
private(set) var v2 = false

init(parameter: String?) throws {
for pair in GeneratorOptions.parseParameter(string: parameter) {
Expand Down Expand Up @@ -154,6 +155,13 @@ final class GeneratorOptions {
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
}

case "_V2":
if let value = Bool(pair.value) {
self.v2 = value
} else {
throw GenerationError.invalidParameterValue(name: pair.key, value: pair.value)
}

default:
throw GenerationError.unknownParameter(name: pair.key)
}
Expand Down
18 changes: 13 additions & 5 deletions Tests/GRPCProtobufCodeGenTests/ProtobufCodeGenParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ import XCTest

final class ProtobufCodeGenParserTests: XCTestCase {
func testParser() throws {
let descriptorSet = DescriptorSet(protos: [Google_Protobuf_FileDescriptorProto.helloWorld])
guard let fileDescriptor = descriptorSet.fileDescriptor(named: "helloworld.proto") else {
return XCTFail(
"""
Could not find the file descriptor of "helloworld.proto".
"""
)
}
let parsedCodeGenRequest = try ProtobufCodeGenParser().parse(
input: self.helloWorldFileDescriptor
input: fileDescriptor
)
XCTAssertEqual(parsedCodeGenRequest.fileName, "helloworld.proto")
XCTAssertEqual(
Expand Down Expand Up @@ -105,8 +113,10 @@ final class ProtobufCodeGenParserTests: XCTestCase {
CodeGenerationRequest.Dependency(module: "GRPCProtobuf")
)
}
}

var helloWorldFileDescriptor: FileDescriptor {
extension Google_Protobuf_FileDescriptorProto {
static var helloWorld: Google_Protobuf_FileDescriptorProto {
let requestType = Google_Protobuf_DescriptorProto.with {
$0.name = "HelloRequest"
$0.field = [
Expand Down Expand Up @@ -144,7 +154,7 @@ final class ProtobufCodeGenParserTests: XCTestCase {
}
]
}
let protoDescriptor = Google_Protobuf_FileDescriptorProto.with {
return Google_Protobuf_FileDescriptorProto.with {
$0.name = "helloworld.proto"
$0.package = "helloworld"
$0.messageType = [requestType, responseType]
Expand Down Expand Up @@ -187,7 +197,5 @@ final class ProtobufCodeGenParserTests: XCTestCase {
}
$0.syntax = "proto3"
}
let descriptorSet = DescriptorSet(protos: [protoDescriptor])
return descriptorSet.fileDescriptor(named: "helloworld.proto")!
}
}
Loading