← Library Select Deterministic

SelectIntOp

Returns IfTrue if Cond is true, otherwise IfFalse.

Inputs

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

Outputs

FieldTypeDescription
ResultintSelected value

Typical Use Cases

  • Conditional integer selection
  • Flag assignment based on a condition
  • Choosing between two index values

Complete Runnable Example

Selects a retry limit based on whether the request is marked as high-priority.

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 := false
	ifTrue := 10
	ifFalse := 5
	library.RegisterConst("cond_val", cond)
	library.RegisterConst("high_retries", ifTrue)
	library.RegisterConst("low_retries", ifFalse)

	g, err := graph.NewBuilder("selectint_demo").
		Vertex("src_c").Op("cond_val").Output("Result", "wire_c").
		Vertex("src_t").Op("high_retries").Output("Result", "wire_t").
		Vertex("src_f").Op("low_retries").Output("Result", "wire_f").
		Vertex("sel").Op("SelectIntOp").
		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_high_priority": cond}
	if raw, ok := eng.GetOutput("selected"); ok {
		out["retry_limit"] = *raw.(*int)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}