mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-09-08 20:46:25 +08:00
refactor: simplify code with Go 1.25 APIs
This commit is contained in:
+2
-6
@@ -477,8 +477,6 @@ func (s Stream) walkLimited(fn WalkFunc, option *rxOptions) Stream {
|
|||||||
pool := make(chan lang.PlaceholderType, option.workers)
|
pool := make(chan lang.PlaceholderType, option.workers)
|
||||||
|
|
||||||
for item := range s.source {
|
for item := range s.source {
|
||||||
// important, used in another goroutine
|
|
||||||
val := item
|
|
||||||
pool <- lang.Placeholder
|
pool <- lang.Placeholder
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
@@ -489,7 +487,7 @@ func (s Stream) walkLimited(fn WalkFunc, option *rxOptions) Stream {
|
|||||||
<-pool
|
<-pool
|
||||||
}()
|
}()
|
||||||
|
|
||||||
fn(val, pipe)
|
fn(item, pipe)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -507,13 +505,11 @@ func (s Stream) walkUnlimited(fn WalkFunc, option *rxOptions) Stream {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
|
||||||
for item := range s.source {
|
for item := range s.source {
|
||||||
// important, used in another goroutine
|
|
||||||
val := item
|
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
// better to safely run caller defined method
|
// better to safely run caller defined method
|
||||||
threading.GoSafe(func() {
|
threading.GoSafe(func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
fn(val, pipe)
|
fn(item, pipe)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ func (u *Unmarshaler) fillUnmarshalerStruct(fieldType reflect.Type,
|
|||||||
target := reflect.New(baseType)
|
target := reflect.New(baseType)
|
||||||
switch u.key {
|
switch u.key {
|
||||||
case jsonTagKey:
|
case jsonTagKey:
|
||||||
unmarshaler, ok := target.Interface().(json.Unmarshaler)
|
unmarshaler, ok := reflect.TypeAssert[json.Unmarshaler](target)
|
||||||
if !ok {
|
if !ok {
|
||||||
return errUnsupportedType
|
return errUnsupportedType
|
||||||
}
|
}
|
||||||
@@ -424,7 +424,7 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any,
|
|||||||
func (u *Unmarshaler) implementsUnmarshaler(t reflect.Type) bool {
|
func (u *Unmarshaler) implementsUnmarshaler(t reflect.Type) bool {
|
||||||
switch u.key {
|
switch u.key {
|
||||||
case jsonTagKey:
|
case jsonTagKey:
|
||||||
return t.Implements(reflect.TypeOf((*json.Unmarshaler)(nil)).Elem())
|
return t.Implements(reflect.TypeFor[json.Unmarshaler]())
|
||||||
default:
|
default:
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@@ -740,12 +740,12 @@ func (u *Unmarshaler) processFieldTextUnmarshaler(fieldType reflect.Type, value
|
|||||||
if value.Elem().Kind() == reflect.Ptr {
|
if value.Elem().Kind() == reflect.Ptr {
|
||||||
target := reflect.New(Deref(fieldType))
|
target := reflect.New(Deref(fieldType))
|
||||||
SetValue(fieldType.Elem(), value, target)
|
SetValue(fieldType.Elem(), value, target)
|
||||||
tval, ok = target.Interface().(encoding.TextUnmarshaler)
|
tval, ok = reflect.TypeAssert[encoding.TextUnmarshaler](target)
|
||||||
} else {
|
} else {
|
||||||
tval, ok = value.Interface().(encoding.TextUnmarshaler)
|
tval, ok = reflect.TypeAssert[encoding.TextUnmarshaler](value)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tval, ok = value.Addr().Interface().(encoding.TextUnmarshaler)
|
tval, ok = reflect.TypeAssert[encoding.TextUnmarshaler](value.Addr())
|
||||||
}
|
}
|
||||||
if ok {
|
if ok {
|
||||||
switch mv := mapValue.(type) {
|
switch mv := mapValue.(type) {
|
||||||
|
|||||||
@@ -185,8 +185,7 @@ func currentCgroupV1() (cgroup, error) {
|
|||||||
|
|
||||||
// https://man7.org/linux/man-pages/man7/cgroups.7.html
|
// https://man7.org/linux/man-pages/man7/cgroups.7.html
|
||||||
// comma-separated list of controllers for cgroup version 1
|
// comma-separated list of controllers for cgroup version 1
|
||||||
fields := strings.Split(subsys, ",")
|
for val := range strings.SplitSeq(subsys, ",") {
|
||||||
for _, val := range fields {
|
|
||||||
cgroups[val] = path.Join(cgroupDir, val)
|
cgroups[val] = path.Join(cgroupDir, val)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -260,8 +259,7 @@ func parseUints(val string) ([]uint64, error) {
|
|||||||
|
|
||||||
var sets []uint64
|
var sets []uint64
|
||||||
ints := make(map[uint64]lang.PlaceholderType)
|
ints := make(map[uint64]lang.PlaceholderType)
|
||||||
cols := strings.Split(val, ",")
|
for r := range strings.SplitSeq(val, ",") {
|
||||||
for _, r := range cols {
|
|
||||||
if strings.Contains(r, "-") {
|
if strings.Contains(r, "-") {
|
||||||
fields := strings.SplitN(r, "-", 2)
|
fields := strings.SplitN(r, "-", 2)
|
||||||
minimum, err := parseUint(fields[0])
|
minimum, err := parseUint(fields[0])
|
||||||
|
|||||||
@@ -70,9 +70,6 @@ func (s *Server) Stop() {
|
|||||||
|
|
||||||
group := threading.NewRoutineGroup()
|
group := threading.NewRoutineGroup()
|
||||||
for _, conn := range s.conns {
|
for _, conn := range s.conns {
|
||||||
// new variable to avoid closure problems, can be removed after go 1.22
|
|
||||||
// see https://golang.org/doc/faq#closures_and_goroutines
|
|
||||||
conn := conn
|
|
||||||
group.Run(func() {
|
group.Run(func() {
|
||||||
// ignore the error when closing the connection
|
// ignore the error when closing the connection
|
||||||
_ = conn.Conn().Close()
|
_ = conn.Conn().Close()
|
||||||
|
|||||||
@@ -94,9 +94,7 @@ func ParseForm(r *http.Request, v any) error {
|
|||||||
// ParseHeader parses the request header and returns a map.
|
// ParseHeader parses the request header and returns a map.
|
||||||
func ParseHeader(headerValue string) map[string]string {
|
func ParseHeader(headerValue string) map[string]string {
|
||||||
ret := make(map[string]string)
|
ret := make(map[string]string)
|
||||||
fields := strings.Split(headerValue, separator)
|
for field := range strings.SplitSeq(headerValue, separator) {
|
||||||
|
|
||||||
for _, field := range fields {
|
|
||||||
field = strings.TrimSpace(field)
|
field = strings.TrimSpace(field)
|
||||||
if len(field) == 0 {
|
if len(field) == 0 {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ func getMiddleware(api *spec.ApiSpec) []string {
|
|||||||
for _, g := range api.Service.Groups {
|
for _, g := range api.Service.Groups {
|
||||||
middleware := g.GetAnnotation("middleware")
|
middleware := g.GetAnnotation("middleware")
|
||||||
if len(middleware) > 0 {
|
if len(middleware) > 0 {
|
||||||
for _, item := range strings.Split(middleware, ",") {
|
for item := range strings.SplitSeq(middleware, ",") {
|
||||||
result.Add(strings.TrimSpace(item))
|
result.Add(strings.TrimSpace(item))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -91,7 +91,7 @@ func responseStatusCode(atDoc apiSpec.AtDoc) int {
|
|||||||
|
|
||||||
func responseDescriptions(atDoc apiSpec.AtDoc) map[int]string {
|
func responseDescriptions(atDoc apiSpec.AtDoc) map[int]string {
|
||||||
descriptions := make(map[int]string)
|
descriptions := make(map[int]string)
|
||||||
for _, item := range strings.Split(getStringFromKVOrDefault(atDoc.Properties, propertyKeyResponses, ""), "<br>") {
|
for item := range strings.SplitSeq(getStringFromKVOrDefault(atDoc.Properties, propertyKeyResponses, ""), "<br>") {
|
||||||
codeText, description, ok := strings.Cut(item, "-")
|
codeText, description, ok := strings.Cut(item, "-")
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
|
|||||||
Vendored
+1
-2
@@ -135,9 +135,8 @@ func readEnv(goctlHome string) *sortedmap.SortedMap {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
dataStr := string(data)
|
dataStr := string(data)
|
||||||
lines := strings.Split(dataStr, "\n")
|
|
||||||
sm := sortedmap.New()
|
sm := sortedmap.New()
|
||||||
for _, line := range lines {
|
for line := range strings.SplitSeq(dataStr, "\n") {
|
||||||
_, _, err = sm.SetExpression(line)
|
_, _, err = sm.SetExpression(line)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -178,7 +178,7 @@ func (p *printer) print(x reflect.Value) {
|
|||||||
p.printf("}")
|
p.printf("}")
|
||||||
|
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
if s, ok := x.Interface().([]byte); ok {
|
if s, ok := reflect.TypeAssert[[]byte](x); ok {
|
||||||
p.printf("%#q", s)
|
p.printf("%#q", s)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -196,7 +196,7 @@ func (p *printer) print(x reflect.Value) {
|
|||||||
p.printf("}")
|
p.printf("}")
|
||||||
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if val, ok := x.Interface().(apitoken.Position); ok {
|
if val, ok := reflect.TypeAssert[apitoken.Position](x); ok {
|
||||||
p.printf("%s", val.String())
|
p.printf("%s", val.String())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user