lib

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2025 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Overview

Package lib Package starlarkstruct defines the Starlark types 'struct' and 'module', both optional language extensions.

Index

Constants

View Source
const CacheLineSize = int(unsafe.Sizeof(cpu.CacheLinePad{}))
View Source
const Default = starlark.String("record")

Default is the default constructor for structs. It is merely the string "record".

View Source
const ModelValueType = "model_value"
View Source
const SymmetricValueType = "symmetric_value"

Variables

View Source
var (
	SymmetryPrefix = RandString(9) + "-" // Replace this with a UUID

	Builtins = starlark.StringDict{
		"struct":           starlark.NewBuiltin("struct", starlarkstruct.Make),
		"record":           starlark.NewBuiltin("record", Make),
		"enum":             starlark.NewBuiltin("enum", MakeEnum),
		"genericmap":       starlark.NewBuiltin("genericmap", MakeGenericMap),
		"genericset":       starlark.NewBuiltin("genericset", MakeGenericSet),
		"symmetric_values": starlark.NewBuiltin("symmetric_values", MakeSymmetricValues),
		"bag":              starlark.NewBuiltin("bag", MakeBag),
		"math":             math.Module,
		"itertools":        ItertoolsModule,
	}

	StarlarkPtrTypes = []starlark.Value{
		&starlark.Set{},
		&starlark.List{},
		&starlark.Dict{},
		&starlarkstruct.Struct{},
		&starlark.Tuple{},
		&Struct{},
		&Bag{},
		&GenericSet{},
		&GenericMap{},
		&Role{},
	}
)
View Source
var ItertoolsModule = &starlarkstruct.Module{
	Name: "itertools",
	Members: starlark.StringDict{
		"chain":                         starlark.NewBuiltin("chain", itertoolsChain),
		"combinations":                  starlark.NewBuiltin("combinations", itertoolsCombinations),
		"permutations":                  starlark.NewBuiltin("permutations", itertoolsPermutations),
		"product":                       starlark.NewBuiltin("product", itertoolsProduct),
		"combinations_with_replacement": starlark.NewBuiltin("combinations_with_replacement", itertoolsCombinationsWithReplacement),
	},
}

Functions

func AddSelfParamBuiltin

func AddSelfParamBuiltin(role *Role, val *starlark.Function) func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func BuiltinAttr

func BuiltinAttr(recv starlark.Value, name string, methods map[string]*starlark.Builtin) (starlark.Value, error)

func BuiltinAttrNames

func BuiltinAttrNames(methods map[string]*starlark.Builtin) []string

func ClearChannelRefs

func ClearChannelRefs()

func ClearRoleRefs

func ClearRoleRefs()

func CompareStringer

func CompareStringer[E fmt.Stringer](a, b E) int

func CreateChannelBuiltin

func CreateChannelBuiltin(channels map[int]*Channel) *starlark.Builtin

func CreateResolveRoleBuiltIn added in v0.2.0

func CreateResolveRoleBuiltIn(roles *[]*Role) *starlark.Builtin

CreateResolveRoleBuiltIn returns a builtin resolve_role, when called with a role's __id__ (GetId()), returns the Role object in the roles array with the given id.

func CreateRoleBuiltin

func CreateRoleBuiltin(astRole *ast.Role, symmetric bool, roles *[]*Role) *starlark.Builtin

func GenerateAllPermutations

func GenerateAllPermutations[T any](arr [][]T) [][][]T

GenerateAllPermutations generates all permutations of each subarray and all combinations of these permutations.

func GeneratePermutations

func GeneratePermutations[T any](arr []T) [][]T

GeneratePermutations is a helper function that initializes the result and calls the permute function.

func GenerateRefString

func GenerateRefString(name string, ref int) string

func JsonToYaml

func JsonToYaml(jsonData []byte) ([]byte, error)

JsonToYaml converts JSON data to YAML data

func Make

