api

package module
v1.0.0-alpha.16 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 6 Imported by: 0

README

Universal Ferret API

Portable contracts for compiled execution and retained source-level debugging. Implementations own engine configuration; consumers share Runtime, reusable Plan, per-run Session, and the types in source, result, debugger, and diagnostics.

Execution and ownership

Runtime.Compile and CompileDebug finish compilation before returning a plan. Syntax and compiler errors are immediate. Plans support repeated and concurrent sessions with independent parameters and filesystem configuration; Params returns a caller-owned snapshot.

Each object releases the resources it owns. Owning runtimes reject subsequent work according to their closed-state semantics. Borrowing adapters may document a no-op Close that leaves the adapter and underlying runtime usable; the underlying owner remains responsible for cleanup.

Plan closure prevents subsequent session and debug-session creation. It does not implicitly close or cancel already-created sessions or debug sessions. Returned descendants remain responsible for their own lifecycle. Parent closure does not implicitly cancel caller-owned operations and need not wait for operations or constructors already started. Callers coordinate outstanding work and cleanup when descendants use parent-owned resources.

Close is idempotent and retains the completed cleanup result, without requiring identical error-wrapper pointers. Ordinary execution owners cancel and settle running work before closing the session. Debugger session closure terminates and settles active commands. Runtime.Run owns its temporary session and plan and returns execution and cleanup errors together, preserving available output. Output and inspection snapshots belong to their callers.

Runtime.Run and Session.Run return (*Output, error). Output presence is independent of the error:

Output Error Meaning
nil non-nil No output was produced.
non-nil nil Execution succeeded, including empty output.
non-nil non-nil Output was produced, but cleanup or other processing also failed.

A non-nil &Output{} is present output; the zero value is not an absence sentinel. Inspect output independently of the error:

output, err := runtime.Run(ctx, api.NewAnonymousSource("RETURN 42"))
if output != nil {
    consume(output.ContentType, output.Content)
}
if err != nil {
    return err
}

The output fields and their serialized representation are unchanged. Transports preserve output presence through their own representations.

Non-nil caller contexts control cancellation of Run, Compile, CompileDebug, NewSession, and NewDebugSession. Cancellation errors preserve context.Canceled and context.DeadlineExceeded through errors.Is. Implementations need not derive operation contexts to coordinate parent Close. They may use internal contexts for their own resources and may translate portable option callbacks before validating the operation context.

Options

Session options target an implementation of SessionOptions, which may apply settings directly or queue implementation-specific options. Non-nil callbacks run exactly once in order, and their returned errors are joined. Later options can override earlier values; parameter maps merge. Runtime-specific extensions validate their target explicitly.

Portable or application-level errors may be returned immediately by an option callback. Runtime-specific conversion and validation may be deferred until the setting is used. This includes host-parameter conversion, output codecs, filesystem-root construction, and runtime-specific capabilities. A nil setter error therefore does not certify runtime validity.

Invalid settings must fail the operation no later than their relevant point of use. Implementations need not preflight all session configuration before compilation, resource acquisition, or execution. Runtime.Run may compile, create a session, then execute. Output codec availability may be validated during result encoding, after the query has run. Implementations document validation timing and when mutable inputs are converted or snapshotted.

WithOptimizationLevel rejects values outside the portable enum during callback application. Each runtime defines which known optimization levels it supports and any restrictions for debug compilation.

Portable data

Native Ferret produces one-based lines and byte columns, with zero-based, half-open byte spans. Source names are identities and need not be filesystem paths; anonymous sources have an empty name. Adapters translate native indexed source text into the portable Source representation. Portable coordinates, encoded output, debugger values, variables, frames, breakpoints, reasons, and events preserve their existing fields and JSON representations.

debugger.ValueReference.Valid accepts positive references. References are scoped to a paused state and become stale when execution resumes. debugger.NoFunction identifies the top-level program body. Compiler table identifiers and validation remain implementation details.

Debugger commands are StepIn, StepOver, and StepOut. Breakpoint requests use canonical source locations and binding options. A completed event and its output can accompany a later cleanup error. Error projections should preserve each diagnostic's source, annotation order, joined branches, and native causes through standard Go error traversal.

Documentation

Overview

Package api defines implementation-independent contracts for compiling, executing, and debugging Ferret queries.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Location

type Location = source.Location

Location represents the location of a specific point in a source file, including the file name and position.

type OptimizationLevel

type OptimizationLevel int
const (
	OptimizationNone OptimizationLevel = iota
	OptimizationBasic
	OptimizationFull
	OptimizationAggressive
)

type Output

type Output = result.Output

Output is the encoded result returned from session or runtime execution. Execution returns a pointer: nil means no output was produced, while a non-nil pointer to a zero-valued Output still represents produced output.

type Plan

type Plan interface {
	io.Closer
	Params() []string
	NewSession(ctx context.Context, opts ...SessionOption) (Session, error)
	NewDebugSession(ctx context.Context, opts ...SessionOption) (debugger.Session, error)
}

Plan represents a compiled program. Compilation finishes before a plan is returned. Plans support independent sessions and are not consumed by execution. Params returns a caller-owned snapshot.

Close releases plan-owned resources and prevents subsequent session and debug-session creation. It is idempotent and retains its cleanup result, without requiring identical error-wrapper pointers. Close need not wait for constructors already started. It does not implicitly close or cancel returned sessions or debug sessions; callers remain responsible for their lifecycle.

