← Library String Deterministic

StringLookupOp

Looks up a key in a static string map, returning the associated value or empty string if the key is not found.

Inputs

Field Type Description
Key*stringKey to look up

Outputs

Field Type Description
ResultstringAssociated value, or empty string if not found

Params

Key Type Default Description
mapstring'{}'JSON key→value lookup table

Typical Use Cases

  • Code-to-label translation (status code to message)
  • Static routing tables in workflow config
  • Config-driven string substitution

Returns empty string on miss. Use IfEmptyStringOp downstream to detect and handle missing keys.

Complete Runnable Example

Translates a locale code like "fr" to its full language name for display in the UI.

main.go
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)
}