Returns true if the string slice contains the given value.
| Field | Type | Description |
|---|---|---|
Input | *[]string | Slice to search |
Value | *string | Value to search for |
| Field | Type | Description |
|---|---|---|
Match | bool | True if value is found in the slice |
Checks whether the user's role list includes "admin" before granting access to a settings panel.
package main
import (
"context"
"encoding/json"
"log"
"os"
"time"
"github.com/akennis/sparsi-go/library"
_ "github.com/akennis/dagor/operator/builtin"
"github.com/panjf2000/ants/v2"
"github.com/akennis/dagor"
"github.com/akennis/dagor/graph"
)
func main() {
roles := []string{"editor", "reviewer", "admin"}
target := "admin"
library.RegisterConst("roles_val", roles)
library.RegisterConst("target_val", target)
g, err := graph.NewBuilder("slicecontains_demo").
Vertex("src").Op("roles_val").Output("Result", "wire_in").
Vertex("tgt").Op("target_val").Output("Result", "wire_val").
Vertex("chk").Op("SliceContainsOp").
Input("Input", "wire_in").Input("Value", "wire_val").
Output("Match", "has_role").
Build()
if err != nil {
log.Fatalf("build: %v", err)
}
pool, err := ants.NewPool(4)
if err != nil {
log.Fatalf("pool: %v", err)
}
defer pool.Release()
eng, err := dagor.NewEngine(g, pool)
if err != nil {
log.Fatalf("engine: %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
if err := eng.Run(ctx); err != nil {
log.Fatalf("run: %v", err)
}
out := map[string]any{"roles": roles, "target": target}
if raw, ok := eng.GetOutput("has_role"); ok {
out["has_role"] = *raw.(*bool)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(out)
}