ingest

package
v0.2.4 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2026 License: MIT Imports: 27 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.

Documentation

Overview

Package ingest turns a reference — a path, a pattern, a URL, stdin — into documents, and records where every document came from.

The readers here are the ones the standard library can serve honestly: files, directory trees, HTTP(S), and readers already in memory. Everything else (object stores, SaaS APIs, databases, message queues) is a driver: implement semantic.Ingester and put it in a Registry under its scheme. No source is faked.

A document without its origin is not evidence, so every reader fills Doc.Meta with an Origin: the reference, byte offset, size, modification time, content hash and type. Read it back with OriginOf.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrScheme means no reader is registered for the reference's scheme.
	ErrScheme = errors.New("ingest: no reader for scheme")
	// ErrEmpty means the reference was blank.
	ErrEmpty = errors.New("ingest: empty reference")
	// ErrSize means the source is larger than the reader was allowed to read.
	ErrSize = errors.New("ingest: source over size limit")
	// ErrBlocked means a fetch was aimed at an address it may not reach:
	// loopback, private, link-local or otherwise internal.
	ErrBlocked = errors.New("ingest: address blocked")
)

Errors this package reports. Test for them with errors.Is.

View Source
var Default = &Registry{by: map[string]semantic.Ingester{
	"file":  File{},
	"dir":   Dir{},
	"glob":  Glob{},
	"http":  Web{},
	"https": Web{},
	"stdin": Reader{},
}}

Default resolves the schemes this package implements: file, dir, glob, http, https and stdin.

Functions

func Record

func Record(d semantic.Doc) map[string]any

Record returns the structured record a document was built from — one JSON object, one JSONL line, one CSV row — or nil when the document is plain text.

func Scheme

func Scheme(ref string) string

Scheme names the kind of source a reference points at.

A URL yields its own scheme. "-" is standard input. A local path yields "glob" when it contains pattern characters, "dir" when it names a directory that exists, and "file" otherwise — so, like the Python it is ported from, this looks at the filesystem to tell a directory from a file.

func Title

func Title(d semantic.Doc) string

Title returns the page title HTML recorded, or "".

Types

type Decoder

type Decoder interface {
	Decode(b []byte, o Origin) ([]semantic.Doc, error)
}

Decoder turns the bytes of one source into documents. Sources read bytes; decoders decide how many documents those bytes are. Every reader in this package takes one, so the same JSON or CSV handling serves a file, a fetch and standard input.

type Dir

type Dir struct {
	// Match keeps only paths matching one of these patterns. A pattern is
	// tried against the file's name and against its path relative to the
	// root, with path.Match syntax: "*.md", "docs/*.json". Empty keeps
	// everything.
	Match []string
	// Skip drops paths matching one of these patterns, applied after Match.
	Skip []string
	// Depth limits how deep the walk goes: 1 is the directory itself, 2 its
	// subdirectories, and 0 — the zero value — is unlimited.
	Depth int
	// Min and Max bound file size in bytes. Max zero means no upper bound.
	Min, Max int64
	// Hidden includes files and directories whose name begins with a dot.
	Hidden bool
	// Decode turns each file's bytes into documents, as in File.
	Decode Decoder
}

Dir walks a directory tree and reads the files in it.

Documents read before an error are still returned alongside it, so one unreadable file does not discard a walk of ten thousand.

func (Dir) Ingest

func (d Dir) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest walks the tree rooted at ref.

type File

type File struct {
	// Max is the largest file this reader will read, in bytes. Zero means
	// no limit.
	Max int64
	// Decode turns the bytes into documents. Nil picks a decoder from the
	// file's extension: json, jsonl, csv, tsv, html, otherwise plain text.
	Decode Decoder
}

File reads one file. How many documents it yields depends on what the file holds: text is one document, a JSON array or a CSV is one per record.

func (File) Ingest

func (f File) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest reads the file at ref. A "file://" prefix is accepted and stripped.

