typed

package
v0.0.0-...-51f9457 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package typed contains implementation of strongly-typed lists and dicts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Converter

type Converter interface {
	// Convert takes a value and either returns it as is (if it already has the
	// necessary type) or allocates a new value of necessary type and populates
	// it based on data in 'x'.
	//
	// Returns an error if 'x' can't be converted.
	Convert(x starlark.Value) (starlark.Value, error)

	// Type returns a name of the type the converter converts to.
	//
	// Used only to construct composite type names such as "list<T>".
	Type() string
}

Converter can convert values to a some Starlark type or reject them as incompatible.

Must be idempotent, i.e. the following must not panic for all 'x':

y, err := Convert(x)
if err == nil {
  z, err := Convert(y)
  if err != nil {
    panic("converted to an incompatible type")
  }
  if z != y {
    panic("doesn't pass through already converted item")
  }
}

Must be stateless. Must not mutate the values being converted.

type Dict

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

Dict is a Starlark value that looks like a regular dict, but implements type checks and implicit conversions for keys and values when assigning elements.

Note that keys(), values() and items() methods return regular lists, not typed ones.

Differences from regular dicts:

  • Not comparable by value to regular dicts, only to other typed dicts. go.starlark.net has no way to express that typed dicts should be comparable to regular dicts.
  • update() doesn't support keyword arguments. Using keyword arguments implies string keys which make no sense in generic typed Dict interface, since generally strings are not valid keys. This also substantially simplifies the implementation of update().

func AsTypedDict

func AsTypedDict(k, v Converter, x starlark.Value) (*Dict, error)

AsTypedDict allocates a new dict<k,v>, and copies it from 'x'.

Returns an error if 'x' is not an iterable mapping or some of its (k, v) pairs can't be converted to requested types.

func NewDict

func NewDict(key, val Converter, size int) *Dict

NewDict returns a dict with give preallocated capacity.

func (*Dict) Attr

func (d *Dict) Attr(name string) (starlark.Value, error)

func (*Dict) AttrNames

func (d *Dict) AttrNames() []string

func (*Dict) Clear

func (d *Dict) Clear() error

func (*Dict) CompareSameType

func (d *Dict) CompareSameType(op syntax.Token, y starlark.Value, depth int) (bool, error)

func (*Dict) Delete

func (d *Dict) Delete(k starlark.Value) (starlark.Value, bool, error)

func (*Dict) Freeze

func (d *Dict) Freeze()

func (*Dict) Get

func (d *Dict) Get(k starlark.Value) (v starlark.Value, found bool, err error)

func (*Dict) Hash

func (d *Dict) Hash() (uint32, error)

func (*Dict) Items

func (d *Dict) Items() []starlark.Tuple

func (*Dict) Iterate

func (d *Dict) Iterate() starlark.Iterator

func (*Dict) KeyConverter

func (d *Dict) KeyConverter() Converter

KeyConverter returns the type converter used for keys of this dict.

func (*Dict) Keys

func (d *Dict) Keys() []starlark.Value

func (*Dict) Len

func (d *Dict) Len() int

func (*Dict) SetKey

func (d *Dict) SetKey(k, v starlark.Value) error

func (*Dict) String

func (d *Dict) String() string

func (*Dict) Truth

func (d *Dict) Truth() starlark.Bool

func (*Dict) Type

func (d *Dict) Type() string

func (*Dict) ValueConverter

func (d *Dict) ValueConverter() Converter

ValueConverter returns the type converter used for values of this dict.

type List

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

List is a Starlark value that looks like a regular list, but implements type checks and implicit conversions when assigning elements.

Differences from regular lists:

  • Not comparable by value to regular lists, only to other typed lists. go.starlark.net has no way to express that typed lists should be comparable to regular lists.
  • Only == and != comparison operators are supported.
  • `l += ...` is same as `l = l + ...`, not `l.extend(...)`

func AsTypedList

func AsTypedList(t Converter, x starlark.Value) (*List, error)

AsTypedList allocates a new list<t>, and copies it from 'x'.

Returns an error if 'x' is not an iterable or some of its elements can't be converted to 't'.

func NewList

func NewList(item Converter, elems []starlark.Value) (*List, error)

NewList returns a list with given elements, converted to necessary type through 'item' converter.

All subsequently added elements will be converter through this converter too.

func (*List) Append

func (l *List) Append(v starlark.Value) error

func (*List) Attr

func (l *List) Attr(name string) (starlark.Value, error)

func (*List) AttrNames

func (l *List) AttrNames() []string

func (*List) Binary

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

Binary implements 'l <op> y' (or 'y <op> l', depending on 'side').

Note that *starlark.List has its binary operators implement natively by the Starlark evaluator, not via HasBinary interface.

func (*List) Clear

func (l *List) Clear() error

func (*List) CompareSameType

func (l *List) CompareSameType(op syntax.Token, y starlark.Value, depth int) (bool, error)

func (*List) Converter

func (l *List) Converter() Converter

Converter returns the type converter used by this list.

func (*List) Freeze

func (l *List) Freeze()

func (*List) Hash

func (l *List) Hash() (uint32, error)

func (*List) Index

func (l *List) Index(i int) starlark.Value

func (*List) Iterate

func (l *List) Iterate() starlark.Iterator

func (*List) Len

func (l *List) Len() int

func (*List) SetIndex

func (l *List) SetIndex(i int, v starlark.Value) error

func (*List) Slice

func (l *List) Slice(start, end, step int) starlark.Value

func (*List) String

func (l *List) String() string

func (*List) Truth

func (l *List) Truth() starlark.Bool

func (*List) Type

func (l *List) Type() string

Jump to

Keyboard shortcuts

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