Looks up a key in a static string map, returning the associated value or empty string if the key is not found.
| Field | Type | Description |
|---|---|---|
Key | *string | Key to look up |
| Field | Type | Description |
|---|---|---|
Result | string | Associated value, or empty string if not found |
| Key | Type | Default | Description |
|---|---|---|---|
map | string | '{}' | JSON key→value lookup table |
Returns empty string on miss. Use IfEmptyStringOp downstream to detect and handle missing keys.
Translates a locale code like "fr" to its full language name for display in the UI.
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() {
code := "fr"
library.RegisterConst("code_val", code)
g, err := graph.NewBuilder("lookup_demo").
Vertex("src").Op("code_val").Output("Result", "wire_key").
Vertex("lk").Op("StringLookupOp").
Params(map[string]string{
"map": `{"en":"English","fr":"French","de":"German"}`,
}).
Input("Key", "wire_key").
Output("Result", "language").
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{"code": code}
if raw, ok := eng.GetOutput("language"); ok {
out["language"] = *raw.(*string)
}
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
enc.Encode(out)
}