-
Notifications
You must be signed in to change notification settings - Fork 28
feat: add ContainsBy and OrO #18
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: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,8 +51,6 @@ import ( | |
| "reflect" | ||
|
|
||
| "github.com/bytedance/gg/goption" | ||
| "github.com/bytedance/gg/gptr" | ||
| "github.com/bytedance/gg/gvalue" | ||
| ) | ||
|
|
||
| // R represents a generic result: Every result is a value, | ||
|
|
@@ -87,7 +85,7 @@ func OK[T any](v T) R[T] { | |
| // | ||
| // ⚠️ WARNING: Passing a nil error will cause ❌PANIC❌! | ||
| func Err[T any](e error) R[T] { | ||
| if gvalue.IsNil(e) { | ||
| if reflect.ValueOf(e).IsNil() { | ||
| panic(fmt.Errorf("expected a non-nil error, but found nil error with type %T", e)) | ||
| } | ||
| return R[T]{err: e} | ||
|
|
@@ -112,11 +110,11 @@ func (r R[T]) ValueOr(v T) T { | |
| // | ||
| // 💡 HINT: Refer to function [github.com/bytedance/gg/gvalue.Zero] | ||
| // for explanation of zero value. | ||
| func (r R[T]) ValueOrZero() T { | ||
| func (r R[T]) ValueOrZero() (v T) { | ||
| if r.err == nil { | ||
| return r.val | ||
| } | ||
| return gvalue.Zero[T]() | ||
| return v | ||
| } | ||
|
|
||
| // Err returns the internal error of R. | ||
|
|
@@ -154,14 +152,15 @@ func (r R[T]) IfErr(f func(error)) { | |
| } | ||
|
|
||
| func checkErr(e error) { | ||
| if e != nil && gvalue.IsNil(e) { | ||
| if e != nil && reflect.ValueOf(e).IsNil() { | ||
| panic(fmt.Errorf("error with type %T is nil", e)) | ||
| } | ||
| } | ||
|
|
||
| // typ returns the string representation of type of optional value. | ||
| func (r R[T]) typ() string { | ||
| typ := reflect.TypeOf(gvalue.Zero[T]()) | ||
| var v T | ||
| typ := reflect.TypeOf(v) | ||
| if typ == nil { | ||
| return "any" | ||
| } | ||
|
|
@@ -187,7 +186,8 @@ type jsonR[T any] struct { | |
| func (r R[T]) MarshalJSON() ([]byte, error) { | ||
| jr := jsonR[T]{} | ||
| if r.err != nil { | ||
| jr.Err = gptr.Of(r.err.Error()) | ||
| msg := r.err.Error() | ||
| jr.Err = &msg | ||
| } else { | ||
| jr.Val = &r.val | ||
| } | ||
|
|
@@ -216,14 +216,10 @@ func (r *R[T]) UnmarshalJSON(data []byte) error { | |
| } | ||
|
|
||
| if jr.Err == nil && jr.Val == nil { | ||
| r.val = gvalue.Zero[T]() | ||
| r.err = nil | ||
| } else if jr.Err != nil { | ||
| r.val = gvalue.Zero[T]() | ||
| r.err = errors.New(*jr.Err) | ||
| } else { | ||
| r.val = *jr.Val | ||
| r.err = nil | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里不能直接删除吧?把一个 这么一看发现原来也有问题 😂,应该在 L205 之前就把 r 清空了。 |
||
| } | ||
|
|
||
| return nil | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OrO 这个函数名称能直观的表达出你想要的函数意思吗?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OrO 最后一个 O,是啥单词的缩写?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Option。类似 gsync中的LoadO、gconv中的ToR |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ package gvalue | |
| import ( | ||
| "reflect" | ||
|
|
||
| "github.com/bytedance/gg/goption" | ||
| "github.com/bytedance/gg/internal/constraints" | ||
| ) | ||
|
|
||
|
|
@@ -67,6 +68,26 @@ func Or[T comparable](vals ...T) (v T) { | |
| return | ||
| } | ||
|
|
||
| // OrO returns the first non-zero value of inputs. | ||
| // If all values are zero, return [goption.Nil]. | ||
| // | ||
| // 🚀 EXAMPLE: | ||
| // | ||
| // OrO(false, true) ⏩ goption.OK(true) | ||
| // OrO(0, 1, 2) ⏩ goption.OK(1) | ||
| // OrO("", "1", "2") ⏩ goption.OK("1") | ||
| // OrO(0, 0, 0) ⏩ goption.Nil[int]() | ||
| // OrO("", "", "") ⏩ goption.Nil[string]() | ||
| func OrO[T comparable](vals ...T) goption.O[T] { | ||
| var zero T | ||
| for _, val := range vals { | ||
| if val != zero { | ||
| return goption.OK(val) | ||
| } | ||
| } | ||
| return goption.Nil[T]() | ||
| } | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 感觉这个函数名字不太直观,名字也不太清晰(还以为是
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 主要是想迁移原先的choose.NotZero
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 按原来规范可能叫 OfNonZero 比较好?不过还是不如 choose 直观。 |
||
| // Max returns the maximum value of inputs. | ||
| // | ||
| // 🚀 EXAMPLE: | ||
|
|
||
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.
想说这样性能会差不少,转头一看 gvalue.IsNil 也已经是反射了(
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.
有个问题,原来的gvalue.IsNil()识别不了nil slice
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.
有啥好办法吗
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.
场景是啥?我印象
var t []T和var t = []T{}在使用上应该完全没区别才对?(或者说不会导致什么问题)Uh oh!
There was an error while loading. Please reload this page.
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.
从使用上,我也觉得不应该使用 IsNil 或者直接 == nil 来区分空slice。
但是从语义上,IsNil应该能区分出slice是否是nil,之前完全区分不出来,误导性比较强。
之前的实现:
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.
空 slice 就不是 nil 吧?
可以补一个测试。
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.
Yes,现在的表现是你发的这样的。
