expr

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const FinalName = "FINAL"

FinalName is the namespace through which end-of-scope values are read.

Variables

View Source
var PositionNames = []string{"VERTICAL_POSITION", "VERTICAL_SPACE"}

PositionNames are the two predefined names whose value depends on where in the frame a band lands. A band whose expressions read either is not cached.

View Source
var Predeclared = starlark.StringDict{
	"math":     starmath.Module,
	"time":     timeModule,
	"strftime": starlark.NewBuiltin("strftime", builtinStrftime),
	"format":   starlark.NewBuiltin("format", builtinFormat),
	"decimal":  starlark.NewBuiltin("decimal", builtinDecimal),
	"quantize": starlark.NewBuiltin("quantize", builtinQuantize),

	"float": starlark.NewBuiltin("float", builtinFloat),
	"int":   starlark.NewBuiltin("int", builtinInt),
}

Predeclared is the fixed part of every expression's environment: the modules and the engine's own builtins. Names here resolve ahead of parameters, variables, and record fields, which is why a template may not reuse one.

View Source
var PredefinedNames = []string{
	"THIS",
	"ITEM_NUMBER",
	"DATA_COUNT",
	"REPORT_COUNT",
	"PAGE_COUNT",
	"COLUMN_COUNT",
	"PAGE_NUMBER",
	"COLUMN_NUMBER",
	"VERTICAL_POSITION",
	"VERTICAL_SPACE",
	"BUILD_TIME",
	"FINAL",
}

PredefinedNames are the names the engine supplies to every expression, per doc/expressions.md#predefined-variables. Per-group names -- <group>_COUNT and <group>_PAGE_NUMBER -- are added by the template.

Functions

func Format

func Format(spec string, args []starlark.Value) (string, error)

Format applies a %-style specification to the arguments.

This is the engine's formatter, reached from a field's or barcode's `format=` property and from the `format` builtin. Unlike Starlark's own `%` operator it supports the full set of flags, width, and precision, which is why numeric presentation belongs here rather than inside an expression.

func FormatArgs

func FormatArgs(value starlark.Value) []starlark.Value

FormatArgs turns an expression result into the argument list a format specification consumes. A tuple spreads positionally; anything else is a single argument.

func IsPredeclared

func IsPredeclared(name string) bool

IsPredeclared reports whether a name is supplied by the fixed environment.

func IsReserved

func IsReserved(name string) bool

IsReserved reports whether a name is unavailable for a parameter, variable, or group because resolution puts something else first.

func IsUniversal

func IsUniversal(name string) bool

IsUniversal reports whether a name is a Starlark builtin the dialect keeps.

func Str

func Str(value starlark.Value) string

Str renders a value the way %s does: a string is itself, everything else is its Starlark string form.

func Strftime

func Strftime(when gotime.Time, spec string) string

Strftime formats a time with the familiar % directives. It is locale-independent: month and day names are English.

func Truth

func Truth(value starlark.Value) bool

Truth applies the truth rules of doc/expressions.md#truth-values.

The one rule that is not Starlark's own is for a record, which is true whenever it is not None. Everything else -- including a time value, which is false only when it is the zero time -- follows the host dialect.

Types

type Decimal

type Decimal struct {
	Val decimal.Decimal
}

Decimal is an exact decimal value, declared by member type="decimal".

Arithmetic between decimals, and between a decimal and an int, is exact; the result's scale is what exactness requires. Division quantizes to six fractional digits, rounding half away from zero. Mixing a decimal with a float is an error rather than a silent conversion.

func NewDecimal

func NewDecimal(dec decimal.Decimal) Decimal

NewDecimal wraps a decimal.Decimal as a Starlark value.

func ParseDecimal

func ParseDecimal(text string) (Decimal, error)

ParseDecimal parses plain decimal text.

func (Decimal) BigInt

func (dec Decimal) BigInt() *big.Int

BigInt truncates toward zero.

func (Decimal) Binary

func (dec Decimal) Binary(
	op syntax.Token,
	operand starlark.Value,
	side starlark.Side,
) (starlark.Value, error)

Binary implements decimal arithmetic.

func (Decimal) CompareSameType

func (dec Decimal) CompareSameType(
	op syntax.Token,
	operand starlark.Value,
	depth int,
) (bool, error)

Cmp orders decimals against decimals and ints.

func (Decimal) Float

func (dec Decimal) Float() float64

Float converts to a float64, losing exactness.

func (Decimal) Freeze

func (dec Decimal) Freeze()

Freeze is a no-op: a Decimal is immutable.

func (Decimal) Hash

