-
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 6 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 |
---|---|---|
|
@@ -51,13 +51,21 @@ internal struct ReflectionServiceData: Sendable { | |
internal var fileDescriptorDataByFilename: [String: FileDescriptorProtoData] | ||
internal var serviceNames: [String] | ||
internal var fileNameBySymbol: [String: String] | ||
|
||
// Stores the file names for each extension identified by an ExtensionDescriptor object. | ||
private var fileNameByExtensionDescriptor: [ExtensionDescriptor: String] | ||
// Stores the field numbers for each type that has extensions. | ||
private var fieldNumbersByType: [String: [Int32]] | ||
// Stores all the messageTypeNames from the protos. | ||
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 +100,11 @@ 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 = String(`extension`.extendee.drop(while: { $0 == "." })) | ||
let extensionDescriptor = ExtensionDescriptor( | ||
extendeeTypeName: `extension`.extendee, | ||
extendeeTypeName: typeName, | ||
fieldNumber: `extension`.number | ||
) | ||
let oldFileName = self.fileNameByExtensionDescriptor.updateValue( | ||
|
@@ -112,7 +121,12 @@ internal struct ReflectionServiceData: Sendable { | |
""" | ||
) | ||
} | ||
self.fieldNumbersByType[typeName, default: []].append(`extension`.number) | ||
} | ||
// Populating messageTypeNames array. | ||
self.messageTypeNames.append( | ||
contentsOf: fileDescriptorProto.qualifiedMessageTypes | ||
) | ||
} | ||
} | ||
|
||
|
@@ -151,12 +165,21 @@ 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] | ||
} | ||
|
||
// Returns nil if the type has no extensions or if it doesn't exist. | ||
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 +239,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 +251,31 @@ 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 { | ||
// Checks if the typeName is a valid Message type name, that has no extensions, | ||
// or if it is invalid. | ||
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 +308,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 +344,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.
This behaviour is surprising:
nil
usually means the thing we were looking for didn't exist (e.g. considerdictionary["foo"]
). Having it also returnnil
when the type exists but doesn't have any extensions isn't a good API because it places the onus on the caller to work out whether the type exists or not.In this case we should probably just do the work upfront and ensure that
fieldNumbersByType
contains the right information, i.e. empty arrays when the type exists but has no extensions.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.
This is still not right: the only reason we store
messageTypeNames
is to check whether a message exists in this function, it isn't used anywhere else.When we create and populate
fieldNumbersByType
we should do so such it has a value for every type we know about, even if the value is an empty array. It means we do the work upfront and the semantics are clear: if a value exists then the type exists, if no value exists then we don't know about the type.