func Make(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

Make is the implementation of a built-in function that instantiates an immutable struct from the specified keyword arguments.

An application can add 'struct' to the Starlark environment like so:

globals := starlark.StringDict{
	"struct":  starlark.NewBuiltin("struct", starlarkstruct.Make),
}

func MakeBag

func MakeBag(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (
	starlark.Value, error)

func MakeEnum

func MakeEnum(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func MakeGenericMap

func MakeGenericMap(thread *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (
	starlark.Value, error)

func MakeGenericSet

func MakeGenericSet(thread *starlark.Thread, b *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (
	starlark.Value, error)

func MakeSymmetricValues

func MakeSymmetricValues(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func MarshalJSON

func MarshalJSON(obj interface{}) ([]byte, error)

func MarshalJSONStarlarkValue

func MarshalJSONStarlarkValue(m starlark.Value, depth int) ([]byte, error)

func RandString

func RandString(n int) string

func ReadProtoFromBytes

func ReadProtoFromBytes(yamlBytes []byte, protomsg proto.Message) error

func ReadProtoFromFile

func ReadProtoFromFile(filename string, protomsg proto.Message) error

func YamlToJson

func YamlToJson(yamlData []byte) ([]byte, error)

YamlToJson converts YAML data to JSON data

Types

type Bag

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

func NewBag

func NewBag(elems []starlark.Value) *Bag

func (*Bag) Attr

func (b *Bag) Attr(name string) (starlark.Value, error)

func (*Bag) AttrNames

func (b *Bag) AttrNames() []string

func (*Bag) Clear

func (b *Bag) Clear() error

func (*Bag) CompareSameType

func (b *Bag) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (*Bag) Delete

func (b *Bag) Delete(k starlark.Value) (bool, error)

func (*Bag) Find

func (b *Bag) Find(k starlark.Value) (int, error)

func (*Bag) Freeze

func (b *Bag) Freeze()

func (*Bag) Get

func (b *Bag) Get(value starlark.Value) (v starlark.Value, found bool, err error)

func (*Bag) Has

func (b *Bag) Has(x starlark.Value) (found bool, err error)

func (*Bag) Hash

func (b *Bag) Hash() (uint32, error)

func (*Bag) Insert

func (b *Bag) Insert(val starlark.Value) error

func (*Bag) InsertAll

func (b *Bag) InsertAll(v starlark.Iterable) error

func (*Bag) Iterate

func (b *Bag) Iterate() starlark.Iterator

func (*Bag) Len

func (b *Bag) Len() int

func (*Bag) String

func (b *Bag) String() string

func (*Bag) Truth

func (b *Bag) Truth() starlark.Bool

func (*Bag) Type

func (b *Bag) Type() string

type Channel

type Channel struct {
	Id   int    `json:"id"`
	Name string `json:"name,omitempty"`
	// contains filtered or unexported fields
}

Channel represents a custom Starlark object

func (*Channel) Attr

func (c *Channel) Attr(name string) (starlark.Value, error)

Attr gets an attribute (ordering, delivery, blocking)

func (*Channel) AttrNames

func (c *Channel) AttrNames() []string

AttrNames returns the list of available attributes

func (*Channel) Freeze

func (c *Channel) Freeze()

Freeze is required for immutability in Starlark

func (*Channel) Hash

func (c *Channel) Hash() (uint32, error)

Hash allows Channel to be used as a dictionary key

func (*Channel) MayDropMessages

func (c *Channel) MayDropMessages() bool

func (*Channel) RefStringShort

func (c *Channel) RefStringShort() string

RefStringShort returns a string representation of the Channel object

func (*Channel) String

func (c *Channel) String() string

String returns a string representation of the Channel object

func (*Channel) Truth

func (c *Channel) Truth() starlark.Bool

Truth always returns true (non-null)

func (*Channel) Type

func (c *Channel) Type() string

Type returns the type name

type Counter

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

func NewCounter

func NewCounter() *Counter

func (*Counter) Get

func (c *Counter) Get(key string) int

func (*Counter) Inc

func (c *Counter) Inc(key string)

func (*Counter) List

func (c *Counter) List() map[string]int

type GenericMap

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

func NewGenericMap

func NewGenericMap() *GenericMap

func (*GenericMap) Attr

func (g *GenericMap) Attr(name string) (starlark.Value, error)

func (*GenericMap) AttrNames

func (g *GenericMap) AttrNames() []string

func (*GenericMap) Clear

func (g *GenericMap) Clear() error

func (*GenericMap) CompareSameType

func (g *GenericMap) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (*GenericMap) Delete

func (g *GenericMap) Delete(k starlark.Value) (v starlark.Value, found bool, err error)

func (*GenericMap) Freeze

func (g *GenericMap) Freeze()

func (*GenericMap) Get

func (g *GenericMap) Get(key starlark.Value) (v starlark.Value, found bool, err error)

func (*GenericMap) Hash

func (g *GenericMap) Hash() (uint32, error)

func (*GenericMap) Items

func (g *GenericMap) Items() []starlark.Tuple

func (*GenericMap) Iterate

func (g *GenericMap) Iterate() starlark.Iterator

func (*GenericMap) Keys

func (g *GenericMap) Keys() []starlark.Value

func (*GenericMap) Len

func (g *GenericMap) Len() int

func (*GenericMap) SetKey

func (g *GenericMap) SetKey(key, value starlark.Value) error

func (*GenericMap) String

func (g *GenericMap) String() string

func (*GenericMap) Truth

func (g *GenericMap) Truth() starlark.Bool

func (*GenericMap) Type

func (g *GenericMap) Type() string

type GenericSet

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

func NewGenericSet

func NewGenericSet() *GenericSet

func (*GenericSet) Attr

func (g *GenericSet) Attr(name string) (starlark.Value, error)

func (*GenericSet) AttrNames

func (g *GenericSet) AttrNames() []string

func (*GenericSet) Clear

func (g *GenericSet) Clear() error

func (*GenericSet) CompareSameType

func (g *GenericSet) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (*GenericSet) Delete

func (g *GenericSet) Delete(k starlark.Value) (found bool, err error)

func (*GenericSet) Difference

func (g *GenericSet) Difference(other starlark.Iterator) (starlark.Value, error)

func (*GenericSet) Freeze

func (g *GenericSet) Freeze()

func (*GenericSet) Get

func (g *GenericSet) Get(value starlark.Value) (v starlark.Value, found bool, err error)

func (*GenericSet) Has

func (g *GenericSet) Has(k starlark.Value) (found bool, err error)

func (*GenericSet) Hash

func (g *GenericSet) Hash() (uint32, error)

func (*GenericSet) Insert

func (g *GenericSet) Insert(k starlark.Value) error

func (*GenericSet) Intersection

func (s *GenericSet) Intersection(other starlark.Iterator) (starlark.Value, error)

func (*GenericSet) IsSubset

func (g *GenericSet) IsSubset(other *GenericSet) (bool, error)

func (*GenericSet) IsSuperset

func (s *GenericSet) IsSuperset(other starlark.Iterator) (bool, error)

func (*GenericSet) Iterate

func (g *GenericSet) Iterate() starlark.Iterator

func (*GenericSet) Len

func (g *GenericSet) Len() int

func (*GenericSet) String

func (g *GenericSet) String() string

func (*GenericSet) SymmetricDifference

func (s *GenericSet) SymmetricDifference(other starlark.Iterator) (starlark.Value, error)

func (*GenericSet) Truth

func (g *GenericSet) Truth() starlark.Bool

func (*GenericSet) Type

func (g *GenericSet) Type() string

func (*GenericSet) Union

func (g *GenericSet) Union(iter starlark.Iterator) (starlark.Value, error)

type LinearCollection

type LinearCollection[T any] interface {
	Len() int
	Empty() bool
	Add(T)
	Remove() (T, bool)
	Clear(n int)
	ClearAll()
	Retain(n int)
}

type ModelValue

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

func NewModelValue

func NewModelValue(prefix string, i int) ModelValue

func (ModelValue) CompareSameType

func (m ModelValue) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (ModelValue) Freeze

func (m ModelValue) Freeze()

func (ModelValue) FullString

func (m ModelValue) FullString() string

func (ModelValue) GetId

func (m ModelValue) GetId() int

func (ModelValue) GetPrefix

func (m ModelValue) GetPrefix() string

func (ModelValue) Hash

func (m ModelValue) Hash() (uint32, error)

func (ModelValue) MarshalJSON

func (m ModelValue) MarshalJSON() ([]byte, error)

func (ModelValue) ShortString

func (m ModelValue) ShortString() string

func (ModelValue) String

func (m ModelValue) String() string

func (ModelValue) Truth

func (m ModelValue) Truth() starlark.Bool

func (ModelValue) Type

func (m ModelValue) Type() string

type Pair added in v0.2.0

type Pair[T1 any, T2 any] struct {
	First  T1
	Second T2
}

func NewPair added in v0.2.0

func NewPair[T1 any, T2 any](first T1, second T2) Pair[T1, T2]

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

func NewQueue

func NewQueue[T any]() *Queue[T]

func (*Queue[T]) Add

func (q *Queue[T]) Add(t T)

func (*Queue[T]) Clear

func (q *Queue[T]) Clear(int)

func (*Queue[T]) ClearAll

func (q *Queue[T]) ClearAll()

func (*Queue[T]) Count

func (q *Queue[T]) Count() int

func (*Queue[T]) Dequeue

func (q *Queue[T]) Dequeue() (T, bool)

func (*Queue[T]) Empty

func (q *Queue[T]) Empty() bool

func (*Queue[T]) Enqueue

func (q *Queue[T]) Enqueue(v T) *Queue[T]

func (*Queue[T]) Len

func (q *Queue[T]) Len() int

func (*Queue[T]) Remove

func (q *Queue[T]) Remove() (T, bool)

func (*Queue[T]) Retain

func (q *Queue[T]) Retain(n int)

type RandomQueue

type RandomQueue[T any] struct {
	// contains filtered or unexported fields
}

func NewRandomQueue

func NewRandomQueue[T any](random rand.Rand) *RandomQueue[T]

func (*RandomQueue[T]) Add

func (r *RandomQueue[T]) Add(t T)

func (*RandomQueue[T]) Clear

func (r *RandomQueue[T]) Clear(n int)

func (*RandomQueue[T]) ClearAll

func (r *RandomQueue[T]) ClearAll()

func (*RandomQueue[T]) Empty

func (r *RandomQueue[T]) Empty() bool

func (*RandomQueue[T]) Len

func (r *RandomQueue[T]) Len() int

func (*RandomQueue[T]) Remove

func (r *RandomQueue[T]) Remove() (T, bool)

func (*RandomQueue[T]) Retain

func (r *RandomQueue[T]) Retain(n int)

type Role

type Role struct {
	Ref         int
	Name        string
	Symmetric   bool
	Params      *Struct
	Fields      *Struct
	Methods     map[string]*starlark.Function
	RoleMethods map[string]*starlark.Builtin
	InitValues  *Struct
}

func (*Role) AddMethod

func (r *Role) AddMethod(name string, val starlark.Value) error

func (*Role) Attr

func (r *Role) Attr(name string) (starlark.Value, error)

func (*Role) AttrNames

func (r *Role) AttrNames() []string

func (*Role) Freeze

func (r *Role) Freeze()

func (*Role) GetId

func (r *Role) GetId() starlark.Value

func (*Role) Hash

func (r *Role) Hash() (uint32, error)

func (*Role) IsSymmetric

func (r *Role) IsSymmetric() bool

func (*Role) MarshalJSON

func (r *Role) MarshalJSON() ([]byte, error)

func (*Role) RefString

func (r *Role) RefString() string

func (*Role) RefStringShort

func (r *Role) RefStringShort() string

func (*Role) SetField

func (r *Role) SetField(name string, val starlark.Value) error

func (*Role) String

func (r *Role) String() string

func (*Role) Truth

func (r *Role) Truth() starlark.Bool

func (*Role) Type

func (r *Role) Type() string

type RoleStub

type RoleStub struct {
	Role    *Role
	Channel *Channel
}

RoleStub wraps an existing Role and associates it with a Channel

func NewRoleStub

func NewRoleStub(role *Role, c *Channel) *RoleStub

func (*RoleStub) Attr

func (rs *RoleStub) Attr(name string) (starlark.Value, error)

Attr allows retrieving attributes and methods of RoleStub

func (*RoleStub) AttrNames

func (rs *RoleStub) AttrNames() []string

AttrNames lists available attributes

func (*RoleStub) Freeze

func (rs *RoleStub) Freeze()

func (*RoleStub) Hash

func (rs *RoleStub) Hash() (uint32, error)

func (*RoleStub) String

func (rs *RoleStub) String() string

String representation of RoleStub

func (*RoleStub) Truth

func (rs *RoleStub) Truth() starlark.Bool

func (*RoleStub) Type

func (rs *RoleStub) Type() string

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

func NewStack

func NewStack[T any]() *Stack[T]

func (*Stack[T]) Add

func (s *Stack[T]) Add(t T)

func (*Stack[T]) Clear

func (s *Stack[T]) Clear(n int)

func (*Stack[T]) ClearAll

func (s *Stack[T]) ClearAll()

func (*Stack[T]) Clone

func (s *Stack[T]) Clone() *Stack[T]

func (*Stack[T]) Empty

func (s *Stack[T]) Empty() bool

func (*Stack[T]) Head

func (s *Stack[T]) Head() (T, bool)

func (*Stack[T]) Len

func (s *Stack[T]) Len() int

func (*Stack[T]) MarshalJSON

func (s *Stack[T]) MarshalJSON() ([]byte, error)

func (*Stack[T]) Peek

func (s *Stack[T]) Peek() (T, bool)

func (*Stack[T]) Pop

func (s *Stack[T]) Pop() (T, bool)

func (*Stack[T]) Push

func (s *Stack[T]) Push(v T) *Stack[T]

func (*Stack[T]) RawArray

func (s *Stack[T]) RawArray() []T

func (*Stack[T]) RawArrayCopy

func (s *Stack[T]) RawArrayCopy() []T

func (*Stack[T]) Remove

func (s *Stack[T]) Remove() (T, bool)

func (*Stack[T]) Retain

func (s *Stack[T]) Retain(n int)

type Struct

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

Struct is an immutable Starlark type that maps field names to values. It is not iterable and does not support len.

A struct has a constructor, a distinct value that identifies a class of structs, and which appears in the struct's string representation.

Operations such as x+y fail if the constructors of the two operands are not equal.

The default constructor, Default, is the string "struct", but clients may wish to 'brand' structs for their own purposes. The constructor value appears in the printed form of the value, and is accessible using the Constructor method.

Use Attr to access its fields and AttrNames to enumerate them.

func FromKeywords

func FromKeywords(constructor starlark.Value, kwargs []starlark.Tuple) *Struct

FromKeywords returns a new struct instance whose fields are specified by the key/value pairs in kwargs. (Each kwargs[i][0] must be a starlark.String.)

func FromStringDict

func FromStringDict(constructor starlark.Value, d starlark.StringDict) *Struct

FromStringDict returns a new struct instance whose elements are those of d. The constructor parameter specifies the constructor; use Default for an ordinary struct.

func (*Struct) Attr

func (s *Struct) Attr(name string) (starlark.Value, error)

Attr returns the value of the specified field.

func (*Struct) AttrNames

func (s *Struct) AttrNames() []string

AttrNames returns a new sorted list of the struct fields.

func (*Struct) Binary

func (x *Struct) Binary(op syntax.Token, y starlark.Value, side starlark.Side) (starlark.Value, error)

func (*Struct) CompareSameType

func (x *Struct) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (*Struct) Constructor

func (s *Struct) Constructor() starlark.Value

Constructor returns the constructor used to create this struct.

func (*Struct) Freeze

func (s *Struct) Freeze()

func (*Struct) Hash

func (s *Struct) Hash() (uint32, error)

func (*Struct) MarshalJSON

func (s *Struct) MarshalJSON() ([]byte, error)

func (*Struct) ReplaceEntriesFromStringDict

func (s *Struct) ReplaceEntriesFromStringDict(d starlark.StringDict)

func (*Struct) SetField

func (s *Struct) SetField(name string, val starlark.Value) error

func (*Struct) String

func (s *Struct) String() string

func (*Struct) ToStringDict

func (s *Struct) ToStringDict(d starlark.StringDict)

ToStringDict adds a name/value entry to d for each field of the struct.

func (*Struct) Truth

func (s *Struct) Truth() starlark.Bool

func (*Struct) Type

func (s *Struct) Type() string

type SymmetricValue

type SymmetricValue struct {
	ModelValue
}

func NewSymmetricValue

func NewSymmetricValue(prefix string, i int) SymmetricValue

func (SymmetricValue) CompareSameType

func (s SymmetricValue) CompareSameType(op syntax.Token, y_ starlark.Value, depth int) (bool, error)

func (SymmetricValue) Type

func (s SymmetricValue) Type() string

type SymmetricValues

type SymmetricValues struct {
	starlark.Tuple
}

func NewSymmetricValues

func NewSymmetricValues(values []SymmetricValue) *SymmetricValues

func (SymmetricValues) Index

func (s SymmetricValues) Index(i int) SymmetricValue

func (SymmetricValues) Type

func (s SymmetricValues) Type() string

type TriState

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

func (TriState) IsSet

func (t TriState) IsSet() bool

func (TriState) IsSetToFalse

func (t TriState) IsSetToFalse() bool

func (TriState) IsSetToTrue

func (t TriState) IsSetToTrue() bool

func (TriState) Set

func (t TriState) Set(value bool)

func (TriState) Unset

func (t TriState) Unset()

func (TriState) Value

func (t TriState) Value(defaultIfNotSet bool) bool

Jump to

Keyboard shortcuts

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