-
Notifications
You must be signed in to change notification settings - Fork 420
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
Support all_extension_numbers_of_type reflection requests #1680
Changes from 5 commits
b618a11
a0c3f65
dfd4fcb
f5e38e4
e398aa3
c5042f0
abfffd6
d5027ed
9537ac6
819735a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,12 +52,16 @@ internal struct ReflectionServiceData: Sendable { | |
internal var serviceNames: [String] | ||
internal var fileNameBySymbol: [String: String] | ||
private var fileNameByExtensionDescriptor: [ExtensionDescriptor: String] | ||
private var fieldNumbersByType: [String: [Int32]] | ||
private var messageTypeNames: [String] | ||
|
||
internal init(fileDescriptors: [Google_Protobuf_FileDescriptorProto]) throws { | ||
self.serviceNames = [] | ||
self.fileDescriptorDataByFilename = [:] | ||
self.fileNameBySymbol = [:] | ||
self.fileNameByExtensionDescriptor = [:] | ||
self.fieldNumbersByType = [:] | ||
self.messageTypeNames = [] | ||
|
||
for fileDescriptorProto in fileDescriptors { | ||
let serializedFileDescriptorProto: Data | ||
|
@@ -92,10 +96,13 @@ internal struct ReflectionServiceData: Sendable { | |
} | ||
} | ||
|
||
// Populating the <extension descriptor, file name> dictionary. | ||
// Populating the <extension descriptor, file name> dictionary and the <typeName, [FieldNumber]> one. | ||
for `extension` in fileDescriptorProto.extension { | ||
let typeName = ReflectionServiceData.extractTypeNameFrom( | ||
fullyQualifiedName: `extension`.extendee | ||
) | ||
let extensionDescriptor = ExtensionDescriptor( | ||
extendeeTypeName: `extension`.extendee, | ||
extendeeTypeName: typeName, | ||
fieldNumber: `extension`.number | ||
) | ||
let oldFileName = self.fileNameByExtensionDescriptor.updateValue( | ||
|
@@ -112,8 +119,28 @@ internal struct ReflectionServiceData: Sendable { | |
""" | ||
) | ||
} | ||
if self.fieldNumbersByType[typeName] == nil { | ||
self.fieldNumbersByType[typeName] = [] | ||
} | ||
let numberPosition = self.fieldNumbersByType[typeName]!.count | ||
self.fieldNumbersByType[typeName]?.insert( | ||
`extension`.number, | ||
at: numberPosition | ||
) | ||
} | ||
// Populating messageTypeNames array. | ||
self.messageTypeNames.append( | ||
contentsOf: fileDescriptorProto.qualifiedMessageTypes | ||
) | ||
} | ||
} | ||
|
||
internal static func extractTypeNameFrom(fullyQualifiedName name: String) -> String { | ||
var nameCopy = name | ||
if nameCopy.first == "." { | ||
nameCopy.removeFirst() | ||
} | ||
return nameCopy | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need a whole function for this, it's just a one liner: |
||
} | ||
|
||
internal func serialisedFileDescriptorProtosForDependenciesOfFile( | ||
|
@@ -151,12 +178,20 @@ internal struct ReflectionServiceData: Sendable { | |
} | ||
|
||
internal func nameOfFileContainingExtension( | ||
named extendeeName: String, | ||
extendeeName: String, | ||
fieldNumber number: Int32 | ||
) -> String? { | ||
let key = ExtensionDescriptor(extendeeTypeName: extendeeName, fieldNumber: number) | ||
return self.fileNameByExtensionDescriptor[key] | ||
} | ||
|
||
internal func extensionsFieldNumbersOfType(named typeName: String) -> [Int32]? { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optional collections are rarely the right thing to do. Are we sure that we should return There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was following the pattern used for the other requests too, meaning that the functions of the registry could return nil, when the thing we are requesting for doesn't exist and the Reflection Service itself "decides" whether or not it should throw an error / send an error response. Should I change this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it make sense to distinguish between a |
||
return self.fieldNumbersByType[typeName] | ||
} | ||
|
||
internal func containsMessageType(name typeName: String) -> Bool { | ||
return self.messageTypeNames.contains(typeName) | ||
} | ||
} | ||
|
||
@available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *) | ||
|
@@ -216,7 +251,7 @@ internal final class ReflectionServiceProvider: Reflection_ServerReflectionAsync | |
) throws -> Reflection_ServerReflectionResponse { | ||
guard | ||
let fileName = self.protoRegistry.nameOfFileContainingExtension( | ||
named: extensionRequest.containingType, | ||
extendeeName: extensionRequest.containingType, | ||
fieldNumber: extensionRequest.extensionNumber | ||
) | ||
else { | ||
|
@@ -228,6 +263,29 @@ internal final class ReflectionServiceProvider: Reflection_ServerReflectionAsync | |
return try self.findFileByFileName(fileName, request: request) | ||
} | ||
|
||
internal func findExtensionsFieldNumbersOfType( | ||
named typeName: String, | ||
request: Reflection_ServerReflectionRequest | ||
) throws -> Reflection_ServerReflectionResponse { | ||
var fieldNumbers = self.protoRegistry.extensionsFieldNumbersOfType(named: typeName) | ||
if fieldNumbers == nil { | ||
guard self.protoRegistry.containsMessageType(name: typeName) else { | ||
throw GRPCStatus( | ||
code: .notFound, | ||
message: "The provided type doesn't have any extensions." | ||
) | ||
} | ||
fieldNumbers = [] | ||
} | ||
return Reflection_ServerReflectionResponse( | ||
request: request, | ||
extensionNumberResponse: .with { | ||
$0.baseTypeName = typeName | ||
$0.extensionNumber = fieldNumbers! | ||
} | ||
) | ||
} | ||
|
||
internal func serverReflectionInfo( | ||
requestStream: GRPCAsyncRequestStream<Reflection_ServerReflectionRequest>, | ||
responseStream: GRPCAsyncResponseStreamWriter<Reflection_ServerReflectionResponse>, | ||
|
@@ -260,6 +318,13 @@ internal final class ReflectionServiceProvider: Reflection_ServerReflectionAsync | |
) | ||
try await responseStream.send(response) | ||
|
||
case let .allExtensionNumbersOfType(typeName): | ||
let response = try self.findExtensionsFieldNumbersOfType( | ||
named: typeName, | ||
request: request | ||
) | ||
try await responseStream.send(response) | ||
|
||
default: | ||
throw GRPCStatus(code: .unimplemented) | ||
} | ||
|
@@ -289,6 +354,17 @@ extension Reflection_ServerReflectionResponse { | |
$0.listServicesResponse = listServicesResponse | ||
} | ||
} | ||
|
||
init( | ||
request: Reflection_ServerReflectionRequest, | ||
extensionNumberResponse: Reflection_ExtensionNumberResponse | ||
) { | ||
self = .with { | ||
$0.validHost = request.host | ||
$0.originalRequest = request | ||
$0.allExtensionNumbersResponse = extensionNumberResponse | ||
} | ||
} | ||
} | ||
|
||
extension Google_Protobuf_FileDescriptorProto { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of notes:
insert
if you want to add an element to the end of the array, useappend