-
Notifications
You must be signed in to change notification settings - Fork 435
[CodeGenLib] Translating dependencies into StructuredSwiftRepresentation #1752
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
Conversation
Motivation: Dependency representations within CodeGeneratorRequest and StructuredSwiftRepresentation don't match (each contains fields that the other one doesn't have). The CodeGenerationRequest representation of a dependency doesn't contain preconcurrency requirements and spi, while the StructuredSwiftRepresentation doesn't allow items imports (func, class etc.). We need to add the missing fields to both representation in order to translate the imports, without losing information. Modifications: - Added the preconcurrency ans spi parameters to the CodeGenerationRequest Dependency struct (and the PreconcurrencyRequirement struct to represent that encapsulates the possible requirement types) - Added the item property to the StructuredSwiftRepresentation ImportDescription struct (and the Item struct backed up by an item representing the possible cases). - Created the function that translates a CodeGenerationRequest.Dependency into a StructuredSwiftRepresentation.ImportDescription. - Added the item rendering in the TextBasedRenderer. - Added tests for the TextRenderer and the IDLToStructuredSwiftRepresentationTranslator that check the imports. Result: Imports can now be translated and added in the generated code.
gjcairo
left a comment
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.
Looking good, left only a few small comments.
| case always | ||
| case never | ||
| case onOS([String]) |
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.
Nit: I wonder if required, notRequired and requiredOnOS([String]) would be better names here?
| let imports = | ||
| try [ImportDescription(moduleName: "GRPCCore")] | ||
| + codeGenerationRequest.dependencies.map { try self.translateImport(dependency: $0) } |
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.
Nit: you can replace this map+concatenation with a reduce(into:):
| let imports = | |
| try [ImportDescription(moduleName: "GRPCCore")] | |
| + codeGenerationRequest.dependencies.map { try self.translateImport(dependency: $0) } | |
| let imports = try codeGenerationRequest.dependencies.reduce( | |
| into: [ImportDescription(moduleName: "GRPCCore")] | |
| ) { partialResult, newDependency in | |
| try partialResult.append(translateImport(dependency: newDependency)) | |
| } |
| if let matchedKind = ImportDescription.Kind.allCases.first(where: { | ||
| "\($0)" == item.kind.value.description | ||
| }) { |
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.
ImportDescription.Kind has a raw value of String. This means that it provides an init?(rawValue:) initialiser. You can take advantage of it to avoid iterating over all cases as you're doing here - if the provided raw value doesn't match an enum case, the initialiser will return nil:
| if let matchedKind = ImportDescription.Kind.allCases.first(where: { | |
| "\($0)" == item.kind.value.description | |
| }) { | |
| if let matchedKind = ImportDescription.Kind(rawValue: item.kind.value.description) { |
| public struct Kind { | ||
| /// Describes the keyword associated with the imported item. | ||
| internal enum Value { | ||
| internal enum Value: CustomStringConvertible { |
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 bit of a nit, but ImportDescription.Kind, which basically mirrors this type, has a String raw value instead of conforming to CustomStringConvertible. Is there a reason why this enum conforms to CustomStringConvertible instead of doing the same as ImportDescription.Kind?
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.
Not really, I will use raw values instead in both cases
| let preconcurrency = dependency.preconcurrency.value | ||
| switch preconcurrency { |
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.
Nit, but as you're not using preconcurrency anywhere else, you can switch directly over the value without declaring the let before:
| let preconcurrency = dependency.preconcurrency.value | |
| switch preconcurrency { | |
| switch dependency.preconcurrency.value { |
…ion (grpc#1752) Translating dependencies into StructuredSwiftRepresentation Motivation: Dependency representations within CodeGeneratorRequest and StructuredSwiftRepresentation don't match (each contains fields that the other one doesn't have). The CodeGenerationRequest representation of a dependency doesn't contain preconcurrency requirements and spi, while the StructuredSwiftRepresentation doesn't allow items imports (func, class etc.). We need to add the missing fields to both representation in order to translate the imports, without losing information. Modifications: - Added the preconcurrency ans spi parameters to the CodeGenerationRequest Dependency struct (and the PreconcurrencyRequirement struct to represent that encapsulates the possible requirement types) - Added the item property to the StructuredSwiftRepresentation ImportDescription struct (and the Item struct backed up by an item representing the possible cases). - Created the function that translates a CodeGenerationRequest.Dependency into a StructuredSwiftRepresentation.ImportDescription. - Added the item rendering in the TextBasedRenderer. - Added tests for the TextRenderer and the IDLToStructuredSwiftRepresentationTranslator that check the imports. Result: Imports can now be translated and added in the generated code.
Motivation:
Dependency representations within CodeGeneratorRequest (Dependency) and StructuredSwiftRepresentation (ImportDescription) don't match, as each contains fields that the other one doesn't have. The CodeGenerationRequest representation of a dependency doesn't contain the preconcurrency requirement and spi, while the StructuredSwiftRepresentation doesn't allow importing items (func, class etc.). We need to add the missing fields to both representations in order to translate the imports, without losing information.
Modifications:
Result:
Imports can now be translated and added in the generated code.