luci: go.chromium.org/luci/starlark/typed Index | Files

package typed

import "go.chromium.org/luci/starlark/typed"

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

Index

Package Files

converter.go dict.go doc.go list.go

type Converter Uses

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 Uses

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 Uses

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 Uses

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

NewDict returns a dict with give preallocated capacity.

func (*Dict) Attr Uses

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

func (*Dict) AttrNames Uses

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

func (*Dict) Clear Uses

func (d *Dict) Clear() error

func (*Dict) CompareSameType Uses

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

func (*Dict) Delete Uses

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

func (*Dict) Freeze Uses

func (d *Dict) Freeze()

func (*Dict) Get Uses

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

func (*Dict) Hash Uses

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

func (*Dict) Items Uses

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

func (*Dict) Iterate Uses

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

func (*Dict) KeyConverter Uses

func (d *Dict) KeyConverter() Converter

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

func (*Dict) Keys Uses

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

func (*Dict) Len Uses

func (d *Dict) Len() int

func (*Dict) SetKey Uses

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

func (*Dict) String Uses

func (d *Dict) String() string

func (*Dict) Truth Uses

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

func (*Dict) Type Uses

func (d *Dict) Type() string

func (*Dict) ValueConverter Uses

func (d *Dict) ValueConverter() Converter

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

type List Uses

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 Uses

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 Uses

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 Uses

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

func (*List) Attr Uses

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

func (*List) AttrNames Uses

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

func (*List) Binary Uses

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 Uses

func (l *List) Clear() error

func (*List) CompareSameType Uses

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

func (*List) Converter Uses

func (l *List) Converter() Converter

Converter returns the type converter used by this list.

func (*List) Freeze Uses

func (l *List) Freeze()

func (*List) Hash Uses

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

func (*List) Index Uses

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

func (*List) Iterate Uses

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

func (*List) Len Uses

func (l *List) Len() int

func (*List) SetIndex Uses

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

func (*List) Slice Uses

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

func (*List) String Uses

func (l *List) String() string

func (*List) Truth Uses

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

func (*List) Type Uses

func (l *List) Type() string

Package typed imports 3 packages (graph) and is imported by 1 packages. Updated 2019-10-16. Refresh now. Tools for package owners.