Skip to content

Conversation

@Akalanka47000
Copy link

Usage and purpose for the following functions are documented within the changes

Errors:

  • Ok
  • OkOr

Type Manipulation:

  • Cast
  • CastJSON
  • FromBytes

Copy link
Owner

@samber samber left a 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/

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
}
Copy link
Owner

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 {
Copy link
Owner

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.
Copy link
Owner

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.

Copy link
Owner

Choose a reason for hiding this comment

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

OrEmpty/ OrElse ?

Copy link
Owner

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

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