-
Notifications
You must be signed in to change notification settings - Fork 83
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 MatchStringSafe
Method to Simplify Error Handling in MatchString
#84
base: master
Are you sure you want to change the base?
Conversation
An interesting idea. Is the “Safe” suffix a standard name for this kind of “throw away an error” method in Go? I think I’ve seen it before but can’t remember where. Any examples? |
"Safe" is not commonly used. "Must" is a common suffix for using a panic on error. But it's not the need here. IsMatchString could be a candidate, but it sounds strange to me. ValidateString sounds better maybe. While I understand the need, I'm bad at wording 😅 |
Anyway, I'm unsure such method should be provided. Apparently, the only source of error is the timeout. But having a process that detects the presence of something that may say "ok not found" is dangerous as it could lead to taking wrong decision, such as deleting a file. Or something like that. I mean if a user code such helper in their code, they are aware of what they do. But providing such helper in the lib could be dangerous Many people are not aware there is a timeout mechanism in this lib. |
MustMatchString that would call MatchString and panic on error, otherwise return a boolean, might be a better and more logic But having a random panic depending on the timeout is also strange. |
By default the timeout mechanism is off, so this could be converted to a "Must" style function that panics if a timeout happens. Lots of examples of Must-style floating around. |
Yes, that's why I suggested that But to find a solution for author needs https://pkg.go.dev/github.com/samber/lo#Must It's what was asked. So Must seems to be good prefix |
(I think we're on the same page I posted my message before I saw yours) Honestly this feels like a trivial wrapper that anybody could write in their code: func Must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
} Does it really need to be in the |
Yes, that's what I stated here |
Thanks for the feedback, everyone. The method is not intended to replace the existing Naming-wise, while Ultimately, adding this method would provide more flexibility to the library's users, giving them the choice to balance simplicity and safety as needed. The method name inspired by this lib: https://github.com/gookit/goutil |
Description
This PR introduces a new method
MatchStringSafe
to theRegexp
struct. The new method simplifies the use of regular expressions by returning only a boolean value, ignoring any errors that might occur. This is particularly useful when users are not concerned with specific error handling (e.g., timeout errors) and prefer a simple true/false response.While the existing
MatchString
method is robust and provides detailed error information, there are many cases where users may not need to handle errors explicitly. In these cases, the additional complexity of dealing with an error value can be cumbersome. TheMatchStringSafe
method simplifies this common use case by allowing users to write more concise and readable code when error handling is not required.