← Library Predicate Deterministic

IfEmptyStringOp

Returns true if the value is nil or an empty string.

Inputs

FieldTypeDescription
Value*stringString to test

Outputs

FieldTypeDescription
MatchboolTrue if nil or empty string

Typical Use Cases

  • Guard before string operations
  • Fallback routing when upstream produced no value
  • Nil-safety check before concat or lookup

Complete Runnable Example

Detects an empty string result from an upstream extraction step before passing it downstream.

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() {
	val := ""
	library.RegisterConst("maybe_result", val)

	g, err := graph.NewBuilder("empty_str_demo").
		Vertex("src").Op("maybe_result").Output("Result", "val_wire").
		Vertex("check").Op("IfEmptyStringOp").
		Input("Value", "val_wire").
		Output("Match", "is_empty").
		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{"input": "(empty string)"}
	if raw, ok := eng.GetOutput("is_empty"); ok {
		out["is_empty"] = *raw.(*bool)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}