mirror of
https://github.com/infiniflow/ragflow.git
synced 2026-08-12 11:43:39 +08:00
refactor: use slices.Contains to simplify code (#16680)
### Summary There is a [new function](https://pkg.go.dev/slices@go1.21.0#Contains) added in the go1.21 standard library, which can make the code more concise and easy to read. Signed-off-by: weifanglab <weifanglab@outlook.com>
This commit is contained in:
@@ -37,6 +37,7 @@ package canvas
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"ragflow/internal/agent/workflowx"
|
||||
@@ -707,9 +708,9 @@ func evalDictOp(m map[string]any, op string, _ any) (bool, error) {
|
||||
func evalListOp(lst []any, op string, value any) (bool, error) {
|
||||
switch op {
|
||||
case "contains":
|
||||
return listContains(lst, value), nil
|
||||
return slices.Contains(lst, value), nil
|
||||
case "not contains":
|
||||
return !listContains(lst, value), nil
|
||||
return !slices.Contains(lst, value), nil
|
||||
case "is":
|
||||
return listEqual(lst, value), nil
|
||||
case "is not":
|
||||
@@ -722,15 +723,6 @@ func evalListOp(lst []any, op string, value any) (bool, error) {
|
||||
return false, fmt.Errorf("invalid operator: %s (list variable)", op)
|
||||
}
|
||||
|
||||
func listContains(lst []any, value any) bool {
|
||||
for _, x := range lst {
|
||||
if x == value {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func listEqual(lst []any, value any) bool {
|
||||
other, ok := value.([]any)
|
||||
if !ok {
|
||||
|
||||
@@ -15,6 +15,7 @@ package component
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/cloudwego/eino/schema"
|
||||
@@ -178,14 +179,7 @@ func TestLLM_Invoke_InvokerError(t *testing.T) {
|
||||
|
||||
func TestLLM_Registered(t *testing.T) {
|
||||
names := RegisteredNames()
|
||||
found := false
|
||||
for _, n := range names {
|
||||
if n == "llm" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
if !slices.Contains(names, "llm") {
|
||||
t.Fatalf("LLM not registered; names=%v", names)
|
||||
}
|
||||
// And a factory round-trip.
|
||||
|
||||
@@ -35,6 +35,7 @@ package component
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -45,14 +46,7 @@ import (
|
||||
// assert their absence here.
|
||||
func TestParallel_Registered(t *testing.T) {
|
||||
names := RegisteredNames()
|
||||
hasParallel := false
|
||||
for _, n := range names {
|
||||
if n == "parallel" {
|
||||
hasParallel = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !hasParallel {
|
||||
if !slices.Contains(names, "parallel") {
|
||||
t.Errorf("Parallel not registered; RegisteredNames=%v", names)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user