-
Notifications
You must be signed in to change notification settings - Fork 24
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?
Conversation
XQ-Gang
commented
Jul 6, 2025
- add ContainsBy
- add OrO
- goption/gresult不依赖别的包,防止循环依赖
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #18 +/- ##
==========================================
+ Coverage 95.79% 96.13% +0.34%
==========================================
Files 34 34
Lines 4443 4451 +8
==========================================
+ Hits 4256 4279 +23
+ Misses 144 131 -13
+ Partials 43 41 -2
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
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.
OrO 这个函数名称能直观的表达出你想要的函数意思吗?
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.
OrO 最后一个 O,是啥单词的缩写?
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.
Option。类似 gsync中的LoadO、gconv中的ToR
| 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)) |
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{} 在使用上应该完全没区别才对?(或者说不会导致什么问题)
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,之前完全区分不出来,误导性比较强。
之前的实现:
var t []T
gvalue.IsNil(t) // falseThere 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 吧?
var s []int
assert.True(t, IsNil(s))
var s2 = []int{}
assert.False(t, IsNil(s2))可以补一个测试。
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.
| r.err = errors.New(*jr.Err) | ||
| } else { | ||
| r.val = *jr.Val | ||
| r.err = 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.
这里不能直接删除吧?把一个 var r Result 传给 json.Unarmal(data, &v) 后,是需要清除原来的值的?
这么一看发现原来也有问题 😂,应该在 L205 之前就把 r 清空了。
| } | ||
| return goption.Nil[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.
感觉这个函数名字不太直观,名字也不太清晰(还以为是 Or0)。能给一些更 real world 的用例么?
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.
主要是想迁移原先的choose.NotZero
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.
按原来规范可能叫 OfNonZero 比较好?不过还是不如 choose 直观。
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.
Round1
