dataconv

package
v0.0.13 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 16 Imported by: 22

Documentation

Overview

Package dataconv provides helper functions to convert between Starlark and Go types.

It works like package starlight, but only supports common Starlark and Go types, and won't wrap any custom types or functions.

For data type conversion, it provides functions to convert between Starlark and Go types:

+---------+   Marshal   +------------+   MarshalStarlarkJSON   +----------+
|         | ----------> |            | ----------------------> |          |
|  Go     |             |  Starlark  |                         | JSON     |
|  Value  | <---------- |  Value     | <---------------------- | String   |
|         |  Unmarshal  |            |   UnmarshalStarlarkJSON |          |
+---------+             +------------+                         +----------+

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertJSONStruct added in v0.0.7

func ConvertJSONStruct(v interface{}) starlark.Value

ConvertJSONStruct converts a struct to a Starlark wrapper with JSON tag.

func ConvertStruct added in v0.0.7

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

ConvertStruct converts a struct to a Starlark wrapper.

func IsEmptyString

func IsEmptyString(s starlark.String) bool

IsEmptyString checks is a starlark string is empty ("" for a go string) starlark.String.String performs repr-style quotation, which is necessary for the starlark.Value contract but a frequent source of errors in API clients. This helper method makes sure it'll work properly

func IsInterfaceNil

func IsInterfaceNil(i interface{}) bool

IsInterfaceNil returns true if the given interface is nil.

func MakeModule added in v0.0.12

func MakeModule(name string, data starlark.StringDict) *starlarkstruct.Module

MakeModule creates a Starlark module from the given name and data.

func MakeStruct added in v0.0.12

func MakeStruct(name string, data starlark.StringDict) *starlarkstruct.Struct

MakeStruct creates a Starlark struct from the given name and data.

func Marshal

func Marshal(data interface{}) (v starlark.Value, err error)

Marshal converts Go values into Starlark types, like ToValue() of package starlight does. It only supports common Go types, won't wrap any custom types like Starlight does.

func MarshalStarlarkJSON

func MarshalStarlarkJSON(data starlark.Value, indent int) (string, error)

MarshalStarlarkJSON marshals a starlark.Value into a JSON string. It first converts the starlark.Value into a Golang value, then marshals it into JSON.

func StarString added in v0.0.9

func StarString(x starlark.Value) string

StarString returns the string representation of a starlark.Value.

func TypeConvert added in v0.0.8

func TypeConvert(data interface{}) interface{}

TypeConvert converts JSON decoded values to their appropriate types. Usually it's used after JSON Unmarshal, Starlark Unmarshal, or similar.

func Unmarshal

func Unmarshal(x starlark.Value) (val interface{}, err error)

Unmarshal converts a starlark.Value into it's Golang counterpart, like FromValue() of package starlight does. It's the opposite of Marshal().

func UnmarshalStarlarkJSON added in v0.0.11

func UnmarshalStarlarkJSON(data []byte) (starlark.Value, error)

UnmarshalStarlarkJSON unmarshals a JSON bytes into a starlark.Value. It first unmarshals the JSON string into a Gol value, then converts it into a starlark.Value.

func WrapModuleData added in v0.0.7

func WrapModuleData(name string, data starlark.StringDict) func() (starlark.StringDict, error)

WrapModuleData wraps data from the given starlark.StringDict into a Starlark module loader, which can be used to load the module into a Starlark interpreter and accessed via `load("name", "data_key")`.

func WrapStructData added in v0.0.11

func WrapStructData(name string, data starlark.StringDict) func() (starlark.StringDict, error)

WrapStructData wraps data from the given starlark.StringDict into a Starlark struct loader, which can be used to load the struct into a Starlark interpreter and accessed via `load("name", "data_key")`.

Types

type Marshaler

type Marshaler interface {
	// MarshalStarlark marshal a custom type to Starlark object.
	MarshalStarlark() (starlark.Value, error)
}

