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

Better handle non-existent migration line + suggestions with Levenshtein distance #558

Merged
merged 1 commit into from
Aug 24, 2024

Conversation

brandur
Copy link
Contributor

@brandur brandur commented Aug 23, 2024

This is related to #546. That was a documentation problem, but there are
some code problems that are related.

The first is that an unknown migration line is a panic in the CLI. This
is a byproduct of the fact that rivermigrate.New checks the migration
line name, but doesn't return an error, so it'd panic instead.

Here, I'm suggesting that we do a small breaking change by having
rivermigrate.New return a possible error. It's not great, but it's
something that'll easy for people to fix, and might help avoid some
future panics. An alternative possibility would be to add a new
NewWithError or something of that nature, but it'd make the API a
little uglier.

Along with that change, we also bring in a change to suggest migration
lines in case of an unknown one using Levenshtein distances. This is
aimed at preventing totally unactionable errors in the event of very
simple misspellings (e.g. "workflow" versus "workflows"). Cobra actually
already has a similar feature built-in for command misspellings.

I vendored in this repo [1] as a Levenshtein implementation and did a
little code clean up. I don't want to add another Go module dependency
for such a simple algorithm (it's just a dozen lines), and I believe
their MIT license should be compatible.

[1] https://github.com/agnivade/levenshtein/tree/master

@brandur brandur force-pushed the brandur-better-handle-non-existent-line branch 2 times, most recently from 034c94d to 052c603 Compare August 23, 2024 14:54
@brandur brandur requested a review from bgentry August 23, 2024 14:56
@brandur brandur force-pushed the brandur-better-handle-non-existent-line branch from 052c603 to b471ce5 Compare August 23, 2024 14:58
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Agniva De Sarker
Copy link
Contributor

Choose a reason for hiding this comment

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

Trying to confirm if this is the correct copyright holder for this bit, where did this name come from?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I vendored the code originally from this repo:

https://github.com/agnivade/levenshtein

I'm thinking I may write my own implementation from scratch at some point when I have a bit more time. I ended up rebuilding a lot of the code anyway to modernize and satisfy lint.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment pointing to the source repo.

@brandur brandur force-pushed the brandur-better-handle-non-existent-line branch from b471ce5 to 6e3c25e Compare August 24, 2024 21:53
…ein distance

This is related to #546. That was a documentation problem, but there are
some code problems that are related.

The first is that an unknown migration line is a panic in the CLI. This
is a byproduct of the fact that `rivermigrate.New` checks the migration
line name, but doesn't return an error, so it'd panic instead.

Here, I'm suggesting that we do a small breaking change by having
`rivermigrate.New` return a possible error. It's not great, but it's
something that'll easy for people to fix, and might help avoid some
future panics. An alternative possibility would be to add a new
`NewWithError` or something of that nature, but it'd make the API a
little uglier.

Along with that change, we also bring in a change to suggest migration
lines in case of an unknown one using Levenshtein distances. This is
aimed at preventing totally unactionable errors in the event of very
simple misspellings (e.g. "workflow" versus "workflows"). Cobra actually
already has a similar feature built-in for command misspellings.

I vendored in this repo [1] as a Levenshtein implementation and did a
little code clean up. I don't want to add another Go module dependency
for such a simple algorithm (it's just a dozen lines), and I believe
their MIT license should be compatible.

[1] https://github.com/agnivade/levenshtein/tree/master
@brandur brandur force-pushed the brandur-better-handle-non-existent-line branch from 6e3c25e to 18d3571 Compare August 24, 2024 21:56
@brandur brandur merged commit 65fd815 into master Aug 24, 2024
14 checks passed
@brandur brandur deleted the brandur-better-handle-non-existent-line branch August 24, 2024 22:38
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