Help us shape the future of Go! Take the 2026 Go Developer Survey

projection

package
v0.63.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Overview

Package projection adds the `{ ... }` projection syntax. The parser dispatches the opening `{` through this bundle's PrimaryExtension; the ProjectionExpr it produces is lowered at build time to the projection runtime action, which evaluates the body with expr-lang. Without this bundle, `{ }` in expression position is a syntax error.

Typical use:

{ name: "Maksymilian", extra: true } -> { name: .name }
{ id: 1, status: "new" } -> { ..., status: "active" }

Index

Constants

View Source
const ID = "projection"

Variables

This section is empty.

Functions

func Bundle

func Bundle(_ map[string]string) compiler.Bundle

func DefaultEvaluator

func DefaultEvaluator(_ context.Context, req Request) (any, error)

DefaultEvaluator returns the input unchanged.

func Library

func Library(eval Evaluator) action.Library

Library returns the runtime projection action bound to eval. A nil eval falls back to DefaultEvaluator, which is a pass-through — used only in isolated parser tests.

func Primary

func Primary() compiler.PrimaryExtension

Primary returns the parser extension. Exposed so tests can build a parser without spinning up the whole bundle.

func PrimaryWithFunctions added in v0.63.0

func PrimaryWithFunctions(functions *FunctionSet) compiler.PrimaryExtension

PrimaryWithFunctions returns a projection primary carrying this file-local function set. It merges with the ordinary projection primary through the compiler's existing MergeablePrimary contract.

func RewriteFunctionReferences added in v0.63.0

func RewriteFunctionReferences(source string, references map[string]string) string

RewriteFunctionReferences replaces qualified function names while preserving quoted text and ordinary member access. It is used when an imported module's local function names are mounted below its @include namespace.

Types

type Evaluator

type Evaluator func(context.Context, Request) (any, error)

Evaluator converts a projection body into a value. NewExprEvaluator is the production implementation; DefaultEvaluator is a pass-through used only in isolated tests.

func NewExprEvaluator

func NewExprEvaluator() Evaluator

NewExprEvaluator compiles each projection body once and caches it. The transformation rules are documented in buildProgramSource and preprocessDots.

type Extension

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

Extension is the compile-time parser extension. It dispatches on the opening brace and scans the raw source up to the matching close, tracking quotes so braces inside strings are ignored.

func (Extension) MergeWith added in v0.63.0

MergeWith carries a source-local function set into the existing projection parser. The compiler merges it only into compilations that declared or inherited those functions.

func (Extension) Name

func (Extension) Name() string

func (Extension) Parse

func (e Extension) Parse(p *compiler.Parser) (compiler.Expr, error)

func (Extension) TokenType

func (Extension) TokenType() compiler.TokenType

type FunctionDefinition added in v0.63.0

type FunctionDefinition struct {
	Name       string
	Parameters []FunctionParameter
	Result     string
	Body       string
	Position   compiler.Position
}

FunctionDefinition is the expression-only form of an NFlow @func declaration. The projection extension owns expression compilation so calls receive normal expr-lang type checking without a process-global function registry.

type FunctionParameter added in v0.63.0

type FunctionParameter struct {
	Name string
	Type string
}

FunctionParameter describes one typed parameter in a source-local @func.

type FunctionSet added in v0.63.0

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

FunctionSet is immutable after construction and can be shared by the projections compiled from one source/module. Its cache is deliberately local to that source; identical expression text in another source cannot reuse a program with different function bindings.

func MergeFunctionSets added in v0.63.0

func MergeFunctionSets(older, newer *FunctionSet) *FunctionSet

MergeFunctionSets combines inherited and local declarations without silently shadowing a function name. Identical inherited declarations are de-duplicated.

func NewFunctionSet added in v0.63.0

func NewFunctionSet(definitions []FunctionDefinition) *FunctionSet

NewFunctionSet copies declarations and assigns deterministic internal names for expr-lang, whose custom-function API accepts simple identifiers only.

func (*FunctionSet) Compile added in v0.63.0

func (s *FunctionSet) Compile(source string, position compiler.Position) (compiler.ExpressionProgram, error)

Compile rewrites source-visible function names and compiles an expression with this immutable source-local function environment.

func (*FunctionSet) Validate added in v0.63.0

func (s *FunctionSet) Validate() error

Validate compiles all declarations, checking signatures, bodies, references, result types, and recursion before a program is built.

type Request

type Request struct {
	Raw   string
	Input any
}

Request is one projection evaluation.

Jump to

Keyboard shortcuts

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