-
-
Notifications
You must be signed in to change notification settings - Fork 925
Added 5 more utility functions for errors and type manipulation #624
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
base: master
Are you sure you want to change the base?
Conversation
samber
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a documentation in docs/docs/data/
| // Converts any type to a given type based on their json representations. It partially fills the target in case they are not directly compatible. Any errors are ignored. | ||
| func CastJSON[T any](val any) T { | ||
| bytes, _ := json.Marshal(val) | ||
| return FromBytes[T](bytes) | ||
| } | ||
|
|
||
| // Converts a byte array to a given type. Ignores any errors. | ||
| func FromBytes[T any](bytes []byte) T { | ||
| var v T | ||
| _ = json.Unmarshal(bytes, &v) | ||
| return v | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's not a good idea to bring such helper in lo. Also the naming is really bad.
| // Converts any type to a given type. If conversion fails, it returns the zero value of the given type. This is a type-safe version of type assertion. | ||
| // | ||
| // Cast enforces strict type assertion, for example trying to convert a number of 10 to float64 will return 0.0 instead of 10.0. | ||
| func Cast[T any](val any) T { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can already do a := val.(A) (skipping the "ok")
so what is the use case for such helper?
| return t, ok | ||
| } | ||
|
|
||
| // Ok returns the value and ignores the error. Use with caution and only when you don't care about the error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the idea of both Ok and OkOr, but the method name does not seems "self-explaining" to me. Can you suggest other names ?
Anyway, we need to rename Ok to OkOrEmpty to match the convention of this library.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OrEmpty/ OrElse ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yuweiweiouo suggested EmptyOnError in #495
Usage and purpose for the following functions are documented within the changes
Errors:
Type Manipulation: