convert

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2026 License: MIT Imports: 13 Imported by: 20

Documentation

Overview

Package convert provides functions for converting data and functions between Go and Starlark.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultPropertyTag is the default struct tag to use when converting
	DefaultPropertyTag = "starlark"
)

Functions

func FromDict

func FromDict(m *starlark.Dict) map[interface{}]interface{}

FromDict converts a starlark.Dict to a map[interface{}]interface{}. Keys are converted with the same rules as TryFromDict; a key with no comparable Go form falls back to its printed form (k.String()) so the entry is preserved instead of panicking. Use TryFromDict to detect such keys instead of silently accepting the fallback.

func FromList

func FromList(l *starlark.List) []interface{}

FromList creates a go slice from the given starlark list. A list that reaches itself converts its cyclic reference to nil.

func FromSet

func FromSet(s *starlark.Set) map[interface{}]bool

FromSet converts a starlark.Set to a map[interface{}]bool. Elements are converted with the same rules as TryFromSet; an element with no comparable Go form falls back to its printed form (v.String()) so the member is preserved instead of panicking. Use TryFromSet to detect such elements instead of silently accepting the fallback.

func FromSetToSlice added in v0.2.0

func FromSetToSlice(s *starlark.Set) []interface{}

FromSetToSlice converts a starlark.Set into a []interface{}, preserving the set's iteration (insertion) order. Use it instead of FromSet when the order of members matters: FromSet returns a Go map, which has no defined iteration order.

func FromStringDict

func FromStringDict(m starlark.StringDict) map[string]interface{}

FromStringDict makes a map[string]interface{} from the given arg. Any inconvertible values are ignored.

func FromTuple

func FromTuple(v starlark.Tuple) []interface{}

FromTuple converts a starlark.Tuple into a []interface{}.

func FromValue

func FromValue(v starlark.Value) interface{}

FromValue converts a starlark value to a go value.

Integer contract: a starlark.Int converts down a fixed ladder — int64 if the value fits, else uint64 if it fits, else *math/big.Int. The Go type of a round-tripped integer therefore depends on its magnitude and is not guaranteed to be identical to what was originally converted in.

func MakeDict

func MakeDict(v interface{}) (starlark.Value, error)

MakeDict makes a Dict from the given map. The acceptable keys and values are the same as ToValue. For nil input, it returns an empty Dict. It panics if the input is not a map.

func MakeDictWithTag added in v0.0.8

func MakeDictWithTag(v interface{}, tagName string) (starlark.Value, error)

MakeDictWithTag makes a Dict from the given map with custom tag. The acceptable keys and values are the same as ToValueWithTag. For nil input, it returns an empty Dict. It panics if the input is not a map.

func MakeList added in v0.0.5

func MakeList(v []interface{}) (*starlark.List, error)

MakeList makes a Starlark List from the given Go slice. The types supported are the same as ToValue. It returns an empty list for nil input.

func MakeSet

func MakeSet(s map[interface{}]bool) (*starlark.Set, error)

MakeSet makes a Set from the given map. The acceptable keys the same as ToValue. For nil input, it returns an empty Set.

func MakeSetFromSlice added in v0.0.5

func MakeSetFromSlice(s []interface{}) (*starlark.Set, error)

MakeSetFromSlice makes a Set from the given slice. The acceptable keys the same as ToValue. For nil input, it returns an empty Set.

func MakeStarFn

func MakeStarFn(name string, gofn interface{}) *starlark.Builtin

MakeStarFn creates a wrapper around the given function that can be called from a starlark script. Argument support is the same as ToValue. If the last value the function returns is an error, it will cause an error to be returned from the starlark function. If there are no other errors, the function will return None. If there's exactly one other value, the function will return the starlark equivalent of that value. If there is more than one return value, they'll be returned as a tuple. MakeStarFn will panic if you pass it something other than a function, like nil or a non-function.

func MakeStringDict

func MakeStringDict(m map[string]interface{}) (starlark.StringDict, error)

MakeStringDict makes a StringDict from the given arg. The types supported are the same as ToValue. It returns an empty dict for nil input.

func MakeStringDictWithTag added in v0.0.8

func MakeStringDictWithTag(m map[string]interface{}, tagName string) (starlark.StringDict, error)

MakeStringDictWithTag makes a StringDict from the given arg with custom tag. The types supported are the same as ToValueWithTag. It returns an empty dict for nil input.

func MakeTuple added in v0.0.5

func MakeTuple(v []interface{}) (starlark.Tuple, error)

MakeTuple makes a Starlark Tuple from the given Go slice. The types supported are the same as ToValue. It returns an empty tuple for nil input.

func ToValue

func ToValue(v interface{}) (starlark.Value, error)

ToValue attempts to convert the given value to a starlark.Value. It supports all int, uint, and float numeric types, plus strings and booleans. It supports structs, maps, slices, and functions that use the aforementioned. Any starlark.Value is passed through as-is.

func ToValueWithTag added in v0.0.8

func ToValueWithTag(v interface{}, tagName string) (starlark.Value, error)

ToValueWithTag attempts to convert the given value to a starlark.Value. It works like ToValue, but also accepts a tag name to use for all nested struct fields.

func TryFromDict added in v0.2.0

func TryFromDict(m *starlark.Dict) (map[interface{}]interface{}, error)

TryFromDict converts a starlark.Dict to a map[interface{}]interface{}. It works like FromDict, but returns an error if any key has no comparable Go form (e.g. a custom value that converts to a non-comparable Go type) instead of falling back to the key's printed form.

func TryFromSet added in v0.2.0

func TryFromSet(s *starlark.Set) (map[interface{}]bool, error)

TryFromSet converts a starlark.Set to a map[interface{}]bool. It works like FromSet, but returns an error if any element has no comparable Go form (e.g. a custom value that converts to a non-comparable Go type) instead of falling back to the element's printed form.

Types

type DoNotCompare added in v0.1.2

type DoNotCompare [0]func()

DoNotCompare is an embedded zero-sized struct used to disallow comparison operations (== and !=) on the containing struct.

type GoInterface

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

GoInterface wraps a go value to expose its methods to starlark scripts. Basic types will not behave as their base type (you can't add 2 to an ID, even if it is an int underneath).

func MakeGoInterface

func MakeGoInterface(v interface{}) *GoInterface

MakeGoInterface converts the given value into a GoInterface. This will panic if the value is nil or the type is not a bool, string, float kind, int kind, or uint kind.

func (*GoInterface) Attr

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

Attr returns a starlark value that wraps the method or field with the given name.

func (*GoInterface) AttrNames

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

AttrNames returns the names Attr resolves for this value: the wrapped value's methods plus the synthetic to* conversion attributes, sorted and without duplicates.

func (*GoInterface) Freeze

func (g *GoInterface) Freeze()

Freeze is a no-op: GoInterface exposes no write path, so there is nothing to freeze. The wrapped Go value itself is not protected — the host can still mutate it.

func (*GoInterface) Hash

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

Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y). Hash may fail if the value's type is not hashable, or if the value contains a non-hashable value.

func (*GoInterface) String

func (g *GoInterface) String() string

String returns the string representation of the value. Cyclic or overly deep values are elided as "<cyclic TYPE>" instead of overflowing the stack; see safeGoString.

func (*GoInterface) ToBool

func (g *GoInterface) ToBool() (bool, error)

ToBool converts the interface value into a starlark bool. This will fail if the underlying type is not a bool type or pointer to a bool type.

func (*GoInterface) ToFloat

func (g *GoInterface) ToFloat() (float64, error)

ToFloat converts the interface value into a starlark float. This will fail if the underlying type is not a float type (including if the underlying type is a pointer to a float).

func (*GoInterface) ToInt

func (g *GoInterface) ToInt() (int64, error)

ToInt converts the interface value into a starlark int. This will fail if the underlying type is not an int type or pointer to an int type.

