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

Make .ext on empty paths return "" rather than crashing with a NPE #87

Merged
merged 2 commits into from
Dec 8, 2021

Conversation

lihaoyi
Copy link
Member

@lihaoyi lihaoyi commented Dec 7, 2021

Fixes com-lihaoyi/Ammonite#1017

This is more consistent with calling .ext on non-empty paths which have no extension, since those currently return ""

We also introduce a new a lastOption method, as a way to get the last segment in a list without throwing. Not sure if that's the right thing to do, or whether we should make .last return "" (which is otherwise not a valid return value, since path segments cannot be empty).

Covered with simple unit tests

@lihaoyi lihaoyi requested a review from lefou December 7, 2021 04:55
@lihaoyi
Copy link
Member Author

lihaoyi commented Dec 7, 2021

CI failure seems like a flake

Copy link
Member

@lefou lefou left a comment

Choose a reason for hiding this comment

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

Nice fix. I think I spotted one issue.

I also like the new lastOption idea, which is effectively a lastOpt. Definitely better than returning an empty string from .last.

@@ -193,7 +198,9 @@ trait BasePathImpl extends BasePath{
else last.slice(0, li)
}

def last: String
def last: String = lastOpt.getOrElse("empty path has no last segment")
Copy link
Member

Choose a reason for hiding this comment

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

Did you mean to throw an exception here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Hah good catch. I'll add a test to cover this error behavior as well

@lihaoyi
Copy link
Member Author

lihaoyi commented Dec 8, 2021

@lefou this should be ready for another look

Copy link
Member

@lefou lefou left a comment

Choose a reason for hiding this comment

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

LGTM!

@lefou
Copy link
Member

lefou commented Dec 8, 2021

This should be released as next minor release, because of the newly introduced method in the API.

@lihaoyi lihaoyi merged commit a603803 into master Dec 8, 2021
@lefou lefou deleted the fix-empty-ext branch July 28, 2022 09:06
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.

Nullpointer Exception: Path("/").ext
2 participants