← Library Predicate Deterministic

IfStringHasSuffixOp

Returns true if string A ends with string B.

Inputs

FieldTypeDescription
A*stringString to test
B*stringExpected suffix

Outputs

FieldTypeDescription
MatchboolTrue when A ends with B

Typical Use Cases

  • File extension checks
  • Domain suffix routing
  • Format detection from a trailing marker

Complete Runnable Example

Checks whether an uploaded filename ends with ".pdf" before routing it to the PDF parser.

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() {
	filename, suffix := "report_2026.pdf", ".pdf"
	library.RegisterConst("filename_val", filename)
	library.RegisterConst("suffix_val", suffix)

	g, err := graph.NewBuilder("hassuffix_demo").
		Vertex("src_a").Op("filename_val").Output("Result", "wire_a").
		Vertex("src_b").Op("suffix_val").Output("Result", "wire_b").
		Vertex("check").Op("IfStringHasSuffixOp").
		Input("A", "wire_a").Input("B", "wire_b").
		Output("Match", "is_pdf").
		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{"filename": filename, "suffix": suffix}
	if raw, ok := eng.GetOutput("is_pdf"); ok {
		out["is_pdf"] = *raw.(*bool)
	}
	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}