← Library Select Deterministic

SelectStringOp

Returns IfTrue if Cond is true, otherwise IfFalse.

Inputs

FieldTypeDescription
Cond*boolCondition to test
IfTrue*stringValue when Cond is true
IfFalse*stringValue when Cond is false

Outputs

FieldTypeDescription
ResultstringSelected value

Typical Use Cases

  • Ternary string selection
  • Conditional label assignment
  • Choosing between two string outputs based on a bool condition

Complete Runnable Example

Routes a job to the priority queue when the user is marked as premium, otherwise the standard queue.

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() {
	cond := true
	ifTrue := "Priority queue"
	ifFalse := "Standard queue"
	library.RegisterConst("cond_val", cond)
	library.RegisterConst("true_val", ifTrue)
	library.RegisterConst("false_val", ifFalse)

	g, err := graph.NewBuilder("selectstr_demo").
		Vertex("src_c").Op("cond_val").Output("Result", "wire_c").
		Vertex("src_t").Op("true_val").Output("Result", "wire_t").
		Vertex("src_f").Op("false_val").Output("Result", "wire_f").
		Vertex("sel").Op("SelectStringOp").
		Input("Cond", "wire_c").Input("IfTrue", "wire_t").Input("IfFalse", "wire_f").
		Output("Result", "selected").
		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{"is_premium": cond}
	if raw, ok := eng.GetOutput("selected"); ok {
		out["queue"] = *raw.(*string)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}