func (*GoInterface) ToString

func (g *GoInterface) ToString() (string, error)

ToString converts the interface value into a starlark string. This will fail if the underlying type is not a string (including if the underlying type is a pointer to a string).

func (*GoInterface) ToUint

func (g *GoInterface) ToUint() (uint64, error)

ToUint converts the interface value into a starlark int. This will fail if the underlying type is not an uint type or pointer to an uint type.

func (*GoInterface) Truth

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

Truth returns the truth value of an object.

func (*GoInterface) Type

func (g *GoInterface) Type() string

Type returns a short string describing the value's type.

func (*GoInterface) Value added in v0.0.3

func (g *GoInterface) Value() reflect.Value

Value returns reflect.Value of the underlying value.

type GoMap

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

GoMap is a wrapper around a Go map that makes it satisfy starlark's expectations of a starlark dict.

func NewGoMap

func NewGoMap(m interface{}) *GoMap

NewGoMap wraps the given map m in a new GoMap. This function will panic if m is nil or not a map.

func (*GoMap) Attr

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

func (*GoMap) AttrNames

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

func (*GoMap) Clear

func (g *GoMap) Clear() error

func (*GoMap) Delete

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

func (*GoMap) Freeze

func (g *GoMap) Freeze()

Freeze marks this wrapper as frozen: mutations through this GoMap fail afterwards. The freeze is shallow — it does not propagate to the wrapped Go map, which the host (or other wrappers around the same value) can still mutate.

func (*GoMap) Get

func (g *GoMap) Get(in starlark.Value) (out starlark.Value, found bool, err error)

Get implements starlark.Mapping.

func (*GoMap) Hash

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

Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y). Hash may fail if the value's type is not hashable, or if the value contains a non-hashable value.

func (*GoMap) Items

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

func (*GoMap) Iterate

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

func (*GoMap) Keys

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

func (*GoMap) Len

func (g *GoMap) Len() int

func (*GoMap) SetKey

func (g *GoMap) SetKey(k, v starlark.Value) (err error)

SetKey implements starlark.HasSetKey.

func (*GoMap) String

func (g *GoMap) String() string

String returns the string representation of the value. Cyclic or overly deep values are elided as "<cyclic TYPE>" instead of overflowing the stack; see safeGoString.

func (*GoMap) Truth

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

Truth returns the truth value of an object.

func (*GoMap) Type

func (g *GoMap) Type() string

Type returns a short string describing the value's type.

func (*GoMap) Value

func (g *GoMap) Value() reflect.Value

Value returns reflect.Value of the underlying map.

type GoSlice

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

GoSlice is a wrapper around a Go slice or array to adapt it for use with starlark. Arrays are copied into a slice at wrap time: they arrive by value (unaddressable) through interface{}, so in-place mutation could never reach the host's array anyway — mutations affect the copy only.

func NewGoSlice

func NewGoSlice(slice interface{}) *GoSlice

NewGoSlice wraps the given slice or array in a new GoSlice; arrays are copied into a slice (see the GoSlice doc). This function will panic if the argument is not a slice nor an array.

func (*GoSlice) Attr

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

func (*GoSlice) AttrNames

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

func (*GoSlice) Clear

func (g *GoSlice) Clear() error

func (*GoSlice) Freeze

func (g *GoSlice) Freeze()

Freeze marks this wrapper as frozen: mutations through this GoSlice fail afterwards. The freeze is shallow — it does not propagate to the wrapped Go slice, which the host (or other wrappers around the same value) can still mutate.

func (*GoSlice) Hash

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

Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y). Hash may fail if the value's type is not hashable, or if the value contains a non-hashable value.

func (*GoSlice) Index

func (g *GoSlice) Index(i int) starlark.Value

Index implements starlark.Indexable.

func (*GoSlice) Iterate

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

func (*GoSlice) Len

func (g *GoSlice) Len() int

func (*GoSlice) SetIndex

func (g *GoSlice) SetIndex(index int, v starlark.Value) error

func (*GoSlice) Slice

