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

Detect and validate duplicate registrations within an Assembly #199

Merged
merged 1 commit into from
Oct 29, 2024

Conversation

bradfol
Copy link
Collaborator

@bradfol bradfol commented Sep 20, 2024

We are able to detect duplicates from within a single module at parse/compile time, so it will be good to fail early and provide an error to the developer.

@bradfol bradfol requested a review from skorulis-ap September 20, 2024 21:52
extension ConfigurationSet {

private struct Key: Hashable {
let service: String
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think both arguments and ifConfigCondition are needed in this key for uniqueness. ifConfigCondition is a little more complex as per the following code.

# if DEBUG
container.register(Service.self, factory: DebugService.init)
#endif

# if RELEASE
container.register(Service.self, factory: ReleaseService.init)
#endif

container.register(Service.self, factory: DuplicateService.init)

The DEBUG and RELEASE configurations don't clash (but there's no way to know that from code that those are exclusive) but both clash with the registration that isn't wrapped.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah thanks yes i'll update

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated to include arguments. Still need to work on the ifConfigCondition check as well

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also i realized i need to separate the assemblies by TargetResolver because we have shadowed registrations for Entities

Comment on lines 171 to 183
registrationCount += 1
registrationSet.insert(Key(service: registration.service, name: registration.name))

guard registrationCount == registrationSet.count else {
throw ConfigurationSetParsingError.detectedDuplicateRegistration(
service: registration.service,
name: registration.name
)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: registrationCount could be removed by checking if the registrationSet contains the key already

Suggested change
registrationCount += 1
registrationSet.insert(Key(service: registration.service, name: registration.name))
guard registrationCount == registrationSet.count else {
throw ConfigurationSetParsingError.detectedDuplicateRegistration(
service: registration.service,
name: registration.name
)
}
let key = Key(service: registration.service, name: registration.name)
guard !registrationSet.contains(key) else {
throw ConfigurationSetParsingError.detectedDuplicateRegistration(
service: registration.service,
name: registration.name
)
}
registrationSet.insert(key)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks, updated

Copy link
Collaborator

@skorulis-ap skorulis-ap left a comment

Choose a reason for hiding this comment

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

This looks safe but I'm not sure if there are any edge cases in cash-ios that might prevent building.

@bradfol bradfol force-pushed the bradfol/parse-dup-validation branch from cf34d18 to b9205eb Compare September 24, 2024 21:29
@bradfol bradfol force-pushed the bradfol/parse-dup-validation branch from b9205eb to c9f5c3d Compare October 28, 2024 17:17
We are able to detect duplicates from within a single module at parse/compile time, so it will be good to fail early and provide an error to the developer.
@bradfol bradfol force-pushed the bradfol/parse-dup-validation branch from c9f5c3d to e3c6df4 Compare October 28, 2024 18:17
@bradfol
Copy link
Collaborator Author

bradfol commented Oct 29, 2024

Ok i updated to add support for assemblies with multiple different TargetResolvers within the same configuration set. Ran against cash-ios and it has detected some duplicates.

@bradfol bradfol merged commit d069370 into main Oct 29, 2024
1 check passed
@bradfol bradfol deleted the bradfol/parse-dup-validation branch October 29, 2024 00:08
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.

2 participants