type Func

type Func func(context.Context, string) ([]semantic.Doc, error)

Func adapts a plain function to semantic.Ingester, so a driver can be one function rather than a type.

func (Func) Ingest

func (f Func) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest calls f.

type Glob

type Glob struct {
	// Max is the largest file this reader will read, in bytes, as in File.
	Max int64
	// Decode turns each file's bytes into documents, as in File.
	Decode Decoder
}

Glob reads every file matching a shell pattern, in sorted order.

func (Glob) Ingest

func (g Glob) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest reads the files matching the pattern ref. As with Dir, documents read before an error are returned alongside it.

type HTML

type HTML struct{}

HTML reads a page as text: script and style contents are dropped, tags become spaces, entities are unescaped, and the title is kept in Doc.Meta.

func (HTML) Decode

func (HTML) Decode(b []byte, o Origin) ([]semantic.Doc, error)

Decode strips b of markup.

type JSON

type JSON struct {
	// Field names the string member that holds a record's text. When it is
	// empty the whole record is the text, as compact JSON.
	Field string
}

JSON reads a JSON document: an array becomes one document per element, anything else becomes one document.

func (JSON) Decode

func (j JSON) Decode(b []byte, o Origin) ([]semantic.Doc, error)

Decode parses b as JSON and turns it into documents.

type Lines

type Lines struct {
	// Field names the string member that holds a record's text, as in JSON.
	Field string
}

Lines reads JSON Lines: one JSON value per line, one document per value. Blank lines are skipped. Each document's offset is the byte offset of its line, so a document can be traced back to the exact bytes it came from.

func (Lines) Decode

func (l Lines) Decode(b []byte, o Origin) ([]semantic.Doc, error)

Decode parses b as JSON Lines.

type Origin

type Origin struct {
	// Ref is the path or URL the text was read from.
	Ref string
	// Offset is the byte offset of this document within Ref. It is 0 for a
	// whole-source document and the start of the record for one row or line.
	Offset int64
	// Size is the number of bytes read from Ref.
	Size int64
	// Mod is the source's modification time, zero when the source has none.
	Mod time.Time
	// Hash is the SHA-256 of the bytes read, hex encoded.
	Hash string
	// Type is the source's kind: an extension such as "json" or "csv", or
	// what the leading bytes say it is.
	Type string
	// Mime is the media type when one is known.
	Mime string
	// Status is the HTTP status of a fetch, 0 for sources that have none.
	Status int
	// At is when the read happened.
	At time.Time
}

Origin is where a document's text came from. Readers write it into Doc.Meta so provenance survives any later stage that only copies the map.

func OriginOf

func OriginOf(d semantic.Doc) Origin

OriginOf reads back the origin a reader recorded on d. Values that have been through JSON — where an int becomes a float and a time becomes a string — are understood, so a document reloaded from disk still reports where it came from.

func (Origin) Meta

func (o Origin) Meta() map[string]any

Meta renders the origin as the map a Doc carries.

type Raw

type Raw struct{}

Raw reads the bytes as one document of text.

func (Raw) Decode

func (Raw) Decode(b []byte, o Origin) ([]semantic.Doc, error)

Decode returns a single document holding the whole source.

type Reader

type Reader struct {
	// R is the stream to read. Nil means os.Stdin.
	R io.Reader
	// Source names the origin. Empty falls back to the reference passed to
	// Ingest, then to "stdin".
	Source string
	// Type says how to read the bytes ("json", "csv", …) when Decode is nil.
	Type string
	// Decode turns the bytes into documents. Nil picks a decoder from Type.
	Decode Decoder
}

Reader reads everything from R as one source. A nil R reads standard input, which makes the zero value the stdin reader.

func (Reader) Ingest

func (r Reader) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest reads R to the end and decodes it. The reference is used only to name the origin when Source is empty.

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