func (g *GoSlice) Slice(start, end, step int) starlark.Value

func (*GoSlice) String

func (g *GoSlice) String() string

String returns the string representation of the value. Cyclic or overly deep values are elided as "<cyclic TYPE>" instead of overflowing the stack; see safeGoString.

func (*GoSlice) Truth

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

Truth returns the truth value of an object.

func (*GoSlice) Type

func (g *GoSlice) Type() string

Type returns a short string describing the value's type.

func (*GoSlice) Value

func (g *GoSlice) Value() reflect.Value

Value returns reflect.Value of the underlying slice.

type GoStruct

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

GoStruct is a wrapper around a Go struct to let it be manipulated by Starlark scripts.

func NewStruct

func NewStruct(strct interface{}) *GoStruct

NewStruct makes a new Starlark-compatible Struct from the given struct or pointer to struct. This will panic if you pass it nil or anything else.

func NewStructWithTag

func NewStructWithTag(strct interface{}, tagName string) *GoStruct

NewStructWithTag makes a new Starlark-compatible Struct from the given struct or pointer to struct, using the given struct tag to determine which fields to expose. This will panic if you pass it anything else.

func (*GoStruct) Attr

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

Attr returns a Starlark value that wraps the method or field with the given name.

func (*GoStruct) AttrNames

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

AttrNames returns the list of all fields and methods on this struct.

func (*GoStruct) Freeze

func (g *GoStruct) Freeze()

Freeze marks this wrapper as frozen: writes through this GoStruct (attribute or index assignment) fail afterwards. The freeze is shallow — it does not propagate to the wrapped Go struct, which the host (or other wrappers around the same value) can still mutate.

func (*GoStruct) Get added in v0.1.1

func (g *GoStruct) Get(in starlark.Value) (v starlark.Value, found bool, err error)

Get returns the value of the field with the given name, allowing the struct to be accessed like a dictionary.

func (*GoStruct) Hash

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

Hash returns a function of x such that Equals(x, y) => Hash(x) == Hash(y). Hash may fail if the value's type is not hashable, or if the value contains a non-hashable value.

func (*GoStruct) SetField

func (g *GoStruct) SetField(name string, val starlark.Value) error

SetField sets the struct field with the given name with the given value.

func (*GoStruct) SetKey added in v0.1.1

func (g *GoStruct) SetKey(k, v starlark.Value) error

SetKey implements the starlark.HasSetKey interface, allowing the struct to be used like a dictionary.

func (*GoStruct) String

func (g *GoStruct) String() string

String returns the string representation of the value. Cyclic or overly deep values are elided as "<cyclic TYPE>" instead of overflowing the stack; see safeGoString.

func (*GoStruct) Truth

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

Truth returns the truth value of an object.

func (*GoStruct) Type

func (g *GoStruct) Type() string

Type returns a short string describing the value's type.

func (*GoStruct) Value

func (g *GoStruct) Value() reflect.Value

Value returns reflect.Value of the underlying struct.

type Kwarg

type Kwarg struct {
	Name  string
	Value interface{}
}

Kwarg is a single instance of a python foo=bar style named argument.

func FromKwargs

func FromKwargs(kwargs []starlark.Tuple) ([]Kwarg, error)

FromKwargs converts a Python style name=val, name2=val2 list of tuples into a []Kwarg. It is an error if any tuple is not exactly 2 values, or if the first one is not a string.

type PanicError added in v0.2.0

type PanicError struct {
	Value interface{} // the recovered panic value
	Stack []byte      // the goroutine stack captured at recovery
}

PanicError reports a panic recovered at the conversion boundary. The conversion entry points are not supposed to panic; if this error surfaces, it indicates a bug in starlight (or a misbehaving custom type), and the captured stack identifies where the panic started.

func (*PanicError) Error added in v0.2.0

func (e *PanicError) Error() string

Error implements the error interface. The message keeps the historic "panic recovered" prefix and appends the captured stack.

Jump to

Keyboard shortcuts

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