NewSession and NewDebugSession use non-nil caller contexts for cancellation. Close does not cancel those contexts. Callers coordinate work and cleanup when sessions use plan-owned resources.

type PlanOption

type PlanOption = func(PlanOptions) error

func WithOptimizationLevel

func WithOptimizationLevel(level OptimizationLevel) PlanOption

WithOptimizationLevel sets the optimization level for the execution plan. The callback rejects unknown enum values before invoking the target setter; the runtime determines which known levels it supports.

type PlanOptions

type PlanOptions interface {
	SetOptimizationLevel(OptimizationLevel) error
}

type Position

type Position = source.Position

Position represents a specific point in a source file, defined by line and column numbers.

type Range

type Range = source.Range

Range represents a range of characters in a source file, including the location and span.

type Runtime

type Runtime interface {
	io.Closer
	// Run returns nil output with an error when no output was produced.
	// A non-nil output with a nil error indicates success, including empty output.
	// A non-nil output may accompany an error from cleanup or other processing;
	// callers must inspect output independently of the error.
	Run(ctx context.Context, src Source, opts ...SessionOption) (*Output, error)
	Compile(ctx context.Context, src Source, opts ...PlanOption) (Plan, error)
	CompileDebug(ctx context.Context, src Source, opts ...PlanOption) (Plan, error)
}

Runtime compiles source into reusable plans. Callers own directly created plans and sessions and are responsible for their cleanup.

Close releases implementation-owned resources and is idempotent, retaining its cleanup result without requiring identical error-wrapper pointers. Owning runtimes reject subsequent work according to their closed-state semantics. Borrowing adapters may document a no-op Close that leaves the adapter and underlying runtime usable. Close does not implicitly cancel caller-owned work and need not wait for operations already started.

Run, Compile, and CompileDebug use non-nil caller contexts for cancellation. Callers coordinate work and cleanup when descendants use parent-owned resources. Run closes its temporary session and plan, preserving execution and cleanup errors together with any available encoded output.

type Session

type Session interface {
	io.Closer
	// Run returns nil output with an error when no output was produced.
	// A non-nil output with a nil error indicates success, including empty output.
	// A non-nil output may accompany an error from cleanup or other processing;
	// callers must inspect output independently of the error.
	Run(c context.Context) (*Output, error)
}

Session executes a compiled plan with per-session configuration. Run observes its non-nil context and returns caller-owned encoded output. Unless documented otherwise, callers serialize Run and settle it before Close. Close is idempotent and retains its cleanup result, without requiring identical error-wrapper pointers.

type SessionOption

type SessionOption = func(SessionOptions) error

SessionOption applies portable or application-specific session configuration. Implementations invoke non-nil callbacks once in order and join their returned errors. Callbacks may return errors immediately; runtime-specific validation may instead fail in the operation that uses the setting. See SessionOptions.

func WithFSRoot

func WithFSRoot(root string) SessionOption

WithFSRoot selects the rooted filesystem used by one execution session. The runtime defines path validation and owns any filesystem resources created for the session. Validation and construction may occur when the root is applied.

func WithOutputContentType

func WithOutputContentType(contentType string) SessionOption

WithOutputContentType selects the output codec content type for session results. Codec availability may be checked when output is encoded, after query execution.

func WithParam

func WithParam(key string, value any) SessionOption

WithParam sets a session parameter for the execution. The runtime owns conversion and validation of the host value.

func WithParams

func WithParams(params map[string]any) SessionOption

WithParams merges the provided parameter map into the session environment, overriding existing keys while preserving any other previously defined parameters. The runtime defines when values are converted or snapshotted.

type SessionOptions

type SessionOptions interface {
	SetParam(string, any) error
	SetParams(map[string]any) error
	SetOutputContentType(string) error
	SetFSRoot(string) error
}

SessionOptions configures a session directly or queues implementation options. Setters may report portable or application-level errors immediately and defer runtime-specific conversion and validation until the setting is used. A nil setter error does not certify that the runtime can use the value.

Invalid settings must fail the operation no later than their relevant point of use. Validation need not precede compilation, resource acquisition, or all query execution. Output codec availability may be checked during result encoding, after the query has run. Implementations document validation and mutable-input conversion or snapshot timing.

Later setters override earlier values; SetParams merges keys. Runtime-specific options reject incompatible targets.

type Source

type Source = source.Source

Source represents the input data for a Ferret query.

func NewAnonymousSource

func NewAnonymousSource(content string) Source

NewAnonymousSource creates a new anonymous Source instance with the given content.

func NewSource

func NewSource(name, content string) Source

NewSource creates a new Source instance with the given name and content.

type Span

type Span = source.Span

Span represents a range of characters in a source file, defined by start and end positions.

Directories

Path Synopsis
Package debugger defines portable contracts and values for controlling and inspecting Ferret debug sessions, including events, breakpoints, frames, and values.
Package debugger defines portable contracts and values for controlling and inspecting Ferret debug sessions, including events, breakpoints, frames, and values.
Package diagnostics defines the portable structured reporting model shared by Ferret runtimes and tooling.
Package diagnostics defines the portable structured reporting model shared by Ferret runtimes and tooling.
Package result defines portable encoded outputs returned by Ferret query execution.
Package result defines portable encoded outputs returned by Ferret query execution.
Package source defines portable Ferret source inputs and coordinates.
Package source defines portable Ferret source inputs and coordinates.

Jump to

Keyboard shortcuts

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