convert

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 10 Imported by: 4

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{}

func FromList

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

FromList creates a go slice from the given starlark list.

func FromSet

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

FromSet converts a starlark.Set to a map[interface{}]bool

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

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.

Types

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 list of all fields and methods on this struct.

func (*GoInterface) Freeze

func (g *GoInterface) Freeze()

Freeze causes the value, and all values transitively reachable from it through collections and closures, to be marked as frozen. All subsequent mutations to the data structure through this API will fail dynamically, making the data structure immutable and safe for publishing to other Starlark interpreters running concurrently.

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. Starlark string values are quoted as if by Python's repr.

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 causes the value, and all values transitively reachable from it through collections and closures, to be marked as frozen. All subsequent mutations to the data structure through this API will fail dynamically, making the data structure immutable and safe for publishing to other Starlark interpreters running concurrently.

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. Starlark string values are quoted as if by Python's repr.

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 to adapt it for use with starlark.

func NewGoSlice

func NewGoSlice(slice interface{}) *GoSlice

NewGoSlice wraps the given slice in a new GoSlice. This function will panic if m is nil or not a slice nor 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 causes the value, and all values transitively reachable from it through collections and closures, to be marked as frozen. All subsequent mutations to the data structure through this API will fail dynamically, making the data structure immutable and safe for publishing to other Starlark interpreters running concurrently.

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. Starlark string values are quoted as if by Python's repr.

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 causes the value, and all values transitively reachable from it through collections and closures, to be marked as frozen. All subsequent mutations to the data structure through this API will fail dynamically, making the data structure immutable and safe for publishing to other Starlark interpreters running concurrently.

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

func (g *GoStruct) String() string

String returns the string representation of the value. Starlark string values are quoted as if by Python's repr.

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.

Jump to

Keyboard shortcuts

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