Added 5 more utility functions for errors and type manipulation#624
Open
Akalanka47000 wants to merge 2 commits intosamber:masterfrom
Open
Added 5 more utility functions for errors and type manipulation#624Akalanka47000 wants to merge 2 commits intosamber:masterfrom
Akalanka47000 wants to merge 2 commits intosamber:masterfrom
Conversation
samber
reviewed
Nov 5, 2025
Owner
samber
left a comment
There was a problem hiding this comment.
Please add a documentation in docs/docs/data/
Comment on lines
+205
to
+216
| // 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 | ||
| } |
Owner
There was a problem hiding this comment.
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 { |
Owner
There was a problem hiding this comment.
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. |
Owner
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Usage and purpose for the following functions are documented within the changes
Errors:
Type Manipulation: