mem

package
v0.1.0-dev.20260311011736 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatLiteral

func FormatLiteral(v starlark.Value) (string, error)

FormatLiteral serializes a frozen Starlark value as a valid Starlark source literal. Used to inline closure bindings in synthetic files.

Supports: String, Int, Float, Bool, NoneType, List, Dict, Tuple, Struct. Struct values (e.g., marshaled Resources) are serialized as dict literals with sorted keys for deterministic output. Returns an error for types that cannot be represented as source literals.

func ValidateArity

func ValidateArity(fn *starlark.Function, minParams, maxParams int) error

ValidateArity checks that a function's arity is compatible with the target action's expected parameter range.

Types

type Callable

type Callable struct {
	Resource // embeds mem.Resource (source text in Data)

	// Compiled bytecode — Program.Write output. Nil until Compile.
	Compiled []byte

	// URI identity fields — compose the opaque URI:
	// mem:callable/<FuncType>/<Name>
	FuncType string // named Go type: "file.Reducer", "Predicate"
	Name     string // function name or <action>.<param> for lambdas

	// Metadata captured at extraction time.
	FuncName        string   // function name in synthetic file ("_callable" or original)
	ParamNames      []string // parameter names
	NumParams       int      // total params (for validation)
	CompilerVersion uint32   // starlark.CompilerVersion at compile time
	OriginalPos     string   // "recipe.star:42" (diagnostics only)
	// contains filtered or unexported fields
}

Callable is a mem.Resource that holds a Starlark function extracted into a self-contained synthetic source file with compiled bytecode.

The URI is opaque: mem:callable/<FuncType>/<Name>. FuncType is the named Go type the callable satisfies (e.g., "file.Reducer", "Predicate"). Name is the function name or <action>.<param> for lambdas.

The Data field (inherited from Resource) holds the synthetic source text. The Compiled field holds serialized bytecode from Program.Write. Both are []byte — serializable, transferable, persistable.

Lifecycle:

  1. Extract(*starlark.Function) → Callable (Phase 2)
  2. Compile() → populates Compiled bytecode (Phase 3)
  3. Init(thread) → loads bytecode, extracts live fn (Phase 3)
  4. Fn() → returns live callable for adapter invocation

func Extract

func Extract(fn *starlark.Function, funcType string) (*Callable, error)

Extract introspects a *starlark.Function and produces a self-contained Callable with synthesized source text. The synthetic file inlines all closure bindings as module-level constants, making it independent of the original script.

The function name is derived from fn.Name() (or "<action>.<param>" for lambdas when the caller provides a fallback via ExtractWithName).

Parameters:

  • fn: Starlark function to extract
  • funcType: Go type name the callable satisfies (e.g., "file.Reducer")

Returns:

  • *Callable: the extracted callable with synthesized source
  • error: any extraction error

func ExtractWithName

func ExtractWithName(fn *starlark.Function, funcType, name string) (*Callable, error)

ExtractWithName is like Extract but allows the caller to specify the callable name (useful for lambdas where the default name is "lambda").

Parameters:

  • fn: Starlark function to extract
  • funcType: Go type name the callable satisfies (e.g., "file.Reducer")
  • name: Name for the callable (overrides fn.Name())

Returns:

  • *Callable: the extracted callable with synthesized source
  • error: any extraction error

func NewCallable

func NewCallable(funcType, name string) *Callable

NewCallable creates a Callable with the given function type and name. The source text (Data) and compiled bytecode should be set by the extraction and compilation phases.

func (*Callable) Compile

func (c *Callable) Compile() error

Compile compiles the synthetic source text and stores the bytecode. Called once after extraction. Idempotent — recompiling with the same source and compiler version produces the same bytecode.

func (*Callable) Fn

func (c *Callable) Fn() starlark.Callable

Fn returns the live callable. Panics if Init has not been called.

func (*Callable) FuncTypeName

func (c *Callable) FuncTypeName() string

FuncTypeName returns the Go type name this callable satisfies. Implements op.CallableResource.

func (*Callable) Init

func (c *Callable) Init(thread *starlark.Thread) error

Init loads the compiled program (or recompiles from source on version mismatch) and extracts the callable function. Must be called before Fn.

func (*Callable) SetSource

func (c *Callable) SetSource(source []byte)

SetSource sets the synthetic source text and recomputes the hash.

type Resource

type Resource struct {
	op.ResourceBase
	ContentType string // "callable", "json", "template", etc.
	Qualifier   string // type-specific qualifier (e.g., "file.Reducer/myfn" for callables)
	Data        []byte // raw content
	Hash        string // SHA-256 of Data — metadata, NOT part of URI
}

Resource represents an in-memory data resource identified by a mem: URI.

Unlike file or git resources that reference external systems, a mem.Resource holds its content directly. The Data field contains the raw bytes (source text, JSON, template content, etc.). The ContentType field classifies the content for dispatch (e.g., "callable", "json", "template").

The URI is opaque: mem:<content-type>/<qualifier>. The content hash is stored as a metadata field for change detection — NOT part of the URI. Two resources with the same URI but different hashes trigger a catalog shadow.

func NewResource

func NewResource(contentType, qualifier string) Resource

NewResource creates a mem.Resource with the given content type and qualifier.

Data must be set separately; Hash is computed when ComputeHash is called.

func NewResourceWithData

func NewResourceWithData(contentType, qualifier string, data []byte) Resource

NewResourceWithData creates a mem.Resource with content and computes the hash.

func ResourceFromValue

func ResourceFromValue(v any) (Resource, error)

ResourceFromValue constructs a mem.Resource from a string mem: URI.

Parameters:

  • v: expected to be a string in the format "mem:<content-type>[/<qualifier>]"

Returns:

  • Resource: initialized with the parsed content type and qualifier
  • error: if v is not a string or the URI format is invalid

func (*Resource) ComputeHash

func (r *Resource) ComputeHash()

ComputeHash calculates the SHA-256 hash of Data and stores it in Hash.

func (Resource) String

func (r Resource) String() string

String returns a compact JSON representation of the resource.

Jump to

Keyboard shortcuts

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