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 ¶
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 ¶
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.