func (dec Decimal) Hash() (uint32, error)

Hash returns a hash of the value.

func (Decimal) Neg

func (dec Decimal) Neg() starlark.Value

Neg implements unary minus.

func (Decimal) Quantize

func (dec Decimal) Quantize(places int32) Decimal

Quantize rounds to places fractional digits, half away from zero.

func (Decimal) String

func (dec Decimal) String() string

String renders the value as plain decimal text, with no exponent.

The scale is preserved rather than trimmed: exactness decides how many fractional digits a result has, so adding two 2-place values shows two places and multiplying them shows four.

func (Decimal) Truth

func (dec Decimal) Truth() starlark.Bool

Truth reports whether the value is non-zero.

func (Decimal) Type

func (dec Decimal) Type() string

Type names the Starlark type.

func (Decimal) Unary

func (dec Decimal) Unary(op syntax.Token) (starlark.Value, error)

Unary implements unary operators on a decimal.

type Namespace

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

Namespace is a fixed set of named values reachable by attribute, used for FINAL.

func NewNamespace

func NewNamespace(name string, values map[string]starlark.Value) *Namespace

NewNamespace builds a namespace.

func (*Namespace) Attr

func (ns *Namespace) Attr(name string) (starlark.Value, error)

Attr looks a name up in the namespace.

func (*Namespace) AttrNames

func (ns *Namespace) AttrNames() []string

AttrNames lists the namespace's names, sorted.

func (*Namespace) Freeze

func (ns *Namespace) Freeze()

Freeze is a no-op.

func (*Namespace) Hash

func (ns *Namespace) Hash() (uint32, error)

Hash refuses.

func (*Namespace) String

func (ns *Namespace) String() string

String renders the namespace.

func (*Namespace) Truth

func (ns *Namespace) Truth() starlark.Bool

Truth reports true.

func (*Namespace) Type

func (ns *Namespace) Type() string

Type names the Starlark type.

type Program

type Program struct {
	// Source is the expression as written.
	Source string
	// Where names the template file, node path, and property, for diagnostics.
	Where string
	// Params are the free names, in the order the function takes them.
	Params []string
	// FinalNames are the names read through FINAL.
	FinalNames []string
	// UsesFinal reports whether FINAL appears at all.
	UsesFinal bool
	// UsesPosition reports whether the expression reads VERTICAL_POSITION
	// or VERTICAL_SPACE, which is what makes a band's measurement uncacheable.
	UsesPosition bool
	// contains filtered or unexported fields
}

Program is one compiled template expression.

Compilation happens once, at template load. The expression becomes a function whose parameters are exactly the names it references, so an evaluation costs one call with those values and nothing scales with the number of columns in the data.

func Compile

func Compile(where, source string) (*Program, error)

Compile parses and compiles one expression.

where is a human-readable location -- file, node path, property -- that every diagnostic from this expression carries.

func (*Program) Call

func (program *Program) Call(
	thread *starlark.Thread,
	args []starlark.Value,
) (starlark.Value, error)

Call evaluates the program. args must be positionally aligned with Params.

type Record

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

Record is a data record, or a nested JSON object declared type="object".

It supports both attribute access and subscripting, so a field is reachable as THIS.amount and as THIS["odd name"]. It is immutable, and true whenever it exists — an empty record is still true, which is why record truth is defined as "not None".

func NewRecord

func NewRecord(keys []string, values map[string]starlark.Value) *Record

NewRecord builds a record from ordered keys and their values.

func (*Record) Attr

func (record *Record) Attr(name string) (starlark.Value, error)

Attr implements attribute access.

func (*Record) AttrNames

func (record *Record) AttrNames() []string

AttrNames lists the field names, sorted, as Starlark requires.

func (*Record) Freeze

func (record *Record) Freeze()

Freeze is a no-op: a record is immutable.

func (*Record) Get

func (record *Record) Get(keyValue starlark.Value) (starlark.Value, bool, error)

Get implements subscripting, which is how a field whose name is not a valid identifier, or one that `records` does not declare, is reached.

func (*Record) Hash

func (record *Record) Hash() (uint32, error)

Hash refuses: a record is not usable as a dict key.

func (*Record) Keys

func (record *Record) Keys() []string

Keys returns the field names in declaration order.

func (*Record) String

func (record *Record) String() string

String renders the record for repr and str.

func (*Record) Truth

func (record *Record) Truth() starlark.Bool

Truth reports true. A record that is absent is None, not an empty record.

func (*Record) Type

func (record *Record) Type() string

Type names the Starlark type.

Jump to

Keyboard shortcuts

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