Skip to content

Conversation

@stefanadranca
Copy link
Collaborator

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:

  • Added the preconcurrency and spi parameters to the CodeGenerationRequest Dependency struct (and the PreconcurrencyRequirement struct that encapsulates the possible requirement types).
  • Added the item property to the StructuredSwiftRepresentation ImportDescription struct (and the Item struct backed up by an enum 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 TextBasedRenderer and the IDLToStructuredSwiftRepresentationTranslator that check the imports.

Result:

Imports can now be translated and added in the generated code.

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.
Copy link
Collaborator

@gjcairo gjcairo left a 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.

Comment on lines 203 to 205
case always
case never
case onOS([String])
Copy link
Collaborator

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?

Comment on lines 27 to 29
let imports =
try [ImportDescription(moduleName: "GRPCCore")]
+ codeGenerationRequest.dependencies.map { try self.translateImport(dependency: $0) }
Copy link
Collaborator

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:):

Suggested change
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))
}

Comment on lines 58 to 60
if let matchedKind = ImportDescription.Kind.allCases.first(where: {
"\($0)" == item.kind.value.description
}) {
Copy link
Collaborator

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:

Suggested change
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 {
Copy link
Collaborator

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?

Copy link
Collaborator Author

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

Comment on lines 73 to 74
let preconcurrency = dependency.preconcurrency.value
switch preconcurrency {
Copy link
Collaborator

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:

Suggested change
let preconcurrency = dependency.preconcurrency.value
switch preconcurrency {
switch dependency.preconcurrency.value {

@stefanadranca stefanadranca requested a review from glbrntt January 8, 2024 17:17
@stefanadranca stefanadranca merged commit f7641a0 into grpc:main Jan 12, 2024
glbrntt pushed a commit to glbrntt/grpc-swift that referenced this pull request Feb 5, 2024
…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.
@glbrntt glbrntt added the version/v2 Relates to v2 label Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

version/v2 Relates to v2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants