Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added ReflectionService initialiser using file paths #1699

Merged
merged 15 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
58 changes: 58 additions & 0 deletions Sources/GRPCReflectionService/Server/ReflectionService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ public final class ReflectionService: CallHandlerProvider, Sendable {
self.reflectionService.serviceName
}

/// Creates a `ReflectionService` by loading serialized reflection data created by `protoc-gen-grpc-swift`.
///
/// You can generate serialized reflection data using the `protoc-gen-grpc-swift` plugin for `protoc` by
/// setting the to `True`. The paths provided should be absolute or relative to the current working directory.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"setting the to True" ...?

///
/// - Parameter filePaths: The paths to files containing serialized reflection data.
///
/// - Throws: When a file can't be read from disk or parsed.
public init(serializedFileDescriptorProtoFilePaths filePaths: [String]) throws {
let fileDescriptorProtos = try ReflectionService.readSerializedFileDescriptorProtos(
atPaths: filePaths
)
self.reflectionService = try ReflectionServiceProvider(
fileDescriptorProtos: fileDescriptorProtos
)
}

public init(fileDescriptors: [Google_Protobuf_FileDescriptorProto]) throws {
self.reflectionService = try ReflectionServiceProvider(fileDescriptorProtos: fileDescriptors)
}
Expand Down Expand Up @@ -425,3 +442,44 @@ where Success == Grpc_Reflection_V1_ServerReflectionResponse.OneOf_MessageRespon
}
}
}

@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
extension ReflectionService {
static func readSerializedFileDescriptorProto(
atPath path: String
) throws -> Google_Protobuf_FileDescriptorProto {
let fileURL: URL
#if os(Linux)
fileURL = URL(fileURLWithPath: path)
#else
if #available(macOS 13.0, iOS 16.0, tvOS 16.0, watchOS 9.0, *) {
fileURL = URL(filePath: path, directoryHint: .notDirectory)
} else {
fileURL = URL(fileURLWithPath: path)
}
#endif
let binaryData = try Data(contentsOf: fileURL)
guard let serializedData = Data(base64Encoded: binaryData) else {
throw GRPCStatus(
code: .invalidArgument,
message:
"""
The \(path) file contents could not be transformed \
into serialized data representing a file descriptor proto.
"""
)
}
return try Google_Protobuf_FileDescriptorProto(serializedData: serializedData)
}

static func readSerializedFileDescriptorProtos(
atPaths paths: [String]
) throws -> [Google_Protobuf_FileDescriptorProto] {
var fileDescriptorProtos = [Google_Protobuf_FileDescriptorProto]()
glbrntt marked this conversation as resolved.
Show resolved Hide resolved
fileDescriptorProtos.reserveCapacity(paths.count)
for path in paths {
try fileDescriptorProtos.append(readSerializedFileDescriptorProto(atPath: path))
}
return fileDescriptorProtos
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -519,4 +519,80 @@ final class ReflectionServiceUnitTests: GRPCTestCase {

XCTAssertEqual(try extensionsFieldNumbersOfTypeResult.get(), [1, 2, 3, 4, 5, 130])
}

func testReadSerializedFileDescriptorProto() throws {
let initialFileDescriptorProto = generateFileDescriptorProto(fileName: "test", suffix: "1")
let data = try initialFileDescriptorProto.serializedData().base64EncodedData()
#if os(Linux)
let temporaryDirectory = "/tmp/"
#else
let temporaryDirectory = FileManager.default.temporaryDirectory.path()
#endif
let filePath = "\(temporaryDirectory)test\(UUID()).grpc.reflection"
FileManager.default.createFile(atPath: filePath, contents: data)
defer {
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: filePath))
}
let reflectionServiceFileDescriptorProto =
try ReflectionService.readSerializedFileDescriptorProto(atPath: filePath)
XCTAssertEqual(reflectionServiceFileDescriptorProto, initialFileDescriptorProto)
}

func testReadSerializedFileDescriptorProtoInvalidFileContents() throws {
let invalidData = "%%%%%££££".data(using: .utf8)
#if os(Linux)
let temporaryDirectory = "/tmp/"
#else
let temporaryDirectory = FileManager.default.temporaryDirectory.path()
#endif
let filePath = "\(temporaryDirectory)test\(UUID()).grpc.reflection"
FileManager.default.createFile(atPath: filePath, contents: invalidData)
defer {
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: filePath))
}

XCTAssertThrowsGRPCStatus(
try ReflectionService.readSerializedFileDescriptorProto(atPath: filePath)
) {
status in
XCTAssertEqual(
status,
GRPCStatus(
code: .invalidArgument,
message:
"""
The \(filePath) file contents could not be transformed \
into serialized data representing a file descriptor proto.
"""
)
)
}
}

func testReadSerializedFileDescriptorProtos() throws {
let initialFileDescriptorProtos = makeProtosWithDependencies()
var filePaths: [String] = []

for initialFileDescriptorProto in initialFileDescriptorProtos {
let data = try initialFileDescriptorProto.serializedData()
.base64EncodedData()
#if os(Linux)
let temporaryDirectory = "/tmp/"
#else
let temporaryDirectory = FileManager.default.temporaryDirectory.path()
#endif
let filePath = "\(temporaryDirectory)test\(UUID()).grpc.reflection"
FileManager.default.createFile(atPath: filePath, contents: data)
filePaths.append(filePath)
}
defer {
for filePath in filePaths {
XCTAssertNoThrow(try FileManager.default.removeItem(atPath: filePath))
}
}

let reflectionServiceFileDescriptorProtos =
try ReflectionService.readSerializedFileDescriptorProtos(atPaths: filePaths)
XCTAssertEqual(reflectionServiceFileDescriptorProtos, initialFileDescriptorProtos)
}
}
Loading