variable

package
v0.3.17 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package variable provides a typed variable system for the graph engine. It supports variable schemas, reference resolution, validation, and default value application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsRef added in v0.1.6

func ContainsRef(s string) bool

ContainsRef reports whether s contains at least one resolvable ${scope.name} reference (i.e. the content inside braces must have a dot-separated scope and name, matching what lookup actually resolves).

func ExtractRefs

func ExtractRefs(text string) []string

ExtractRefs returns all ${scope.name} references found in a string.

Types

type Resolver

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

Resolver resolves ${scope.name} variable references in strings. It is not safe for concurrent use; use Clone to create independent copies for parallel branches.

func NewResolver

func NewResolver() *Resolver

NewResolver creates a new Resolver.

func (*Resolver) AddScope

func (r *Resolver) AddScope(name string, values map[string]any)

AddScope registers a named scope with its values.

func (*Resolver) Clone

func (r *Resolver) Clone() *Resolver

Clone creates an independent deep copy of this Resolver. All nested maps and slices are recursively copied so that parallel branches cannot mutate each other's state.

func (*Resolver) Resolve

func (r *Resolver) Resolve(template string) string

Resolve replaces all ${scope.name} references in template with actual values. References that cannot be resolved are left unchanged.

func (*Resolver) ResolveMap

func (r *Resolver) ResolveMap(config map[string]any) (map[string]any, error)

ResolveMap resolves all string values in a config map. Non-string values are passed through unchanged.

When a string value is exactly a single ${scope.name} reference (no surrounding text), the resolved value preserves the original type from the scope (e.g. float64, int, bool). This allows board variables to inject typed config values such as temperature.

type Schema

type Schema struct {
	Variables []Variable `json:"variables" yaml:"variables"`
}

Schema defines the input/output variable schema for an App or node.

func NewSchema

func NewSchema(vars ...Variable) *Schema

NewSchema creates a new schema from variable definitions.

func (*Schema) ApplyDefaults

func (s *Schema) ApplyDefaults(values map[string]any) map[string]any

ApplyDefaults fills in default values for missing variables. Returns a new map; the original is not modified.

func (*Schema) Get

func (s *Schema) Get(name string) (Variable, bool)

Get returns a variable by name.

func (*Schema) Names

func (s *Schema) Names() []string

Names returns all variable names.

func (*Schema) RequiredNames

func (s *Schema) RequiredNames() []string

RequiredNames returns the names of all required variables.

func (*Schema) ToJSONSchema

func (s *Schema) ToJSONSchema() map[string]any

ToJSONSchema converts the schema to a JSON Schema map (for LLM tool definitions).

func (*Schema) Validate

func (s *Schema) Validate(values map[string]any) error

Validate validates a set of values against the schema. Returns a validation error aggregating all field-level messages. Returns nil if no errors.

type Type

type Type string

Type represents a variable data type.

const (
	TypeString  Type = "string"
	TypeNumber  Type = "number"
	TypeInteger Type = "integer"
	TypeBoolean Type = "boolean"
	TypeArray   Type = "array"
	TypeObject  Type = "object"
	TypeFile    Type = "file"
	TypeAny     Type = "any"
)

type Variable

type Variable struct {
	Name         string     `json:"name" yaml:"name"`
	Type         Type       `json:"type" yaml:"type"`
	Description  string     `json:"description,omitempty" yaml:"description,omitempty"`
	Required     bool       `json:"required,omitempty" yaml:"required,omitempty"`
	DefaultValue any        `json:"default,omitempty" yaml:"default,omitempty"`
	MaxLength    int        `json:"max_length,omitempty" yaml:"max_length,omitempty"`
	Enum         []any      `json:"enum,omitempty" yaml:"enum,omitempty"`
	ItemType     Type       `json:"item_type,omitempty" yaml:"item_type,omitempty"`
	Properties   []Variable `json:"properties,omitempty" yaml:"properties,omitempty"`
}

Variable represents a typed variable with metadata.

func (*Variable) ApplyDefault

func (v *Variable) ApplyDefault(value any) any

ApplyDefault returns the default value if value is nil.

func (*Variable) Validate

func (v *Variable) Validate(value any) error

Validate checks if a value matches the variable's type constraints.

Jump to

Keyboard shortcuts

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