Skip to content

Conversation

@stefanadranca
Copy link
Collaborator

@stefanadranca stefanadranca commented Jan 3, 2024

Add validation step for input in IDLToStructuredSwiftTranslator

Motivation:

The IDLToStructuredSwiftTranslator should discover errors in the CodeGenerationRequest object, before the specialized translators start their work. This way, if the input is not correct no translator will be started.

Modifications:

Moved the validation functions into a IDLToStructuredSwiftTranslator extension and the associated tests in a separate test file.

Result:

The input validation is not done by each individual specialzed translator, but by the main translator, before transformations take place.

…nslator

Motivation:

The IDLToStructuredSwiftTranslator should discover errors in the CodeGenerationRequest object, before the specialized translators start their work. This way, if the input is not correct no translator will be started.

Modifications:

Moved the validation functions into a IDLToStructuredSwiftTranslator extension and the associated tests in a separate test file.

Result:

The input validation is not done by each individual specialzed translator, but by the main translator, before transformations take place.
@stefanadranca stefanadranca changed the title [CodeGenLib] Add validation step for input in IDLToStructuredSwiftTra… [CodeGenLib] Add validation step for input Jan 3, 2024
@stefanadranca stefanadranca marked this pull request as ready for review January 4, 2024 13:09
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.

LGTM, just left a very small comment

Comment on lines 71 to 83
let noNamespaceServices = servicesByNamespace["", default: []]
let namespaces = servicesByNamespace.keys
for service in noNamespaceServices {
if namespaces.contains(service.name) {
throw CodeGenError(
code: .nonUniqueServiceName,
message: """
Services with no namespace must not have the same names as the namespaces. \
\(service.name) is used as a name for a service with no namespace and a namespace.
"""
)
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Very minor nit, but you can skip the , default: []] subscript and instead make this whole block conditional to it being an empty key:

Suggested change
let noNamespaceServices = servicesByNamespace["", default: []]
let namespaces = servicesByNamespace.keys
for service in noNamespaceServices {
if namespaces.contains(service.name) {
throw CodeGenError(
code: .nonUniqueServiceName,
message: """
Services with no namespace must not have the same names as the namespaces. \
\(service.name) is used as a name for a service with no namespace and a namespace.
"""
)
}
}
if let noNamespaceServices = servicesByNamespace[""] {
let namespaces = servicesByNamespace.keys
for service in noNamespaceServices {
if namespaces.contains(service.name) {
throw CodeGenError(
code: .nonUniqueServiceName,
message: """
Services with no namespace must not have the same names as the namespaces. \
\(service.name) is used as a name for a service with no namespace and a namespace.
"""
)
}
}
}

grouping: codeGenerationRequest.services,
by: { $0.namespace }
)
try checkServiceNamesAreUnique(for: servicesByNamespace)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
try checkServiceNamesAreUnique(for: servicesByNamespace)
try self.checkServiceNamesAreUnique(for: servicesByNamespace)

)
try checkServiceNamesAreUnique(for: servicesByNamespace)
for service in codeGenerationRequest.services {
try checkMethodNamesAreUnique(in: service)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
try checkMethodNamesAreUnique(in: service)
try self.checkMethodNamesAreUnique(in: service)

@stefanadranca stefanadranca requested a review from glbrntt January 8, 2024 17:40
@stefanadranca stefanadranca merged commit ced0d70 into grpc:main Jan 11, 2024
glbrntt pushed a commit to glbrntt/grpc-swift that referenced this pull request Feb 5, 2024
Add validation step for input in IDLToStructuredSwiftTranslator

Motivation:

The IDLToStructuredSwiftTranslator should discover errors in the CodeGenerationRequest object, before the specialized translators start their work. This way, if the input is not correct no translator will be started.

Modifications:

Moved the validation functions into a IDLToStructuredSwiftTranslator extension and the associated tests in a separate test file.

Result:

The input validation is not done by each individual specialzed translator, but by the main translator, before transformations take place.
@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