env

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2024 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package env provides an environment for managing variable frames in an interpreter context, using a stack-based approach.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Env

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

Env represents an environment with a stack of variable frames. It allows pushing and popping frames and querying variables.

func NewEnv

func NewEnv(options ...EnvOption) *Env

NewEnv creates a new environment for managing variable frames. It accepts optional EnvOptions to configure the initial state.

func (*Env) GetCurrentFrame

func (e *Env) GetCurrentFrame() *Frame

GetCurrentFrame returns the current top frame from the stack. Returns nil if the stack is empty.

func (*Env) GetVar

func (e *Env) GetVar(name string) (interface{}, bool)

GetVar tries to retrieve a variable's value by its name from the current frame. Returns the value and a boolean indicating if the variable was found. If the stack is empty, it returns nil and false.

func (*Env) LookupAll

func (e *Env) LookupAll(expression string, allowMissingKeys bool) ([]interface{}, error)

LookupAll performs a jsonpath query on the variables of the current frame. It returns all matches as a slice of interface{} and an error if the query fails or if the current frame is nil. The function requires a valid jsonpath expression and uses the Kubernetes jsonpath package.

func (*Env) LookupFirst

func (e *Env) LookupFirst(expression string) (interface{}, error)

LookupFirst performs a jsonpath query on the variables of the current frame. It returns the first match as an interface{} and an error if the query fails or if the current frame is nil, or if no matching node is found.

func (*Env) Pop

func (e *Env) Pop()

Pop removes the top frame from the stack. It does nothing if the stack is empty.

func (*Env) Push

func (e *Env) Push(newVars map[string]interface{})

Push creates a new frame on top of the stack with newVars. It performs a shallow copy of the variables from the current top frame and merges them with newVars. If the stack is empty, newVars becomes the first frame.

func (*Env) With

func (e *Env) With(newVars map[string]interface{}, f func() error) error

With creates a new frame on top of the stack with newVars, executes f, and removes the frame from the stack.

type EnvOption

type EnvOption func(*Env)

func WithVars

func WithVars(vars map[string]interface{}) EnvOption

WithVars is an option for NewEnv to initialize the environment with a set of variables. If used when the stack is empty, it creates a new Frame with these variables. Otherwise, it adds the variables to the current frame.

type Frame

type Frame struct {
	Variables map[string]interface{}
}

Frame represents a single variable frame containing a map of variables.

func NewFrame

func NewFrame(parent *Frame, newVars map[string]interface{}) *Frame

NewFrame creates a new Frame with variables. It takes an optional parent frame and merges its variables (shallow copy) with newVars. If parent is nil, only newVars are used. It's primarily used internally by Env.

Jump to

Keyboard shortcuts

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