all: use slices.Contains #29459 (#1177)

This commit is contained in:
JukLee0ira 2025-07-01 17:49:42 +08:00 committed by GitHub
parent bf093348b7
commit 1dc8158ef0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 12 deletions

View file

@ -25,6 +25,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"reflect" "reflect"
"slices"
"strings" "strings"
"sync" "sync"
@ -288,16 +289,6 @@ func (n *Node) openEndpoints() error {
return err return err
} }
// containsLifecycle checks if 'lfs' contains 'l'.
func containsLifecycle(lfs []Lifecycle, l Lifecycle) bool {
for _, obj := range lfs {
if obj == l {
return true
}
}
return false
}
// stopServices terminates running services, RPC and p2p networking. // stopServices terminates running services, RPC and p2p networking.
// It is the inverse of Start. // It is the inverse of Start.
func (n *Node) stopServices(running []Lifecycle) error { func (n *Node) stopServices(running []Lifecycle) error {
@ -575,7 +566,7 @@ func (n *Node) RegisterLifecycle(lifecycle Lifecycle) {
if n.state != initializingState { if n.state != initializingState {
panic("can't register lifecycle on running/stopped node") panic("can't register lifecycle on running/stopped node")
} }
if containsLifecycle(n.lifecycles, lifecycle) { if slices.Contains(n.lifecycles, lifecycle) {
panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle)) panic(fmt.Sprintf("attempt to register lifecycle %T more than once", lifecycle))
} }
n.lifecycles = append(n.lifecycles, lifecycle) n.lifecycles = append(n.lifecycles, lifecycle)

View file

@ -23,6 +23,7 @@ import (
"net" "net"
"net/http" "net/http"
"reflect" "reflect"
"slices"
"strings" "strings"
"testing" "testing"
@ -116,7 +117,7 @@ func TestLifecycleRegistry_Successful(t *testing.T) {
noop := NewNoop() noop := NewNoop()
stack.RegisterLifecycle(noop) stack.RegisterLifecycle(noop)
if !containsLifecycle(stack.lifecycles, noop) { if !slices.Contains(stack.lifecycles, Lifecycle(noop)) {
t.Fatalf("lifecycle was not properly registered on the node, %v", err) t.Fatalf("lifecycle was not properly registered on the node, %v", err)
} }
} }