datalarkengine

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2022 License: Apache-2.0, MIT, Apache-2.0, + 1 more Imports: 21 Imported by: 0

Documentation

Overview

datalarkengine contains all the low-level binding logic.

Perhaps somewhat surprisingly, it includes even wrapper types for the more primitive kinds (like string). This is important (rather than just converting them directly to starlark's values) because we may want things like IPLD type information (or even just NodePrototype) to be retained, as well as sometimes wanting the original pointer to be retained for efficiency reasons.

Example (MapWithStructKeys)
ts := schema.MustTypeSystem(
	schema.SpawnString("String"),
	schema.SpawnStruct("FooBar", []schema.StructField{
		schema.SpawnStructField("foo", "String", false, false),
		schema.SpawnStructField("bar", "String", false, false),
	}, schema.SpawnStructRepresentationStringjoin(":")),
	schema.SpawnMap("Map__FooBar__String", "FooBar", "String", false),
)
type FooBar struct{ Foo, Bar string }
type M struct {
	Keys   []FooBar
	Values map[FooBar]string
}

mustExecExample(nil, []schema.TypedPrototype{
	bindnode.Prototype((*FooBar)(nil), ts.TypeByName("FooBar")),
	bindnode.Prototype((*M)(nil), ts.TypeByName("Map__FooBar__String")),
},
	"mytypes",
	`
		#print(mytypes.Map__FooBar__String({"f:b": "wot"})) # I want this to work someday, but it's not quite that magic yet.
		print(mytypes.Map__FooBar__String(_={mytypes.FooBar(foo="f", bar="b"): "wot"}))
	`)
Output:

map<Map__FooBar__String>{
	struct<FooBar>{foo: string<String>{"f"}, bar: string<String>{"b"}}: string<String>{"wot"}
}
Example (String)
mustExecExample(nil, nil,
	"mytypes",
	`
		print(datalark.String('yo'))
	`)
Output:

string{"yo"}
Example (Structs)
// Start with a schema.
typesystem, err := ipld.LoadSchema("<noname>", strings.NewReader(`
		type FooBar struct {
			foo String
			bar String
		}
	`))
if err != nil {
	panic(err)
}

// These are the golang types we'll bind it to.
type FooBar struct{ Foo, Bar string }

// These are the bindings we'll export to starlark.
bindings := []schema.TypedPrototype{
	bindnode.Prototype((*FooBar)(nil), typesystem.TypeByName("FooBar")),
}

// Here's a script running on them:
mustExecExample(nil, bindings, "mytypes", `
		print(mytypes.FooBar)
		print(mytypes.FooBar(foo="hai", bar="wot"))
		x = {"foo": "z"}
		x["bar"] = "å!"
		print(mytypes.FooBar(**x))
	`)
Output:

<built-in function datalark.Prototype<FooBar>>
struct<FooBar>{
	foo: string<String>{"hai"}
	bar: string<String>{"wot"}
}
struct<FooBar>{
	foo: string<String>{"z"}
	bar: string<String>{"å!"}
}

Index

Examples

Constants

View Source
const (
	AnyMode   Mode = 0
	TypedMode      = 1
	ReprMode       = 2
)

Variables

This section is empty.

Functions

func InjectGlobals

func InjectGlobals(globals starlark.StringDict, obj *Object)

See docs on datalark.InjectGlobals. Typically you should prefer using functions in the datalark package, rather than their equivalents in the datalarkengine package.

func NewListMethod added in v0.4.0

func NewListMethod(name string, meth listMethod, numNeed, numAllow int) *starlark.Builtin

func NewMapMethod added in v0.4.0

func NewMapMethod(name string, meth mapMethod, numNeed, numAllow int) *starlark.Builtin

Types

type ArgSeq added in v0.2.0

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

ArgSeq represents a sequence of arguments passed into a function. The sequence may or may not also have a mapping from argument names to positions, as is the case for keyword args or for restructured args

func (*ArgSeq) IsSingleString added in v0.2.0

func (args *ArgSeq) IsSingleString() bool

IsSingleString returns whether the args are a single string value

type Mode added in v0.2.0

type Mode int

Mode of construction, Typed or Repr or default (both)

type Object

type Object starlark.Dict

Object is a starlark.Value that contains arbitrary attributes (like a starlark.Dict), and also lets you access things with dot notation (e.g. as "attrs") in addition to with map key notation.

We use this for creating hierarchical namespaces. (For example, this is how we make typed constructors available without cluttering global namespaces.)

func MakeConstructors added in v0.2.0

func MakeConstructors(prototypes []schema.TypedPrototype) *Object

MakeConstructors returns the constructors for the given prototypes as an Object

func NewObject

func NewObject(size int) *Object

func PrimitiveConstructors added in v0.2.0

func PrimitiveConstructors() *Object

PrimitiveConstructors returns the constructors for primitive types as an Object

func (*Object) Attr

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

func (*Object) AttrNames

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

func (*Object) Freeze

func (d *Object) Freeze()

func (*Object) Get

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

func (*Object) Hash

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

func (*Object) Items

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

func (*Object) Iterate

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

func (*Object) Keys

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

func (*Object) Len

func (d *Object) Len() int

func (*Object) SetKey

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

func (*Object) String

func (d *Object) String() string

func (*Object) Truth

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

func (*Object) Type

func (d *Object) Type() string

type Prototype

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

Prototype wraps an IPLD `datamodel.NodePrototype`, and in starlark, is a `Callable` which acts like a constructor for that NodePrototype.

There is only one Prototype type, and its behavior varies based on the `datamodel.NodePrototype` its bound to.

func NewPrototype added in v0.2.0

func NewPrototype(name string, np datamodel.NodePrototype) *Prototype

func (*Prototype) Attr added in v0.2.0

func (p *Prototype) Attr(name string) (starlark.Value, error)

func (*Prototype) AttrNames added in v0.2.0

func (p *Prototype) AttrNames() []string

func (*Prototype) CallInternal

func (p *Prototype) CallInternal(thread *starlark.Thread, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error)

func (*Prototype) Freeze

func (p *Prototype) Freeze()

func (*Prototype) Hash

func (p *Prototype) Hash() (uint32, error)

func (*Prototype) Name

func (p *Prototype) Name() string

func (*Prototype) NodePrototype added in v0.2.0

func (p *Prototype) NodePrototype() datamodel.NodePrototype

func (*Prototype) String

func (p *Prototype) String() string

func (*Prototype) Truth

func (p *Prototype) Truth() starlark.Bool

func (*Prototype) Type

func (p *Prototype) Type() string

func (*Prototype) TypeName added in v0.2.0

func (p *Prototype) TypeName() string

type Value

type Value interface {
	starlark.Value
	Node() datamodel.Node
}

func NewBool added in v0.2.0

func NewBool(b bool) Value

NewBool constructs a bool Value

func NewBytes added in v0.2.0

func NewBytes(d []byte) Value

NewBytes constructs a bytes Value

func NewFloat added in v0.2.0

func NewFloat(f float64) Value

NewFloat constructs a float Value

func NewInt added in v0.2.0

func NewInt(n int64) Value

NewInt constructs a int Value

func NewLink(x datamodel.Link) Value

NewBytes constructs a Link Value

func NewList added in v0.4.0

func NewList(starList *starlark.List) (Value, error)

NewList converts a starlark.List into a datalark.Value

func NewNull added in v0.2.0

func NewNull() Value

NewNull constructs a null Value

func NewString added in v0.2.0

func NewString(text string) Value

NewString constructs a string Value

func ToValue added in v0.2.0

func ToValue(n datamodel.Node) (Value, error)

Jump to

Keyboard shortcuts

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