Skip to content

refactor CheckoutState #3666

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

Merged
merged 2 commits into from
Aug 16, 2021
Merged

Conversation

tomerd
Copy link
Contributor

@tomerd tomerd commented Aug 14, 2021

motivation: when working on configuration, CheckoutState data model stood out as one that can be improved to be safer

changes:

  • transition CheckoutState from struct with optionals to enum representing the finite states
  • adjust call sites and tests

@tomerd tomerd force-pushed the refactor/checkout-state branch 2 times, most recently from a5856f2 to bcd15fb Compare August 14, 2021 05:40
return try manifest.dependencyConstraints(productFilter: productFilter)
}

func getDependencies(at revision: String, productFilter: ProductFilter) throws -> [PackageContainerConstraint] {
func getDependencies(at revisionString: String, productFilter: ProductFilter) throws -> [PackageContainerConstraint] {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

please double check logic still looks right

@@ -121,23 +122,25 @@ private struct LocalPackageContainer: PackageContainer {

func getDependencies(at version: Version, productFilter: ProductFilter) throws -> [PackageContainerConstraint] {
// Because of the implementation of `reversedVersions`, we should only get the exact same version.
precondition(dependency?.checkoutState?.version == version)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

please double check logic still looks right

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks correct to me.

@@ -95,7 +96,7 @@ private struct LocalPackageContainer: PackageContainer {
let currentToolsVersion: ToolsVersion

func versionsAscending() throws -> [Version] {
if let version = dependency?.state.checkout?.version {
if case .version(let version, revision: _) = dependency?.state.checkout {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

please double check logic still looks right

Copy link
Contributor

Choose a reason for hiding this comment

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

Looks correct to me.

revision = pin.state.revision
if branch != nil, !updateBranches {
if case .branch(branch, let pinRevision) = pinsStore.pins.first(where: { $0.packageRef == packageRef })?.state {
revision = pinRevision
Copy link
Contributor Author

Choose a reason for hiding this comment

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

please double check logic still looks right

motivation: when working on configuration, CheckoutState data model stood out as one that can be improved

changes:
* transition CheckoutState from struct with optionals to enum representing the finite states
* adust call sites and tests
@tomerd tomerd force-pushed the refactor/checkout-state branch from bcd15fb to 19e23a0 Compare August 14, 2021 05:43
@tomerd
Copy link
Contributor Author

tomerd commented Aug 14, 2021

@swift-ci please smoke test

@tomerd tomerd added the ready Author believes the PR is ready to be merged & any feedback has been addressed label Aug 14, 2021
@tomerd tomerd self-assigned this Aug 14, 2021
}
case revision(Revision)
case version(Version, revision: Revision)
case branch(String, revision: Revision)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we name the first parameter, too?

Copy link
Contributor Author

@tomerd tomerd Aug 16, 2021

Choose a reason for hiding this comment

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

I started with naming them but it was a bit awkward in the call site e.g. let checkoutState: CheckoutState = .branch(branch: "main", revision: ...) vs let checkoutState: CheckoutState = .branch("main", revision: ...)

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, the first parameter is often unnamed when the enum case makes it obvious, and I agree that that applies here. So I think it should be unnamed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ended up adding name: to the branch variant

}
case revision(Revision)
case version(Version, revision: Revision)
case branch(String, revision: Revision)
Copy link
Contributor

Choose a reason for hiding this comment

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

Right, the first parameter is often unnamed when the enum case makes it obvious, and I agree that that applies here. So I think it should be unnamed.

@@ -95,7 +96,7 @@ private struct LocalPackageContainer: PackageContainer {
let currentToolsVersion: ToolsVersion

func versionsAscending() throws -> [Version] {
if let version = dependency?.state.checkout?.version {
if case .version(let version, revision: _) = dependency?.state.checkout {
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks correct to me.

@@ -121,23 +122,25 @@ private struct LocalPackageContainer: PackageContainer {

func getDependencies(at version: Version, productFilter: ProductFilter) throws -> [PackageContainerConstraint] {
// Because of the implementation of `reversedVersions`, we should only get the exact same version.
precondition(dependency?.checkoutState?.version == version)
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks correct to me.

@tomerd
Copy link
Contributor Author

tomerd commented Aug 16, 2021

@swift-ci please smoke test

@tomerd tomerd merged commit c3bd142 into swiftlang:main Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready Author believes the PR is ready to be merged & any feedback has been addressed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants