refactor(core): replace TakeOne usage with cmp.Or#5461
refactor(core): replace TakeOne usage with cmp.Or#5461kevwan merged 1 commit intozeromicro:masterfrom
Conversation
kevwan
left a comment
There was a problem hiding this comment.
Review
Clean, idiomatic Go modernisation. cmp.Or (available since Go 1.21, project requires Go 1.23) is the canonical stdlib replacement for the "first non-zero" pattern — removing a dependency on the internal stringx package from core/mapping is a clear improvement.
core/mapping/utils.go — Both stringx.TakeOne(cache.key, field.Name) → cmp.Or(cache.key, field.Name) replacements are correct. The semantics are identical for strings: returns the first non-empty value, falling back to the second.
core/stringx/strings.go — Adding // Deprecated: use cmp.Or instead. to TakeOne is the right signal. One small suggestion: follow Go's conventional deprecation comment format so tooling picks it up:
// Deprecated: Use cmp.Or from the standard library instead.
func TakeOne(valid, or string) string {(The conventional form starts with Deprecated: on a line of its own or as a sentence within the godoc comment.)
Overall: LGTM. The removal of the internal import simplifies the dependency graph in core/mapping.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
The custom
TakeOnefunction is redundant since Go 1.21 introducedcmp.Or,which provides identical functionality for selecting the first non-zero value.
Changes:
TakeOneas deprecated with a pointer tocmp.Or.TakeOnewithcmp.Or.