file

package module
v0.0.0-...-12a90eb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 23, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

sink/file

A crucible/sink destination that appends one JSON line per payload to an io.Writer or an append-only file. Runtime dependencies: the standard library and crucible/sink only.

// Write to any io.Writer (in-memory, network, etc.)
outlet := file.New(os.Stdout)
_ = outlet.Sink(ctx, OrderShipped{OrderID: "ORD-1", SKU: "WIDGET"})
// stdout: {"order_id":"ORD-1","sku":"WIDGET"}

// Or open and own a file:
outlet, err := file.Open("/var/log/events.jsonl")
if err != nil { /* handle */ }
defer outlet.(sink.Shutdowner).Shutdown(ctx)

m := sink.NewManifold()
m.Attach(outlet)
m.Sink(ctx, OrderShipped{OrderID: "ORD-2", SKU: "GIZMO"})

Each payload is marshaled with encoding/json and written as a single line followed by a newline (\n), producing a valid JSONL stream. The outlet accepts every payload type without a registry; a marshal failure returns a *sink.Error with PhaseApply and Outlet=="file".

Optional capabilities:

  • sink.Flusher: calls Sync() on the underlying writer when available (e.g. *os.File). A no-op for writers that do not implement Sync.
  • sink.Shutdowner: closes the file when the outlet was created by Open. Idempotent; a no-op for New-created outlets.

The outlet is safe for concurrent use.

Stability

Experimental (pre-v1). The API may change until the suite locks v1.0.0.

License

Apache-2.0. See LICENSE and NOTICE.

Documentation

Overview

Package file is a sink destination that appends one JSON line per payload to an io.Writer or to a file opened by path. It depends only on the standard library and crucible/sink.

Use New to wrap any io.Writer, or Open to have the outlet own an append-only file. Each call to Sink marshals the payload as JSON and writes it followed by a newline, producing a valid JSONL (newline-delimited JSON) stream. The outlet is safe for concurrent use.

The outlet accepts every payload type (no registry is involved). An encoding/json marshal failure is returned as a *csink.Error with csink.PhaseApply and Outlet=="file".

Optional capabilities:

  • csink.Flusher: calls the underlying writer's Sync method when the writer implements interface{ Sync() error } (e.g. *os.File), otherwise Flush is a no-op.
  • csink.Shutdowner: closes the file when the outlet owns one (i.e. was created by Open). Shutdown is idempotent.

Stability

Experimental (pre-v1); the API may change until the suite locks v1.0.0.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(w io.Writer, _ ...Option) csink.Outlet

New returns an csink.Outlet that marshals each payload as JSON and writes it as a single line (JSONL) to w. The outlet is safe for concurrent use. Any marshal failure is returned wrapped in a *csink.Error.

Example

ExampleNew demonstrates writing JSONL records to an in-memory buffer.

// SPDX-License-Identifier: Apache-2.0

package main

import (
	"bytes"
	"context"
	"fmt"

	filesink "github.com/stablekernel/crucible/sink/file"
)

type orderShipped struct {
	OrderID string `json:"order_id"`
	SKU     string `json:"sku"`
}

// ExampleNew demonstrates writing JSONL records to an in-memory buffer.
func main() {
	var buf bytes.Buffer
	outlet := filesink.New(&buf)

	_ = outlet.Sink(context.Background(), orderShipped{OrderID: "ORD-1", SKU: "WIDGET"})

	fmt.Print(buf.String())
}
Output:
{"order_id":"ORD-1","sku":"WIDGET"}

func Open

func Open(path string, opts ...Option) (csink.Outlet, error)

Open opens the file at path with O_APPEND|O_CREATE|O_WRONLY and returns an csink.Outlet that writes JSONL records to it. The outlet owns the file and will close it on csink.Shutdowner.Shutdown.

The caller should call Shutdown when done to release the file descriptor.

Types

type Option

type Option func(*outlet)

Option is a functional option for New and Open. No options are defined yet; the parameter is reserved for future extensibility.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL