← Library I/ODeterministic

FileReadOp

Reads the full contents of a file at the given path.

Inputs

FieldTypeDescription
Path*stringFilesystem path to read

Outputs

FieldTypeDescription
ContentstringFull file contents

Typical Use Cases

  • Loading prompt templates from disk
  • Reading config files at runtime
  • Ingesting input documents for processing

Path is a wire, not a param. This allows the path to be computed at runtime by upstream ops such as StringConcatOp or StringLookupOp.

Complete Runnable Example

Writes a greeting to a temp file, then reads it back through the workflow.

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() {
	tmpFile := "/tmp/Sparsi_greeting.txt"
	os.WriteFile(tmpFile, []byte("Hello from Sparsi!"), 0644)

	library.RegisterConst("file_path", tmpFile)

	g, err := graph.NewBuilder("fileread_demo").
		Vertex("path_src").Op("file_path").
		Output("Result", "path_wire").

		Vertex("read_file").Op("FileReadOp").
		Input("Path", "path_wire").
		Output("Content", "file_content").

		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{
		"path": tmpFile,
	}
	if raw, ok := eng.GetOutput("file_content"); ok {
		out["content"] = *raw.(*string)
	}

	enc := json.NewEncoder(os.Stdout)
	enc.SetIndent("", "  ")
	enc.Encode(out)
}