Marshaler is the interface use to marshal Starlark from custom types, i.e. Go to Starlark.

type SharedDict added in v0.0.8

type SharedDict struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

SharedDict is a dictionary that can be shared among multiple Starlark threads.

func NewNamedSharedDict added in v0.0.8

func NewNamedSharedDict(name string) *SharedDict

NewNamedSharedDict creates a new SharedDict instance with the given name.

func NewSharedDict added in v0.0.8

func NewSharedDict() *SharedDict

NewSharedDict creates a new SharedDict instance.

func NewSharedDictFromDict added in v0.0.10

func NewSharedDictFromDict(d *starlark.Dict) *SharedDict

NewSharedDictFromDict creates a new SharedDict instance from the given starlark.Dict. It attempts to clone the dictionary, and returns the original dictionary if failed.

func (*SharedDict) Attr added in v0.0.8

func (s *SharedDict) Attr(name string) (starlark.Value, error)

Attr returns the value of the specified attribute, or (nil, nil) if the attribute is not found. It implements the starlark.HasAttrs interface.

func (*SharedDict) AttrNames added in v0.0.8

func (s *SharedDict) AttrNames() []string

AttrNames returns a new slice containing the names of all the attributes of the SharedDict. It implements the starlark.HasAttrs interface.

func (*SharedDict) CloneDict added in v0.0.10

func (s *SharedDict) CloneDict() (*starlark.Dict, error)

CloneDict returns a clone of the underlying dictionary Notice that this method is not a must for the starlark.Value interface, but it's useful for Go code.

func (*SharedDict) CompareSameType added in v0.0.8

func (s *SharedDict) CompareSameType(op syntax.Token, yv starlark.Value, depth int) (bool, error)

CompareSameType compares the SharedDict with another value of the same type. It implements the starlark.Comparable interface.

func (*SharedDict) Freeze added in v0.0.8

func (s *SharedDict) Freeze()

Freeze prevents the SharedDict from being modified.

func (*SharedDict) Get added in v0.0.8

func (s *SharedDict) Get(k starlark.Value) (v starlark.Value, found bool, err error)

Get returns the value corresponding to the specified key, or not found if the mapping does not contain the key. It implements the starlark.Mapping interface.

func (*SharedDict) Hash added in v0.0.8

func (s *SharedDict) Hash() (uint32, error)

Hash returns the hash value of the SharedDict, actually it's not hashable.

func (*SharedDict) Len added in v0.0.10

func (s *SharedDict) Len() int

Len returns the length of the underlying dictionary. Notice that this method is not a must for the starlark.Value interface, but it's useful for Go code.

func (*SharedDict) SetKey added in v0.0.8

func (s *SharedDict) SetKey(k, v starlark.Value) error

SetKey sets the value for the specified key, supports update using x[k]=v syntax, like a dictionary. It implements the starlark.HasSetKey interface.

func (*SharedDict) SetTypeName added in v0.0.10

func (s *SharedDict) SetTypeName(name string)

SetTypeName sets the type name of the SharedDict.

func (*SharedDict) String added in v0.0.8

func (s *SharedDict) String() string

func (*SharedDict) Truth added in v0.0.8

func (s *SharedDict) Truth() starlark.Bool

Truth returns the truth value of the SharedDict.

func (*SharedDict) Type added in v0.0.8

func (s *SharedDict) Type() string

Type returns the type name of the SharedDict.

type StarlarkFunc added in v0.0.12

type StarlarkFunc func(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

StarlarkFunc is a function that can be called from Starlark.

type Unmarshaler

type Unmarshaler interface {
	// UnmarshalStarlark unmarshal a Starlark object to custom type.
	UnmarshalStarlark(starlark.Value) error
}

Unmarshaler is the interface use to unmarshal Starlark values to custom types, i.e. Starlark to Go.

Jump to

Keyboard shortcuts

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