Skip to content
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

Add support for parsing registrations wrapped in #if conditions #96

Merged
merged 1 commit into from
Nov 17, 2023

Conversation

skorulis-ap
Copy link
Collaborator

@skorulis-ap skorulis-ap commented Nov 11, 2023

This resolves an issue I found where a type I registered was only available behind a conditional build flag. References to this type were being put into the type safety and unit tests which would fail depending on the build configuration.

This change identifies #if statements, stores the condition and then wraps any generated code related to the registration with the original #if.

Limitations

  • #else is not supported to prevent creating relationships between registrations.
  • Nested #if statements are not supported only due to the extra parsing complexity. This can be added in the future.

@@ -32,6 +36,10 @@ public struct Registration: Equatable, Codable {
self.getterConfig = getterConfig
}

private enum CodingKeys: CodingKey {
case service, name, accessLevel, arguments, isForwarded, getterConfig
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ignoring the ifConfigExpression for now. If it's needed in the future we will need to add custom encoding logic

Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: can you add a small comment that the list is there just to exclude the ifConfigCondition property?

@skorulis-ap skorulis-ap force-pushed the skorulis/ifdef branch 3 times, most recently from 63a7c7e to 6a2dc3b Compare November 11, 2023 04:48
@skorulis-ap skorulis-ap marked this pull request as ready for review November 13, 2023 02:59
@skorulis-ap skorulis-ap changed the title Add support for parsing #if conditions and storing them with the registration Add support for parsing registrations wrapped in #if conditions Nov 13, 2023
Copy link
Collaborator

@jmesmith jmesmith left a comment

Choose a reason for hiding this comment

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

nice tests ✨

Copy link
Collaborator

@bradfol bradfol left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀 and thanks for the test coverage

Comment on lines +184 to +186
// Allowing for #else creates a link between the registration inside the #if and those in the #else
// This greatly increases the complexity of handling #if so raise an error when #else is used
if node.poundKeyword.text.contains("#else") {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah I think this is reasonable. Folks that really need it can still work around by making multiple #if blocks

@@ -32,6 +36,10 @@ public struct Registration: Equatable, Codable {
self.getterConfig = getterConfig
}

private enum CodingKeys: CodingKey {
case service, name, accessLevel, arguments, isForwarded, getterConfig
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: can you add a small comment that the list is there just to exclude the ifConfigCondition property?

Comment on lines +321 to +325
XCTAssertEqual(config.registrations[1].service, "B")
XCTAssertEqual(config.registrations[1].ifConfigCondition?.description, "SOME_FLAG && !ANOTHER_FLAG")

XCTAssertEqual(config.registrations[2].service, "C")
XCTAssertEqual(config.registrations[2].ifConfigCondition?.description, "SOME_FLAG && !ANOTHER_FLAG")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice 👍

@skorulis-ap skorulis-ap merged commit 5c1fed4 into main Nov 17, 2023
1 check passed
@skorulis-ap skorulis-ap deleted the skorulis/ifdef branch November 17, 2023 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants