mirror of
https://github.com/datarhei/core.git
synced 2025-10-16 13:00:37 +08:00
Refactor cluster node code
This commit is contained in:
@@ -1,28 +1,54 @@
|
||||
package slices
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// EqualComparableElements returns whether two slices have the same elements.
|
||||
func EqualComparableElements[T comparable](a, b []T) bool {
|
||||
func EqualComparableElements[T comparable](a, b []T) error {
|
||||
extraA, extraB := DiffComparable(a, b)
|
||||
|
||||
if len(extraA) == 0 && len(extraB) == 0 {
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
return false
|
||||
diff := []string{}
|
||||
|
||||
for _, e := range extraA {
|
||||
diff = append(diff, fmt.Sprintf("+ %v", e))
|
||||
}
|
||||
|
||||
for _, e := range extraB {
|
||||
diff = append(diff, fmt.Sprintf("- %v", e))
|
||||
}
|
||||
|
||||
return errors.New(strings.Join(diff, ","))
|
||||
}
|
||||
|
||||
// Equaler defines a type that implements the Equal function.
|
||||
type Equaler[T any] interface {
|
||||
Equal(T) bool
|
||||
Equal(T) error
|
||||
}
|
||||
|
||||
// EqualEqualerElements returns whether two slices of Equaler have the same elements.
|
||||
func EqualEqualerElements[T any, X Equaler[T]](a []T, b []X) bool {
|
||||
func EqualEqualerElements[T any, X Equaler[T]](a []T, b []X) error {
|
||||
extraA, extraB := DiffEqualer(a, b)
|
||||
|
||||
if len(extraA) == 0 && len(extraB) == 0 {
|
||||
return true
|
||||
return nil
|
||||
}
|
||||
|
||||
return false
|
||||
diff := []string{}
|
||||
|
||||
for _, e := range extraA {
|
||||
diff = append(diff, fmt.Sprintf("- %v", e))
|
||||
}
|
||||
|
||||
for _, e := range extraB {
|
||||
diff = append(diff, fmt.Sprintf("+ %v", e))
|
||||
}
|
||||
|
||||
return errors.New(strings.Join(diff, ","))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user