starlarkstruct

package
v0.0.0-...-9b43f0a Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: BSD-3-Clause Imports: 5 Imported by: 215

Documentation

Overview

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

Index

Constants

View Source
const Default = starlark.String("struct")

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

Variables

This section is empty.

Functions

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 MakeModule

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

MakeModule may be used as the implementation of a Starlark built-in function, module(name, **kwargs). It returns a new module with the specified name and members.

Types

type Module

type Module struct {
	Name    string
	Members starlark.StringDict
}

A Module is a named collection of values, typically a suite of functions imported by a load statement.

It differs from Struct primarily in that its string representation does not enumerate its fields.

func (*Module) Attr

func (m *Module) Attr(name string) (starlark.Value, error)

func (*Module) AttrNames

func (m *Module) AttrNames() []string

func (*Module) Freeze

func (m *Module) Freeze()

func (*Module) Hash

func (m *Module) Hash() (uint32, error)

func (*Module) String

func (m *Module) String() string

func (*Module) Truth

func (m *Module) Truth() starlark.Bool

func (*Module) Type

func (m *Module) Type() string

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) 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

Jump to

Keyboard shortcuts

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