Registry resolves a reference to the reader that can read it, and is itself a semantic.Ingester, so a pipeline can take the registry and accept whatever the caller points it at.

It is also the seam for sources this package does not implement. A driver for object storage, a wiki, a queue or a database registers itself under its scheme and is reached by the same call:

ingest.Default.Set("s3", myBucketReader)
docs, err := ingest.Default.Ingest(ctx, "s3://bucket/key")

A file extension works as a key too, so a reader for a binary format takes over the paths that name it:

ingest.Default.Set("parquet", myParquetReader)

The zero Registry is empty and usable; Default holds the readers here.

func (*Registry) Drop

func (r *Registry) Drop(scheme string)

Drop removes the reader registered under scheme.

func (*Registry) Get

func (r *Registry) Get(scheme string) (semantic.Ingester, bool)

Get returns the reader registered under scheme.

func (*Registry) Ingest

func (r *Registry) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest reads whatever ref points at. A URL goes to its scheme's reader; a local path goes to a reader registered for its extension if there is one, otherwise to the reader for its shape — glob, dir or file.

func (*Registry) Schemes

func (r *Registry) Schemes() []string

Schemes lists what is registered, in order.

func (*Registry) Set

func (r *Registry) Set(scheme string, in semantic.Ingester)

Set registers in under scheme, replacing whatever was there.

type Rows

type Rows struct {
	// Sep is the field delimiter. Zero sniffs the header line for one of
	// comma, semicolon, tab or pipe.
	Sep rune
	// Cols names the columns; a column past the end of Cols, or named by an
	// empty entry, is named by its index. When Cols is empty the first row
	// is the header.
	Cols []string
	// Field names the column that holds a row's text. When it is empty the
	// text is every column as "name: value" lines.
	Field string
}

Rows reads delimited text — CSV, TSV, anything with one record per line — into one document per row.

A source that is not valid UTF-8 is read as Latin-1, as the Python this ports does after sniffing the encoding; offsets then refer to the decoded text rather than the source bytes.

func (Rows) Decode

func (r Rows) Decode(b []byte, o Origin) ([]semantic.Doc, error)

Decode parses b as delimited text.

type Text

type Text struct {
	// Source names the origin, "text" when empty.
	Source string
	// Type says how to read the text ("json", "csv", …) when Decode is nil.
	Type string
	// Decode turns the text into documents. Nil picks a decoder from Type.
	Decode Decoder
}

Text treats the reference itself as the document body, for text already in hand rather than at a location.

func (Text) Ingest

func (t Text) Ingest(ctx context.Context, s string) ([]semantic.Doc, error)

Ingest decodes s as the whole source.

type Web

type Web struct {
	// Client sends the request. Nil uses a shared client that enforces the
	// address rules on every connection, including redirects; a client
	// supplied here is used as given, and only the pre-flight check applies.
	Client *http.Client
	// Agent is the User-Agent header, "semantic" when empty.
	Agent string
	// Header carries additional request headers.
	Header http.Header
	// Timeout bounds the whole fetch, 30s when zero.
	Timeout time.Duration
	// Max is the largest response body to read, in bytes. Zero means no
	// limit.
	Max int64
	// Private allows internal addresses.
	Private bool
	// Decode turns the body into documents. Nil picks a decoder from the
	// response content type, falling back to the URL's extension.
	Decode Decoder
}

Web fetches one HTTP or HTTPS URL.

By default it refuses to reach an internal address — loopback, private, link-local, carrier NAT, cloud metadata — because a URL is usually attacker-influenced and the machine doing the fetching can see more of the network than the person asking for it. The check runs before the request and again on the socket the connection actually lands on, so a hostname that resolves to a public address and then to 127.0.0.1 still fails. Set Private to turn it off for a trusted internal deployment.

func (Web) Ingest

func (w Web) Ingest(ctx context.Context, ref string) ([]semantic.Doc, error)

Ingest fetches ref and decodes the response body.

Jump to

Keyboard shortcuts

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